mapreduce代码主要包括三个类,map类、reduce类以及测试类!

以wordcount为例,

map类为:

    static class WordMapper extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException{
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreElements()) {
word.set(itr.nextToken());
context.write(word, one);
} }
}

reduce类为:

    static class WordReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
private IntWritable res = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException
{
int sum = 0;
for(IntWritable val:values){
sum += val.get();
}
res.set(sum);
context.write(key, res);
}
}

主函数代码为:

    public static void main(String args[]) throws Exception{
String inputfilepath = "hdfs://localhost:9000/input1";
String outputfilepath = "hdfs://localhost:9000/output4";
Configuration conf = new Configuration();
Job job = new Job(conf);
job.setJarByClass(WordCount.class);
job.setJobName("word-count"); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); job.setMapperClass(WordMapper.class);
job.setReducerClass(WordReducer.class); job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(inputfilepath));
FileOutputFormat.setOutputPath(job, new Path(outputfilepath));
job.waitForCompletion(true);
}

其他的hadoop简单实例代码如:

数字求和:

 package goal;

 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.FloatWritable;
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 org.apache.hadoop.util.GenericOptionsParser; public class Sum { public static class SumMapper extends
Mapper<Object, Text, Text, FloatWritable>{
private Text word = new Text("sum");
private static FloatWritable nv = new FloatWritable(1.0f);
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException
{
StringTokenizer str = new StringTokenizer(value.toString());
float sum = 0;
while(str.hasMoreTokens()){
String s = str.nextToken();
float val = Float.parseFloat(s);
sum = val;
}
nv.set(sum);
context.write(word, nv);
}
}
public static class SumReducer extends
Reducer<Text, FloatWritable, Text, FloatWritable>{
private Text k = new Text("sum");
private FloatWritable res = new FloatWritable();
public void reduce(Text key, Iterable<FloatWritable> values,
Context context) throws IOException, InterruptedException{
float sum = 0;
for(FloatWritable val : values){
float v = val.get();
sum += v;
}
res.set(sum);
context.write(k, res);
}
} public static void main(String args[])throws Exception{
String other[] = {"hdfs://localhost:9000/input2/1.txt", "hdfs://localhost:9000/output3"};
Configuration conf = new Configuration();
System.out.println("yes");
Job job = new Job(conf, "number sum");
job.setJarByClass(Sum.class);
job.setMapperClass(SumMapper.class);
job.setReducerClass(SumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(FloatWritable.class);
FileInputFormat.addInputPath(job, new Path(other[0]));
FileOutputFormat.setOutputPath(job, new Path(other[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
System.out.println("yes");
} }

最新文章

  1. jQuery 3.0的buildFragment
  2. 作业二--注册GitHub的过程
  3. 进入做Mvc项目的时候 返现某个文件夹下面css js png等静态文件都访问不了
  4. 并发式IO的解决方案:多路非阻塞式IO、多路复用、异步IO
  5. POJ 2763:Housewife Wind(树链剖分)
  6. jquery checkbox选中状态
  7. aspx页面状态管理(查询字符串Request与Application)
  8. Codeforces Round #197 (Div. 2) : E
  9. scala学习笔记——操作符
  10. Day06 - Ruby三种存取限制:Public,Protected,Private
  11. 【代码笔记】Web-Javascript-Javascript typeof
  12. Element-ui 中dialog的使用方法
  13. hadoop客户端如何配置
  14. Java内存模型学习笔记
  15. npm突然找不到D:\nodejs\node_modules\npm\bin\npm-cli.js的解决方法
  16. HttpClient Fluent API 高并发优化
  17. 在liunx系统里面进行复制文件的时候报错:cp:omitting directiory
  18. 条件随机场(CRF)理论及应用
  19. Tomcat面试题目
  20. centos7下忘记mysql5.7密码

热门文章

  1. 告别 MongoDB 2.x 拥抱 3.x 版本的5大理由(转)
  2. android中与Adapter相关的控件----ListView
  3. compensation 在 spec 上的意義
  4. nodejs编写实例基础操作
  5. 通用mapper的框架
  6. Fresco对Listview等快速滑动时停止加载
  7. Jinja2文档学习
  8. DotnetBrowser入门教程-入门
  9. 关于 Android 平台开发相关的有哪些推荐书籍?
  10. 2017.2.28 activiti实战--第五章--用户与组及部署管理(二)部署流程资源