开始学习hadoop,过程十分艰难,借鉴了一些资料,代码仅供自己学习参考
需要先虚拟机dfs和yarn群起,网页打开hadoop100,然后操作
以下是代码实现
Mapper:
package mapreduce1210;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.awt.image.ImageProducer;
import java.io.IOException;
public class it863_01_Mapper extends Mapper<LongWritable, Text,Text, IntWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] s = value.toString().replace("、",",").split(",");
for (String s1:s){
context.write(new Text(s1),new IntWritable(1));
}
}
}
Reducer:
package mapreduce1210;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class it863_01_Reducer extends Reducer<Text, IntWritable,Text, LongWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
long count = 0;
for (IntWritable s_count:values){
s_count.get();
count++;
}
context.write(key,new LongWritable(count));
}
}
Driver:
package mapreduce1210;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.FileInputStream;
import java.io.IOException;
public class it863_01_Driver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Job job = Job.getInstance();
job.setJarByClass(it863_01_Driver.class);
job.setMapperClass(it863_01_Mapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setReducerClass(it863_01_Reducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.addInputPath(job,new Path("D:\\863863\\day1210\\西游记(1).txt"));
FileOutputFormat.setOutputPath(job,new Path("D:\\863863\\day1210\\out"));
boolean success = job.waitForCompletion(true);
if (success){
System.exit(1);
}else {
System.exit(0);
}
}
}
输出的文件截图,东西在part-r-00000里面 用记事本打开就可以看
|