package day02;
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.output.FileOutputFormat;
import java.io.IOException;
/**
* @author
@Create 2019-09-17 16:33 *
* * /
// LongWritable (Map input side) of the recording offset
// data types to be processed read Text
// (note that these two types immutable )
// the Text (map output terminal)
// IntWritable
public class WordCount1 {
// map-side
public static class MapTask the extends Mapper <LongWritable, the Text, the Text, IntWritable> {
@Override
protected void map (LongWritable Key, the Text value, the context context ) throws IOException, InterruptedException {
//Offset value is the key to maintain our data
// context is used to write the processed data out
String [] words = value.toString () Split ( ",." );
// write the data out (hadoop, 1 )
for (String Word: words) {
context.write ( new new the Text (Word), new new IntWritable (. 1 ));
}
}
}
// the reduce side (Hadoop,. 1) (Hadoop, 35)
public static class ReduceTask the extends the Reducer < the Text, IntWritable, the Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
//key 指的是 单词 hadoop
//value (1,1,1,1,1,1,1)
int count = 0;
for (IntWritable value : values) {
count++;
}
//写出去
context.write(new Text(key),new IntWritable(count));
}
}
public static void main(String[] args) throwsIOException, a ClassNotFoundException, InterruptedException {
System.setProperty ( "HADOOP_USER_NAME", "the root" );
// tell which type of input jvm to run
the Configuration = the conf new new the Configuration ();
// set the parameters of the connection hadoop cluster
conf.set ( "fs. defaultFS "," HDFS: // hadoop01: 9000 " );
the Job Job = Job.getInstance (the conf);
job.setMapperClass (MapTask. class );
job.setReducerClass (ReduceTask. class );
job.setJarByClass (WordCount1. class ) ;
// the Job told the output parameters of the type of output
job.setMapOutputKeyClass (Text. class );
job.setMapOutputValueClass (IntWritable. class );
job.setOutputKeyClass (. Text class );
job.setOutputValueClass (. IntWritable class );
// the Job tell input with the output path
FileInputFormat.addInputPath (job, new new Path ( "/ Beida / wc.txt" ));
FileOutputFormat.setOutputPath (the Job, new new Path ( "/ test1" ));
// ? tips about where b is a triple expression
boolean b = job.waitForCompletion ( to true );
System.out.println (b "Niubi success":? "problem" );
}
}