- Prerequisites
- Lab 23: Creating HDFS
- Lab 24: Installing Hadoop Framework
- Lab 25: Query Processing in HDFS
- Managing Files and Running Jobs
- Cheat Sheet
-
Java Installation
- Ensure Java 8 or higher is installed:
java --version
- Install Java if needed:
sudo apt update sudo apt install openjdk-17-jdk -y
- Ensure Java 8 or higher is installed:
-
SSH Configuration
- Install SSH:
sudo apt install openssh-server openssh-client -y
- Set up passwordless SSH:
ssh-keygen -t rsa -P "" cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
- Install SSH:
-
Download Hadoop:
wget https://downloads.apache.org/hadoop/common/hadoop-3.4.0/hadoop-3.4.0.tar.gz
-
Extract Hadoop:
tar -xvzf hadoop-3.4.0.tar.gz sudo mv hadoop-3.4.0 /usr/local/hadoop
-
Set Hadoop Environment Variables: Edit
.bashrc:nano ~/.bashrcAdd:
export HADOOP_HOME=/usr/local/hadoop export HADOOP_INSTALL=$HADOOP_HOME export HADOOP_MAPRED_HOME=$HADOOP_HOME export HADOOP_COMMON_HOME=$HADOOP_HOME export HADOOP_HDFS_HOME=$HADOOP_HOME export YARN_HOME=$HADOOP_HOME export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
Apply changes:
source ~/.bashrc
-
Set JAVA_HOME: Find Java path:
readlink -f $(which java)Update
hadoop-env.sh:nano $HADOOP_HOME/etc/hadoop/hadoop-env.shAdd:
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 -
Configure HDFS:
-
Edit
core-site.xml:nano $HADOOP_HOME/etc/hadoop/core-site.xmlAdd:
<configuration> <property> <name>fs.defaultFS</name> <value>hdfs://localhost:9000</value> </property> </configuration>
-
Edit
hdfs-site.xml:nano $HADOOP_HOME/etc/hadoop/hdfs-site.xmlAdd:
<configuration> <property> <name>dfs.replication</name> <value>1</value> </property> <property> <name>dfs.namenode.name.dir</name> <value>file:///usr/local/hadoop_data/hdfs/namenode</value> </property> <property> <name>dfs.datanode.data.dir</name> <value>file:///usr/local/hadoop_data/hdfs/datanode</value> </property> </configuration>
-
Create HDFS Directories:
sudo mkdir -p /usr/local/hadoop_data/hdfs/namenode sudo mkdir -p /usr/local/hadoop_data/hdfs/datanode sudo chown -R $USER:$USER /usr/local/hadoop_data
-
Format the NameNode:
hdfs namenode -format
-
-
Start HDFS:
start-dfs.sh
-
Verify Services: Check that NameNode and DataNode are running:
jps
-
Access HDFS Web UI: Navigate to:
http://localhost:9870/
-
Edit
yarn-site.xml:nano $HADOOP_HOME/etc/hadoop/yarn-site.xmlAdd:
<configuration> <property> <name>yarn.resourcemanager.hostname</name> <value>localhost</value> </property> <property> <name>yarn.nodemanager.aux-services</name> <value>mapreduce_shuffle</value> </property> </configuration>
-
Start YARN:
start-yarn.sh
-
Verify YARN Services: Check that ResourceManager and NodeManager are running:
jps
- Create WordCount Java Program:
Insert the following code:
nano WordCount.java
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException { String[] words = value.toString().split("\\s+"); for (String w : words) { word.set(w); context.write(word, one); } } } public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
-
Compile the Program:
hadoop com.sun.tools.javac.Main WordCount.java jar cf wordcount.jar WordCount*.class -
Create an Input File:
echo "Hello Hadoop Hello HDFS" > input.txt
-
Upload File to HDFS:
hdfs dfs -mkdir /input hdfs dfs -put input.txt /input/input.txt
-
Run the MapReduce Job:
hadoop jar wordcount.jar WordCount /input/input.txt /output
-
View the Output:
hdfs dfs -cat /output/part-r-00000
-
Remove the Existing Input File from HDFS:
hdfs dfs -rm /input/input.txt
-
Create a New File with Updated Content Locally:
echo "New content for Hadoop processing" > new_input.txt
-
Upload the New Input File to HDFS:
hdfs dfs -put new_input.txt /input/input.txt
-
**Run the Map
Reduce Job Again**:
hadoop jar wordcount.jar WordCount /input/input.txt /output- View the Output:
hdfs dfs -cat /output/part-r-00000
-
Stop HDFS:
stop-dfs.sh
-
Stop YARN:
stop-yarn.sh
-
Verify Services are Stopped:
jps
No Hadoop-related processes should be listed.
This comprehensive guide should cover everything from setting up Hadoop to running MapReduce jobs and managing input/output files. If you need further assistance, feel free to ask!