`

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);   
分享到:
评论

相关推荐

    hadoop的mapreduce把oracle/mysq导入到hbase和hdfs中的程序

    MapReduce作业通常会将输出直接写入HDFS,以便后续的分析任务能快速访问这些数据。 HBase是基于HDFS的分布式NoSQL数据库,它为非结构化和半结构化数据提供了列式存储。HBase适合实时查询,尤其适用于需要随机读取...

    Hadoop演示文稿jjjjjjjjj

    2. 将测试文件内容上传到文件系统上:bin/hdfs dfs -put wcinput/wc.input /user/ctt/mapreduce/wordcount/input/ 3. 查看上传的文件是否正确:bin/hdfs dfs -ls /user/ctt/mapreduce/wordcount/input/ 4. 在HDFS上...

    大数据Mapreduce(1)编程实现文件合并和去重操作.docx

    文件排序操作是指将多个输入文件中的整数进行升序排序,并输出到一个新的文件中。使用MapReduce编程可以实现该操作。 在本实验中,我们使用MapReduce编程实现文件排序操作。首先,我们读取所有输入文件中的整数,并...

    MapReduce详解包括配置文件

    ### MapReduce详解包括配置文件 #### 一、MapReduce概览与原理 MapReduce作为Hadoop的核心组件之一,提供了一种高效、可靠的分布式计算框架。它最初由Doug Cutting基于Google发表的论文《MapReduce: Simplified ...

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

    Hadoop MapReduce 中使用 MultipleOutputFormat 实现多文件输出 Hadoop 默认的输出格式是 TextOutputFormat,输出文件名不可定制。从 Hadoop 0.19.X 开始,Hadoop 提供了一个 org.apache.hadoop.mapred.lib....

    MapReduce2.0程序设计多语言编程(理论+实践)

    例如,CSVFileInputFormat用于读取逗号分隔值文件,而TextOutputFormat则将结果输出为文本格式。 3. **性能优化**:在实践中,我们还需要考虑如何优化MapReduce作业,包括合理使用Combiner减少网络传输,设置合适的...

    MapReduce类型及格式

    MapReduce模型的核心思想是将任务分解为两个阶段:Map(映射)阶段和Reduce(归约)阶段,其输入和输出均为键值对(key-value pair)。 在MapReduce模型中,Map阶段通常处理输入文件中的数据,将输入数据集拆分为...

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

    运行这个MapReduce作业时,我们需要配置Hadoop环境,指定输入文件(A和B)的位置以及输出文件(C)的路径。通过Hadoop的`Job`类和相关输入输出格式类,可以设置这些参数并提交作业到Hadoop集群执行。 总结起来,这...

    Mapreduce-1:python中的MapReduce的孙子/祖父母对

    通过Python的MapReduce实现,我们可以高效地处理大量数据,尤其是当数据分布在多台机器上时,MapReduce的分布式特性能确保任务的并行执行,从而大大提高了处理速度。 总结来说,Python中的MapReduce用于处理大规模...

    Hadoop的MapReduce中多文件输出.pdf

    Hadoop中的多文件输出 Hadoop 的 MapReduce 框架中,默认的输出格式是 TextOutputFormat,这种格式的输出文件名不可定制。在 Hadoop 0.19.X 版本中,提供了一个 org.apache.hadoop.mapred.lib.MultipleOutputFormat...

    大数据实验四-MapReduce编程实践

    本次实验的主要内容是使用MapReduce框架来实现WordCount词频统计功能,即统计HDFS(Hadoop Distributed File System)系统中多个文本文件内的单词出现频率。具体步骤包括: 1. **编写Map处理逻辑**:设计一个Map...

    实验项目 MapReduce 编程

    实验项目“MapReduce 编程”旨在让学生深入理解并熟练运用MapReduce编程模型,这是大数据处理领域中的核心技术之一。实验内容涵盖了从启动全分布模式的Hadoop集群到编写、运行和分析MapReduce应用程序的全过程。 ...

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

    【大数据与云计算培训学习资料 Hadoop的MapReduce中多文件输出】 MapReduce是Hadoop框架的核心部分,主要用于处理大规模数据的分布式计算。在默认情况下,MapReduce任务的输出是一个单一的文件,由TextOutputFormat...

    mapreduce案例文本文件.zip

    这可能意味着压缩包内有一个或多个文件,这些文件可能是用于MapReduce作业的输入数据,也可能是作业执行后的输出结果。如果其中包含源代码,那么读者可以通过查看和运行这些代码来更好地理解MapReduce的工作机制。 ...

    Hadoop MapReduce.pdf

    - 创建一个名为`wordcount`的目录,用于存放输入文件和输出文件。 - 使用`gedit`编辑器打开`WordCount.java`文件。 - 编辑器中的`WordCount.java`代码可以从Hadoop官方文档获取(地址:[Hadoop MapReduce ...

    Hadoop MapReduce多输出详细介绍

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

    MapReduce详解Shuffle过程

    在map端,map task将输出结果存储在内存缓冲区中,当缓冲区快满的时候将缓冲区的数据以一个临时文件的方式存放到磁盘,然后对磁盘中这个map task产生的所有临时文件做合并,生成最终的正式输出文件。 在reduce端,...

    18、MapReduce的计数器与通过MapReduce读取-写入数据库示例

    例如,“Total input files to process”表示处理的总输入文件数量,“number of splits”指示文件被分割成多少个块进行处理,“Running job”显示作业的状态等。 自定义计数器则是开发者根据实际需求创建的,用于...

    22、MapReduce使用Gzip压缩、Snappy压缩和Lzo压缩算法写文件和读取相应的文件

    在MapReduce中,通过设置`mapreduce.output.fileoutputformat.compress`为`true`和`mapreduce.output.fileoutputformat.compress.codec`为`org.apache.hadoop.io.compress.GzipCodec`,可以将输出结果压缩为Gzip格式...

Global site tag (gtag.js) - Google Analytics