欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

(四)利用Hadoop MapReduce 实现文本单词频率统计

程序员文章站 2022-06-13 17:33:59
...

1.Map开发。

package com.aa.mapreduce;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class WordMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
 private LongWritable outValue=new LongWritable(1L);
 public void map(LongWritable key,Text value,Context context)throws IOException,InterruptedException{
  String[] lst=value.toString().split(" ");
  for(String item:lst){
   context.write(new Text(item),outValue);
  }
 }
}

 

2.Reduce开发。

package com.aa.mapreduce;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WordReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
 public void reduce(Text key,Iterable<LongWritable> values,Context context)throws IOException,InterruptedException{
  long total=0;
  for(LongWritable item:values){
   total+=item.get();
  }
  context.write(key,new LongWritable(total));
 }
}

3.调度程序开发.

package com.aa.mapreduce;

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.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class WordMain {
 public static void main(String[] args) throws Exception{
  String input_dir="tmp/word/a.txt";
  String outputDir="tmp/output";
  Configuration conf=new Configuration();
  FileSystem fs=FileSystem.get(conf);
  fs.deleteOnExit(new Path(outputDir));
  fs.close();
  Job job=new Job(conf,"WordMain");
  job.setMapperClass(WordMapper.class);
  job.setReducerClass(WordReducer.class);
  ///job.setNumReduceTasks(tasks)
  job.setMapOutputKeyClass(Text.class);
  job.setMapOutputValueClass(LongWritable.class);
  
  job.setOutputKeyClass(Text.class);
  job.setOutputValueClass(LongWritable.class);
  
  job.setInputFormatClass(TextInputFormat.class);
  TextInputFormat.setInputPaths(job, new Path(input_dir));
  
  job.setOutputFormatClass(TextOutputFormat.class);
  TextOutputFormat.setOutputPath(job, new Path(outputDir));
  
  job.waitForCompletion(true);
  
 }

}
4.执行日志。

12/04/26 09:55:57 INFO jvm.JvmMetrics: Initializing JVM Metrics with processName=JobTracker, sessionId=
12/04/26 09:55:57 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
12/04/26 09:55:59 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
12/04/26 09:56:00 WARN mapred.JobClient: No job jar file set.  User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
12/04/26 09:56:00 INFO input.FileInputFormat: Total input paths to process : 1
12/04/26 09:56:00 WARN snappy.LoadSnappy: Snappy native library not loaded
12/04/26 09:56:02 INFO mapred.JobClient: Running job: job_local_0001
12/04/26 09:56:03 INFO mapred.JobClient:  map 0% reduce 0%
12/04/26 09:56:05 INFO mapred.MapTask: io.sort.mb = 100
12/04/26 09:56:05 INFO mapred.MapTask: data buffer = 79691776/99614720
12/04/26 09:56:05 INFO mapred.MapTask: record buffer = 262144/327680
12/04/26 09:56:06 INFO mapred.MapTask: Starting flush of map output
12/04/26 09:56:06 INFO mapred.MapTask: Finished spill 0
12/04/26 09:56:06 INFO mapred.Task: Task:attempt_local_0001_m_000000_0 is done. And is in the process of commiting
12/04/26 09:56:06 INFO mapred.LocalJobRunner:
12/04/26 09:56:06 INFO mapred.Task: Task 'attempt_local_0001_m_000000_0' done.
12/04/26 09:56:06 INFO mapred.LocalJobRunner:
12/04/26 09:56:07 INFO mapred.JobClient:  map 100% reduce 0%
12/04/26 09:56:07 INFO mapred.Merger: Merging 1 sorted segments
12/04/26 09:56:07 INFO mapred.Merger: Down to the last merge-pass, with 1 segments left of total size: 349 bytes
12/04/26 09:56:07 INFO mapred.LocalJobRunner:
12/04/26 09:56:07 INFO mapred.Task: Task:attempt_local_0001_r_000000_0 is done. And is in the process of commiting
12/04/26 09:56:07 INFO mapred.LocalJobRunner:
12/04/26 09:56:07 INFO mapred.Task: Task attempt_local_0001_r_000000_0 is allowed to commit now
12/04/26 09:56:07 INFO output.FileOutputCommitter: Saved output of task 'attempt_local_0001_r_000000_0' to tmp/output
12/04/26 09:56:07 INFO mapred.LocalJobRunner: reduce > reduce
12/04/26 09:56:07 INFO mapred.Task: Task 'attempt_local_0001_r_000000_0' done.
12/04/26 09:56:08 INFO mapred.JobClient:  map 100% reduce 100%
12/04/26 09:56:08 INFO mapred.JobClient: Job complete: job_local_0001
12/04/26 09:56:08 INFO mapred.JobClient: Counters: 13
12/04/26 09:56:08 INFO mapred.JobClient:   FileSystemCounters
12/04/26 09:56:08 INFO mapred.JobClient:     FILE_BYTES_READ=889
12/04/26 09:56:08 INFO mapred.JobClient:     FILE_BYTES_WRITTEN=93252
12/04/26 09:56:08 INFO mapred.JobClient:   Map-Reduce Framework
12/04/26 09:56:08 INFO mapred.JobClient:     Reduce input groups=16
12/04/26 09:56:08 INFO mapred.JobClient:     Combine output records=0
12/04/26 09:56:08 INFO mapred.JobClient:     Map input records=1
12/04/26 09:56:08 INFO mapred.JobClient:     Reduce shuffle bytes=0
12/04/26 09:56:08 INFO mapred.JobClient:     Reduce output records=16
12/04/26 09:56:08 INFO mapred.JobClient:     Spilled Records=46
12/04/26 09:56:08 INFO mapred.JobClient:     Map output bytes=301
12/04/26 09:56:08 INFO mapred.JobClient:     Combine input records=0
12/04/26 09:56:08 INFO mapred.JobClient:     Map output records=23
12/04/26 09:56:08 INFO mapred.JobClient:     SPLIT_RAW_BYTES=98
12/04/26 09:56:08 INFO mapred.JobClient:     Reduce input records=23