`

Mapreduce多目录/多文件输出

 
阅读更多

Mapreduce多目录/多文件输出

 

一,介绍

1,旧API中有 org.apache.hadoop.mapred.lib.MultipleOutputFormat和org.apache.hadoop.mapred.lib.MultipleOutputs

MultipleOutputFormat allowing to write the output data to different output files.

MultipleOutputs creates multiple OutputCollectors. Each OutputCollector can have its own OutputFormat and types for the key/value pair. Your MapReduce program will decide what to output to each OutputCollector.

2,新API中  org.apache.hadoop.mapreduce.lib.output.MultipleOutputs

整合了上面旧API两个的功能,没有了MultipleOutputFormat。

  The MultipleOutputs class simplifies writing output data to multiple outputs

  Case one: writing to additional outputs other than the job default output. Each additional output, or named output, may be configured with its own             OutputFormat, with its own key class and with its own value class.

  Case two: to write data to different files provided by user

下面这段话来自Hadoop:The.Definitive.Guide(3rd,Early.Release)P251

  “In the old MapReduce API there are two classes for producing multiple outputs: MultipleOutputFormat and MultipleOutputs. In a nutshell, MultipleOutputs is more fully featured, but MultipleOutputFormat has more control over the output directory structure and file naming. MultipleOutputs in the new API combines the best features of the two multiple output classes in the old API.”

二,应用

 1,输出到多个文件或多个文件夹:

     如果只是输出到多个目录,驱动中不需要额外改变,只需要在MapClass或Reduce类中加入如下代码

  private MultipleOutputs<Text,IntWritable> mos;
  public void setup(Context context) throws IOException,InterruptedException {
    mos = new MultipleOutputs(context);
  }
  public void cleanup(Context context) throws IOException,InterruptedException {
    mos.close();
  }

     baseOutputPath=context.getConfiguration().get("path")// 父目录
  然后就可以用mos.write(Key key,Value value,String baseOutputPath)代替context.write(key, value);

  在MapClass或Reduce中使用,输出时也会有默认的文件part-m-00*或part-r-00*,不过这些文件是无内容的,大小为0. 而且只有part-m-00*会传给Reduce。

 2,以多种格式输出:这种输出需要加上文件名,根据业务需求

public class TestwithMultipleOutputs extends Configured implements Tool {

  public static class MapClass extends Mapper<LongWritable,Text,Text,IntWritable> {

    private MultipleOutputs<Text,IntWritable> mos;

    protected void setup(Context context) throws IOException,InterruptedException {
      mos = new MultipleOutputs<Text,IntWritable>(context);
    }

    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
      String line = value.toString();
      String[] tokens = line.split("-");

      mos.write("MOSInt",new Text(tokens[0]), new IntWritable(Integer.parseInt(tokens[1])));  //(第一处)
      mos.write("MOSText", new Text(tokens[0]),tokens[2]);     //(第二处)
      mos.write("MOSText", new Text(tokens[0]),line,tokens[0]+"/");  //(第三处)同时也可写到指定的文件或文件夹中
    }

    protected void cleanup(Context context) throws IOException,InterruptedException {
      mos.close();
    }

  }
  public int run(String[] args) throws Exception {

    Configuration conf = getConf();

    Job job = new Job(conf,"word count with MultipleOutputs");

    job.setJarByClass(TestwithMultipleOutputs.class);

    Path in = new Path(args[0]);
    Path out = new Path(args[1]);

    FileInputFormat.setInputPaths(job, in);
    FileOutputFormat.setOutputPath(job, out);

    job.setMapperClass(MapClass.class);
    job.setNumReduceTasks(0);  

    MultipleOutputs.addNamedOutput(job,"MOSInt",TextOutputFormat.class,Text.class,IntWritable.class);
    MultipleOutputs.addNamedOutput(job,"MOSText",TextOutputFormat.class,Text.class,Text.class);

    System.exit(job.waitForCompletion(true)?0:1);
    return 0;
  }

  public static void main(String[] args) throws Exception {

    int res = ToolRunner.run(new Configuration(), new TestwithMultipleOutputs(), args);
    System.exit(res); 
  }

}

3.复杂应用可以实现MultipleTextOutputFormat 不同的目录都可以context.getconfiguration中获得

 

public class MultiOutputFormatByFileName extends MultipleTextOutputFormat<Text, Text> {
    
    
    
    @Override
    protected String generateLeafFileName(String name) {
        // TODO Auto-generated method stub
        System.out.println(name);
        String[] names = name.split("-");
        
        return names[0]+File.separator+name;
    }
    
    @Override
    protected String generateFileNameForKeyValue(Text key, Text value,
            String name) {
        // TODO Auto-generated method stub
        return super.generateFileNameForKeyValue(key, value, name);
    }
    
}

 main

MultipleOutputs.addNamedOutput(init,"q", MultiOutputFormatByFileName.class , Text.class, Text.class);
MultipleOutputs.addNamedOutput(init,"x", MultiOutputFormatByFileName.class, Text.class, Text.class);
MultipleOutputs.addNamedOutput(init,"bi", MultiOutputFormatByFileName.class, Text.class, Text.class);
MultipleOutputs.addNamedOutput(init,"bu", MultiOutputFormatByFileName.class, Text.class, Text.class);   
分享到:
评论

相关推荐

    大数据实验5实验报告:MapReduce 初级编程实践

    对于两个输入文件,即文件 A 和文件 B,请编写 MapReduce 程序,对两个文件进行合并, 并剔除其中重复的内容,得到一个新的输出文件 C。下面是输入文件和输出文件的一个样例 供参考。 输入文件 A 的样例如下:

    Hadoop mapreduce中使用MultipleOutputFormat的多文件输出

    这个是描述如果设置MultipleOutputFormat,使得mapreduce能够根据文件进行分目录输出

    MapReduce_新型的分布式并行计算编程模型_李成华.pdf

    Ma-pReduce模型受函数式编程语言的启发,将大规模数据处理作业拆分成若干个可独立运行的Map任务,分配到不同的机器上去执行,生成某种格式的中间文件,再由若干个Reduce任务合并这些中间文件获得最后的输出文件。...

    Hadoop的MapReduce中多文件输出.pdf

    Hadoop的MapReduce中多文件输出.pdf

    MapReduce类型及格式

    本文对MapReduce中的数据处理模型进行整体说明,分别对输入和输出的各种类及可口进行讲解,从而可以处理比如文件不分片,非文本文件,多个文件合并等问题

    Hadoop MapReduce多输出详细介绍

    FileOutputFormat及其子类产生的文件放在输出目录下。每个reducer一个文件并且文件由分区号命名:part-r-00000,part-r-00001,等等。有时可能要对输出的文件名进行控制或让每个reducer输出多个文件。MapReduce为此...

    Mapreduce实验报告.doc

    Reduce:不同的Reduce读取各个Map得到的特定的中间文件,将所有相同的中间 文件整合成最后的输出文件。 任务执行基本流程 基本流程图见下一页 首先输入收据文件被Mapreduce库函数分割成M个split集。用户定义的程序...

    大数据与云计算培训学习资料 Hadoop的MapReduce中多文件输出 共9页.pdf

    大数据与云计算培训学习资料 Hadoop的MapReduce中多文件输出 共9页.pdf

    基于Java和mapreduce实现的贝叶斯文本分类器设计.zip

    资源包含文件:课程论文报告word和PDF两个版本+源码及数据 本项目为一个Hadoop课程设计,使用Java语言和map/reduce实现贝叶斯文本分类器。项目的具体内容如下:1:用MapReduce算法实现贝叶斯分类器的训练过程,并...

    java大数据作业_5Mapreduce、数据挖掘

    课后作业 ...5.简述mapreduce流程 6.简述二次排序算法 有输入数据如下所示: 1 2 2 3 2 1 4 6 3 1 3 8 3 2 需要使用二次排序算法,得到如下处理结果: 1 2 2 1 2 3 3 1 3 2 3 8 4 6 请简述处理过程

    基于MapReduce的流量统计(完整Java代码)包括源文件、输出文件、jar包、代码

    根据数据文件phone_data.txt按照如下要求实现MapReduce分析程序编写: 需求: 1)统计每一个手机号耗费的总上行流量、下行流量、总流量 2)将统计结果按照手机归属地不同号段(手机号前3位)输出到不同文件中 3)根据...

    基于MapReduce的学生平均成绩统计

    利用MapReduce实现了求学生成绩的最大值,最小值,及成绩分布。结合我的博客“MapReduce之学生平均成绩”看,效果更好。

    MapReduce:超大机群上的简单数据处理

    计算利用一个输入key/value对集,来产生一个输出key/value对集.MapReduce库的用户用两个函数表达这个计算:map和reduce. 用户自定义的map函数,接受一个输入对,然后产生一个中间key/value对集.MapReduce库把所有具有...

    Google MapReduce(二)

    Google MapReduce实施了一系列的优化。 • 分区函数:保证不同map输出的相同key,落到同一个...• 合并函数:在map结束时,对相同key的多个输出做本地合并,节省总体资源 • 输入文件到map如何切分:随意,切分均匀就行

    Hadoop中MapReduce基本案例及代码(五)

    下面详细介绍MapReduce中Map任务Reduce任务以及MapReduce的执行流程。 Map任务: 读取输入文件内容,解析成key,value对。对输入文件的每一行,解析成key,value对。每一个键值对调用一次map函数。 写自己的逻辑,对...

    大数据技术原理及应用课实验5 :MapReduce初级编程实践

    对于两个输入文件,即文件A和文件B,请编写MapReduce程序,对两个文件进行合并,并剔除其中重复的内容,得到一个新的输出文件C。下面是输入文件和输出文件的一个样例供参考。 ———————————————— 版权...

    Hadoop学习笔记

    bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.2.jar *** 输入文件目录 输出文件目录 *** 本地运行案例 bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.2.jar ...

    hadoop-mapreduce:hadoop MapReduce

    使用HDFS中存储的access.log文件,实现MapReduce以查找每个IP访问该网站的次数。先决条件: 已安装Hadoop。 将access.log文件复制到hdfs中。 假设它在hdfs:/// logs下用法: 将项目导入Eclipse。 将项目导出为jar...

    SecondarySort(代码+jar+输入文件+输出结果)

    完整的MapReduce程序代码 jar包 输入文件 以及输出结果

    java源码部署-MapReduce:在开源应用程序opencsv的junit测试报告上使用Hadoop进行级联的Mapreduce代码。Yo

    为了自动生成报告并复制其输出以在mapreduce程序中使用,编写了一个bash脚本。 运行bash文件:bash run_tests.sh这将生成所有coverage报告并将其复制到mapreduce的输入文件夹中。 (测试位于模块gurpreetkaur_...

Global site tag (gtag.js) - Google Analytics