Hadoop 安装可以参考文章:CentOS 7.6 安装 Hadoop2.6.0
CentOS 7.6
Hadoop 2.6.0
# 一个主节点、一个数据节点
jdk1.7.0_75
hadoop安装目录:/home/hadoop/hadoop-2.6.0
# hadoop 自带 WordCount 程序位置
/home/hadoop/hadoop-2.6.0/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.6.0.jar
# 先关闭所有节点的防火墙设置
systemctl stop firewalld
/home/hadoop/hadoop-2.6.0/sbin/start-dfs.sh
# 停止 hadoop 命令
# /home/hadoop/hadoop-2.6.0/sbin/stop-dfs.sh
vi test.txt
# 添加以下内容
hello summer hi summer try happy hi summer
# 将测试文件复制到 hadoop 文件系统下
hadoop fs -put test.txt /test.txt
hadoop fs -ls /test.txt
hadoop fs -cat /test.txt
hadoop jar /home/hadoop/hadoop-2.6.0/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.6.0.jar wordcount /test.txt /wc_output
hadoop fs -ls /wc_output
hadoop fs -cat /wc_output/part-r-00000
vi 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);
}
}
# 编译生成 class
# Hadoop2.x已经没有了 hadoop-core.jar
# 所以需要单独配置hadoop-common-2.6.0.jar、 hadoop-annotations-2.6.0.jar、hadoop-mapreduce-client-core-2.6.0.jar
# 这些文件都在 /home/hadoop/hadoop-2.6.0/share/hadoop 目录下
# 也可以将这些设置到环境变量中
# javac -classpath 所需jar包路径 WordCount.java
javac -classpath /home/hadoop/hadoop-2.6.0/share/hadoop/common/hadoop-common-2.6.0.jar:/home/hadoop/hadoop-2.6.0/share/hadoop/common/lib/hadoop-annotations-2.6.0.jar:/home/hadoop/hadoop-2.6.0/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.6.0.jar WordCount.java
# 打包成 jar 文件
jar cf wc.jar WordCount*.class
# 利用 hadoop 运行程序
hadoop jar wc.jar WordCount /test.txt /wc_output
hadoop fs -ls /wc_output
hadoop fs -cat /wc_output/part-r-00000
参考文章:【1】运行Hadoop自带的wordcount单词统计程序-香飘叶子-51CTO博客
【2】Hadoop第一个样例Wordcount运行笔记 - Joe’s Blog
【3】Apache Hadoop 3.2.1 – MapReduce Tutorial
【4】Hadoop 2.x 下使用javac编译java文件 - midori的博客 - CSDN博客