hadoop mapper reducer sample demo
2017-05-24 00:00
295 查看
package com.traveller.bumble.hadoop.mr.temperature; import com.traveller.bumble.hadoop.mr.wordcount.WordCountMapper; import com.traveller.bumble.hadoop.mr.wordcount.WordCountReducer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; 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.File; import java.io.IOException; /** * Created by macbook on 2017/5/23. */ public class TemperatureApp { public static void main(String[] args) { try { String srcPath = args[0]; String destPath = args[1]; String isLocal = args[2]; boolean flag = false; //本地模式 if (isLocal.equals("1")) { File file = new File(destPath); flag = deleteDir(file); //集群模式 } else { flag = deletePath(new Path(destPath)); } System.out.println(flag); Job job = Job.getInstance(); job.setJobName("temperature"); job.setJarByClass(TemperatureApp.class); job.setNumReduceTasks(1); FileInputFormat.addInputPath(job, new Path(srcPath)); FileOutputFormat.setOutputPath(job, new Path(destPath)); job.setMapperClass(TemperatureMapper.class); job.setReducerClass(TemperatureReducer.class); job.setOutputKeyClass(IntWritable.class); job.setMapOutputValueClass(IntWritable.class); job.waitForCompletion(true); } catch (Exception e) { e.printStackTrace(); } } private static boolean deletePath(Path path) { try { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); boolean flag = fs.deleteOnExit(path); return flag; } catch (IOException e) { e.printStackTrace(); return false; } } private static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; i < children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } return dir.delete(); } }
mapper
package com.traveller.bumble.hadoop.mr.temperature; 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.io.IOException; /** * Created by macbook on 2017/5/24. */ public class TemperatureMapper extends Mapper<LongWritable,Text,IntWritable,IntWritable> { protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String [] strs = value.toString().split(","); IntWritable year = new IntWritable(Integer.valueOf(strs[0])); IntWritable temprature = new IntWritable(Integer.valueOf(strs[1])); context.write(year,temprature); } }
reducer
package com.traveller.bumble.hadoop.mr.temperature; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Created by macbook on 2017/5/24. */ public class TemperatureReducer extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable> { protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { Iterator<IntWritable> iterators = values.iterator(); List<Integer> temp = new ArrayList<>(); while (iterators.hasNext()){ temp.add(iterators.next().get()); } Integer max = Collections.max(temp); context.write(key,new IntWritable(max)); } }
相关文章推荐
- hadoop mapper reducer sample demo
- Hadoop中的进程与Mapper实例,Reducer实例
- Hadoop中map/reduce编程中关于mapper和reducer的Format问题
- hadoop实例(java模板):数字逆序输出 (自定义mapper,reducer,自定义key2类型,重写compareTo函数,HDFS操作)
- [Hadoop] Hadoop 链式任务 : ChainMapper and ChainReducer的使用
- 提高hadoop的mapreduce job效率笔记之一(修改mapper和reducer数量)
- Hadoop-2.4.1学习之Mapper和Reducer
- Hadoop的Mapper和Reducer如何共享变量
- Hadoop的ChainMapper/ChainReducer
- Hadoop基于WordCount的Mapper、Reducer、Combiner、Partitioner和自定义多文件输出
- Hadoop-2.4.1学习之Mapper和Reducer
- Hadoop问题解决之:java.lang.ClassNotFoundException:com.demo.Demo$DemoMapper
- Hadoop-Mapper-Reducer文档学习
- Hadoop streaming编写mapper与reducer
- Hadoop开发周期(二):编写mapper和reducer程序
- hadoop 多个Mapper和Reducer的Job
- 精通HADOOP(十) - MAPREDUCE任务的基础知识 - 创建客户化的Mapper和Reducer
- hadoop中map/reduce编程中关于mapper和reducer的Format问题
- Hadoop开发周期(二):编写mapper和reducer程序
- Hadoop中Mapper和Reducer是单独进程还是线程