簡體   English   中英

Hadoop-如何收集無值的文本輸出

[英]Hadoop - How to Collect Text Output Without Values

我正在從事地圖縮減工作,我想知道是否有可能向我的輸出文件發出自定義字符串。 沒有計數,沒有其他數量,只是一團文字。

這是我在想什么的基本思想

public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
        // this map doesn't do very much
        String line = value.toString();
        word.set(line);
        // emit to map output
        output.collect(word,one);

        // but how to i do something like output.collect(word)
        // because in my output file I want to control the text 
        // this is intended to be a map only job
    }
}

這種事可能嗎? 這是通過使用hadoop的並行性來創建僅映射作業來轉換數據,但不一定是整個MR框架。 當我運行此作業時,我在每個映射器的hdfs中得到一個輸出文件。

$ hadoop fs -ls /Users/dwilliams/output
2013-09-15 09:54:23.875 java[3902:1703] Unable to load realm info from SCDynamicStore
Found 12 items
-rw-r--r--   1 dwilliams supergroup          0 2013-09-15 09:52 /Users/dwilliams/output/_SUCCESS
drwxr-xr-x   - dwilliams supergroup          0 2013-09-15 09:52 /Users/dwilliams/output/_logs
-rw-r--r--   1 dwilliams supergroup    7223469 2013-09-15 09:52 /Users/dwilliams/output/part-00000
-rw-r--r--   1 dwilliams supergroup    7225393 2013-09-15 09:52 /Users/dwilliams/output/part-00001
-rw-r--r--   1 dwilliams supergroup    7223560 2013-09-15 09:52 /Users/dwilliams/output/part-00002
-rw-r--r--   1 dwilliams supergroup    7222830 2013-09-15 09:52 /Users/dwilliams/output/part-00003
-rw-r--r--   1 dwilliams supergroup    7224602 2013-09-15 09:52 /Users/dwilliams/output/part-00004
-rw-r--r--   1 dwilliams supergroup    7225045 2013-09-15 09:52 /Users/dwilliams/output/part-00005
-rw-r--r--   1 dwilliams supergroup    7222759 2013-09-15 09:52 /Users/dwilliams/output/part-00006
-rw-r--r--   1 dwilliams supergroup    7223617 2013-09-15 09:52 /Users/dwilliams/output/part-00007
-rw-r--r--   1 dwilliams supergroup    7223181 2013-09-15 09:52 /Users/dwilliams/output/part-00008
-rw-r--r--   1 dwilliams supergroup    7223078 2013-09-15 09:52 /Users/dwilliams/output/part-00009

如何在1個文件中獲得結果? 我應該使用身份減少器嗎?

1.要實現output.collect(word) ,可以使用NullWritable類 為此,您必須在Mapper中使用output.collect(word,NullWritable.get()) 請注意,NullWritable是Singleton。

2.如果您不希望有多個文件,可以將reducer的數量設置為1。但這會產生額外的開銷,因為這將涉及網絡上的大量數據改組。 原因是,Reducer必須在運行Mappers的不同機器上獲取其輸入表單。 同樣,所有負載將僅分配給一台計算機。 但是,如果只需要一個輸出文件,則絕對可以使用一個mReducer。 conf.setNumReduceTasks(1)應該足以實現這一目標。

幾個小建議:

  • 我不建議您使用getmerge,因為它將結果文件復制到本地FS上 因此,您必須將其復制回HDFS以便進一步使用。
  • 如果可能,請使用新的API。

如果它是僅映射作業,則輸出文件的數量將等於映射器的數量。 如果需要減速器,它將等於減速器的數量。 但是您始終可以執行hadoop dfs -getmerge <hdfs output directory> <some file>以將輸出目錄中的所有輸出合並到一個文件中。

您可以使用TextOutputFormat輸出純文本文件,例如job.setOutputFormat(TextOutputFormat.class) 然后更改上面的map方法以使用OutputCollector<NullWritable, Text>output.collect(null, "some text") 這將為所有記錄寫some text 如果需要制表符分隔的鍵值,可以將其更改為OutputCollector<Text, Text>output.collect("key", "some text") 這將在輸出中輸出key<tab>some text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM