Hadoop WordCount 案例
作者:Emperor_LawD
- 2022 年 5 月 10 日
本文字数:1730 字
阅读完需:约 6 分钟

新建文件
在
java文件夹下的com.syh中新建一个 java 文件
在
WordCount.java中写入
package com.syh; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; 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; import java.io.IOException; /** * 词频统计 */ public class WordCountApp { /** * map 阶段 */ public static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> { LongWritable one = new LongWritable(1); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // 分 String line = value.toString(); // 拆分 String[] s = line.split(" "); for (String word : s) { // 输出 context.write(new Text(word), one); } } } /** * reduce 阶段 */ public static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable> { @Override protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException { long sum = 0; // 合并统计 for (LongWritable value : values) { // 求和 sum += value.get(); } context.write(key, new LongWritable(sum)); } } public static void main(String[] args) throws Exception { Configuration configuration = new Configuration(); Job job = Job.getInstance(configuration, "wordcount"); job.setJarByClass(WordCountApp.class); // 设置 map 相关参数 FileInputFormat.setInputPaths(job, new Path(args[0])); job.setMapperClass(MyMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(LongWritable.class); // 设置 reduce 相关参数 job.setReducerClass(MyReducer.class); job.setOutputKeyClass(MyReducer.class); job.setOutputValueClass(LongWritable.class); Path outPath = new Path(args[1]); FileSystem fileSystem = FileSystem.get(configuration); if (fileSystem.exists(outPath)) { // 删除文件 fileSystem.delete(outPath, true); System.out.println("输出路径已存在, 已被删除"); } FileOutputFormat.setOutputPath(job, outPath); // 控制台输出详细信息 // 输出:1 不输出:0 System.exit(job.waitForCompletion(true) ? 0 : 1); } }
复制代码
打包程序
Maven -> hadoopdemo -> Lifecycle -> package
点击 package 后开始打包
上传 jar 包
将打包好的 jar 拖入到虚拟机中
通过 shell 方式将输出文件夹删除
hadoop fs -rm -r /output/wc
复制代码
上传到用户目录 lib 文件夹下进行操作
语法: hadoop jar 主函数全限定名 输入 输出 示例: hadoop jar hadoopdemo-1.0-SNAPSHOT.jar com.syh.WordCountApp hdfs://hadoop000:8020/WordCount.txt hdfs://hadoop000:8020/output/wc
复制代码
完成作业
查看统计结果
通过 shell 方式查看
hadoop fs -cat /output/wc/part-r-00000
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 3
版权声明: 本文为 InfoQ 作者【Emperor_LawD】的原创文章。
原文链接:【http://xie.infoq.cn/article/ca57110b27e2febf594ad02bc】。
本文遵守【CC-BY 4.0】协议,转载请保留原文出处及本版权声明。
Emperor_LawD
关注
愿能如萤火一般,可以在黑夜里发一点光。 2021.05.19 加入
还未添加个人简介










评论