使用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);    }}
原创粉丝点击