0

我試圖在Amazon的Elastic Map Reduce上運行一個簡單的字計數map-reduce作業,但輸出是亂碼。輸入文件是hadoop序列文件的common crawl文件的一部分。該文件應該是從爬網的網頁中提取的文本(從HTML中剝離)。無法通過stdin使用流式python map-reduce讀取Hadoop序列文件AWS

我的AWS彈性MapReduce的步驟是這樣的:

Mapper: s3://com.gpanterov.scripts/mapper.py 
Reducer: s3://com.gpanterov.scripts/reducer.py 
Input S3 location: s3://aws-publicdatasets/common-crawl/parse-output/segment/1341690169105/textData-00112 
Output S3 location: s3://com.gpanterov.output/job3/ 

作業成功運行,但是輸出是亂碼。只有奇怪的符號,沒有任何文字。我猜這是因爲hadoop序列文件不能通過標準讀取?但是,如何在這樣的文件上運行mr作業?我們必須首先將序列文件轉換爲文本文件嗎?

從部分00000這個樣子的第幾行:

'\x00\x00\x87\xa0 was found 1 times\t\n' 
'\x00\x00\x8e\x01:\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x05\xc1=K\x02\x01\x00\x80a\xf0\xbc\xf3N\xbd\x0f\xaf\x145\xcdJ!#T\x94\x88ZD\x89\x027i\x08\x8a\x86\x16\x97lp0\x02\x87 was found 1 times\t\n' 

這裏是我的映射:

#!/usr/bin/env python 

import sys 

for line in sys.stdin: 
    words = line.split() 
    for word in words: 
     print word + "\t" + str(1) 

我的減速器:

#!/usr/bin/env python 

import sys 

def output(previous_key, total): 
    if previous_key != None: 
     print previous_key + " was found " + str(total) + " times" 

previous_key = None 
total = 0 

for line in sys.stdin: 
    key, value = line.split("\t", 1) 
    if key != previous_key: 
     output(previous_key, total) 
     previous_key = key 
     total = 0 
    total += int(value) 

output(previous_key, total) 

沒有什麼輸入文件錯誤。在本地機器上,我運行了hadoop fs -text textData-00112 | less,這從網頁返回純文本。 任何關於如何在這些類型的輸入文件上運行python streaming mapreduce作業的輸入(common-crawl hadoop序列文件)非常感謝。

回答

1

您需要將SequenceFileAsTextInputFormat作爲inputformat提供給hadoop streaming jar。

我從來沒有使用亞馬遜AWS MapReduce的,但在一個正常的Hadoop的安裝將​​這樣進行:

HADOOP=$HADOOP_HOME/bin/hadoop 
$HADOOP jar $HADOOP_HOME/contrib/streaming/hadoop-*-streaming.jar \ 
    -input <input_directory> 
    -output <output_directory> \ 
    -mapper "mapper.py" \ 
    -reducer "reducer.py" \ 
    -inputformat SequenceFileAsTextInputFormat 
0

由陽光南大的建議解決了該問題。將 -inputformat SequenceFileAsTextInputFormat 添加到aws彈性mapreduce API中的額外參數框中,並且該作業的輸出結果如預期。

相關問題