zoukankan      html  css  js  c++  java
  • MapReduce排序输出

     hadoop的map是具有输出自动排序功能的~继续学习~

    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.conf.Configured;
    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.Mapper;
    import org.apache.hadoop.mapreduce.Reducer;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    import org.apache.hadoop.util.Tool;
    import org.apache.hadoop.util.ToolRunner;
    
    import java.io.IOException;
    
    
    public class Sort extends Configured implements Tool {
    
      //这里map将输入的value转化成IntWritable类型,作为输出的key
    public static class Map extends Mapper<Object,Text,IntWritable,IntWritable> { private static IntWritable data = new IntWritable(); public void map(Object key,Text value,Context context) throws IOException,InterruptedException { String line = value.toString(); System.out.println("line" + line); data.set(Integer.parseInt(line)); context.write(data, new IntWritable(1)); } }   //reduce将输入的key复制到输出的value上,然后根据输入的value-list中的元素的个数决定key的输出次数 public static class Reduce extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable> {
        //全局linenum来代表key的位次
    private static IntWritable linenum = new IntWritable(1); public void reduce(IntWritable key,Iterable<IntWritable> values,Context context) throws IOException,InterruptedException{ for(IntWritable val : values){ context.write(linenum,key); System.out.println(linenum+" "+key); linenum = new IntWritable(linenum.get()+1); } } } public int run(String[] args) throws Exception{ Configuration aaa = new Configuration(); Job job = Job.getInstance(aaa); String InputPaths = "/usr/local/idea-IC-139.1117.1/Hadoop/out/datainput/sort.txt"; String OutputPath = "/usr/local/idea-IC-139.1117.1/Hadoop/out/dataout/"; job.setJarByClass(Sort.class); job.setJobName("Sort"); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); FileInputFormat.setInputPaths(job, new Path(InputPaths)); FileOutputFormat.setOutputPath(job, new Path(OutputPath)); job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(IntWritable.class); boolean success = job.waitForCompletion(true); return success ? 0 : 1; } public static void main(String[] args) throws Exception{ int ret = ToolRunner.run(new Sort(),args); System.exit(ret); } }

  • 相关阅读:
    JS文本框下拉提示效果
    JS动态添加删除表格行
    JS验证 数字 电话号码 传真 邮箱 手机号码 邮编 日期
    TreeView 中CheckBox级联选中问题
    HashTable Dictionary
    JS操作Frame对象
    Winfrom 中怎样在回车时设置焦点
    Word 操作(未完待续)
    HTML5特性——prefetching预加载功能
    10个实用的 jQuery Mobile 插件推荐
  • 原文地址:https://www.cnblogs.com/yangsy0915/p/5479960.html
Copyright © 2011-2022 走看看