当前环境是 《HDFS 部署之四 (yarn和mapreduce)》 的环境
Example: WordCount v1.0
# 添加环境变量
ssh node-1 "echo 'export HADOOP_CLASSPATH=${JAVA_HOME}/lib/tools.jar' >> /etc/profile.d/hdfs.sh"
ssh node-2 "echo 'export HADOOP_CLASSPATH=${JAVA_HOME}/lib/tools.jar' >> /etc/profile.d/hdfs.sh"
ssh node-3 "echo 'export HADOOP_CLASSPATH=${JAVA_HOME}/lib/tools.jar' >> /etc/profile.d/hdfs.sh"
cd /usr/local/hadoop-3.1.3
# 编辑 WordCount.java 内容如下
import java.io.IOException;
import java.util.StringTokenizer;
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 {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
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);
}
}
# 编译 WordCount.java 并创建一个 jar:
[root@node-1 hadoop-3.1.3]# bin/hadoop com.sun.tools.javac.Main WordCount.java
[root@node-1 hadoop-3.1.3]# jar cf wc.jar WordCount*.class
# 样本文件如下
[root@node-1 hadoop-3.1.3]# bin/hadoop fs -ls /user/joe/wordcount/input/
/user/joe/wordcount/input/file01
/user/joe/wordcount/input/file02
[root@node-1 hadoop-3.1.3]# bin/hadoop fs -cat /user/joe/wordcount/input/file01
Hello World Bye World
[root@node-1 hadoop-3.1.3]# bin/hadoop fs -cat /user/joe/wordcount/input/file02
Hello Hadoop Goodbye Hadoop
# 运行
[root@node-1 hadoop-3.1.3]# bin/hadoop jar wc.jar WordCount /user/joe/wordcount/input /user/joe/wordcount/output
# 查看结果
[root@node-1 hadoop-3.1.3]# bin/hadoop fs -cat /user/joe/wordcount/output/part-r-00000
Bye 1
Goodbye 1
Hadoop 2
Hello 2
World 2
参考: https://hadoop.apache.org/docs/r3.1.3/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html
Comments Closed.