使用Hadoop实现单词统计
来源:互联网 发布:最好的加工软件 编辑:程序博客网 时间:2024/05/16 03:21
使用Hadoop实现单词统计
本篇博客介绍使用Hadoop实现单词统计(来源:慕课网学习),下面是具体步骤:
1.创建目录
hadoop fs -mkdir input hadoop fs -mkdir word_count_class
2.复制博客末尾的代码到Java文件,命名WordCount.java,并运行如下命令编译java文件
javac -classpath /opt/hadoop-1.2.1/hadoop-core-1.2.1.jar:/opt/hadoop-1.2.1/lib/commons-cli-1.2.jar -d 编译后地址(自己制定,e.g.:wordcount_class) 编译后的文件名(自己指定,e.g.:WordCount)
3.打包指令
jar -cvf 打包后文件名.jar(自己指定) *.class
4.在当前目录的子目录input目录下创建两个输入文件(input1,input2),里面加入需要统计的单词,用空格隔开(e.g.:word hadoop …),并输入文件提交输入文件给hadoop
hadoop fs -put 文件路径 提交后的路径
hadoop fs -put input/* input_wordcount/
5.提交jar给hadoop执行
hadoop jar jar包路径 执行的主函数名(主类名,main方法所在类名) 输入目录名 输出目录名
hadoop jar word_count_class/wordcount.jar WordCount input_wordcount output_wordcount
6.运行结果文件存在output_wordcount中,运行如下命令即可查看
hadoop fs -ls ouput_wordcount
7、查看output_wordcount目录下part-….文件即为统计结果
hadoop fs -cat ouput_wordcount/part-..
下面是代码:
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.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.input.TextInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;public class WordCount { public static class WordCountMap extends Mapper<LongWritable, Text, Text, IntWritable> { private final IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer token = new StringTokenizer(line); while (token.hasMoreTokens()) { word.set(token.nextToken()); context.write(word, one); } } } public static class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } context.write(key, new IntWritable(sum)); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = new Job(conf); job.setJarByClass(WordCount.class); job.setJobName("wordcount"); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.setMapperClass(WordCountMap.class); job.setReducerClass(WordCountReduce.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); }}
阅读全文
0 0
- 使用Hadoop实现单词统计
- Hadoop C++单词统计
- hadoop hellokitty 单词统计
- 使用spark的dataframe实现单词统计
- eclipse使用hadoop来统计单词的个数
- Hadoop实例-----统计单词个数
- 使用hadoop实现关联商品统计
- 使用Java实现在单机上统计单词的数目
- 使用Hadoop中的map-reduce机制实现单词的计数
- 使用Hadoop的mapReduce实现计算单词数
- hadoop简单应用-统计文本文件单词个数
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- hadoop的统计单词程序WordCount
- 学习hadoop(2)单词统计
- Hadoop-MapReduce初步应用-统计单词个数
- MyEclipse试用期到期破解方法(不用下载破解器)
- 【HDU】4507 吉哥系列故事——恨7不成妻 数位DP
- ajax 实现订单商品数量的增减及订单的删除进行异步更新界面
- linux caffe 画loss和accuracy随iter变化的曲线
- IDEA 安装PlantUML
- 使用Hadoop实现单词统计
- Jdk9/Java9: modular feature
- 【JavaScript】——之问题中学习
- 初识JavaScript03
- 数据库系统原理(一)--键和范式
- 决策树----ID3
- js中的prototype的作用
- CodeForces-191A Dynasty Puzzles(动态规划DP)
- 近期计划