亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

MultipleOutputFormat和MultipleOutputs

系統(tǒng) 2417 0

?

文章出處:http://www.cnblogs.com/liangzh/archive/2012/05/22/2512264.html

?

?

MultipleOutputFormat和MultipleOutputs

?

一,介紹

?

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兩個(gè)的功能,沒有了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) P 251

?

  “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.”

?

二,應(yīng)用

?

?1, 輸出到多個(gè)文件或多個(gè)文件夾:

?

  驅(qū)動(dòng)中不需要額外改變,只需要在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();
  }
  然后就可以用 mos.write(Key key,Value value,String baseOutputPath) 代替context.write(key, value);
  在MapClass或Reduce中使用,輸出時(shí)也會(huì)有默認(rèn)的文件part-m-00*或part-r-00*,不過這些文件是無內(nèi)容的,大小為0. 而且只有part-m-00*會(huì)傳給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]+"/");   //(第三處)同時(shí)也可寫到指定的文件或文件夾中
    }

?

    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);
  }

?

}

?

測試的數(shù)據(jù):

?

abc-1232-hdf
abc-123-rtd
ioj-234-grjth
ntg-653-sdgfvd
kju-876-btyun
bhm-530-bhyt
hfter-45642-bhgf
bgrfg-8956-fmgh
jnhdf-8734-adfbgf
ntg-68763-nfhsdf
ntg-98634-dehuy
hfter-84567-drhuk

?

結(jié)果截圖:(結(jié)果輸出到/test/testMOSout)

?

MultipleOutputFormat和MultipleOutputs

?

遇到的一個(gè)問題:

?

  如果沒有mos.close(), 程序運(yùn)行中會(huì)出現(xiàn)異常:

?

  12/05/21 20:12:47 WARN hdfs.DFSClient: DataStreamer Exception:

  org.apache.hadoop.ipc.RemoteException:org.apache.hadoop.hdfs.server.namenode.LeaseExpiredException: No lease on
   /test/mosreduce/_temporary/_attempt_local_0001_r_000000_0/h-r-00000 File does not exist. [Lease. Holder: DFSClient_-352105532, pendingcreates: 5]

MultipleOutputFormat和MultipleOutputs


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 精品综合久久久久久97超人该 | 午夜看毛片 | 日韩一级欧美一级毛片在 | 国产一级毛片视频 | 亚洲专区中文字幕 | 精品久久久中文字幕一区 | 亚洲aa在线 | 欧美日韩中文字幕久久伊人 | 99九九99九九九视频精品 | 美日韩中文字幕 | 日韩中文字幕不卡 | 欧美亚洲综合一区 | 国产nv精品你懂得 | 久久久综合网 | 国产精品21区 | 久久久这里只有精品加勒比 | 在线精品国产一区二区 | 激情五月综合婷婷 | 黄色综合网站 | 成人亚洲国产综合精品91 | www.99爱| 久久综合国产 | 日本一区二区三区中文字幕 | 久久精品国产精品青草图片 | 亚洲不卡免费视频 | 青青青在线视频播放免费 | 日本最黄视频 | 理论在线视频 | 国产精品久久久久国产精品三级 | 精品视频在线播放 | 国产 麻豆 欧美亚洲综合久久 | 亚洲综合在线观看视频 | 国产免费久久精品99久久 | 欧美日本在线一区二区三区 | 成人短视频在线观看免费 | 国产高清对白国产露脸91 | 国产精品亚洲欧美大片在线看 | 亚洲国产高清视频在线观看 | 婷婷综合激情 | 国产h版大片在线播放 | 色资源在线观看 |