HTML 태그가 포함 된 파일의 Hadoop Mapreduce 작업
문제
나는 많은 큰 HTML 파일을 가지고 있으며 가장 자주 사용되는 단어를 찾기 위해 Hadoop Mapreduce 작업을 실행하고 싶습니다. 나는 Mapper와 Reduer를 파이썬으로 작성하고 Hadoop 스트리밍을 사용하여 실행했습니다.
여기 내 맵퍼가 있습니다.
#!/usr/bin/env python
import sys
import re
import string
def remove_html_tags(in_text):
'''
Remove any HTML tags that are found.
'''
global flag
in_text=in_text.lstrip()
in_text=in_text.rstrip()
in_text=in_text+"\n"
if flag==True:
in_text="<"+in_text
flag=False
if re.search('^<',in_text)!=None and re.search('(>\n+)$', in_text)==None:
in_text=in_text+">"
flag=True
p = re.compile(r'<[^<]*?>')
in_text=p.sub('', in_text)
return in_text
# input comes from STDIN (standard input)
global flag
flag=False
for line in sys.stdin:
# remove leading and trailing whitespace, set to lowercase and remove HTMl tags
line = line.strip().lower()
line = remove_html_tags(line)
# split the line into words
words = line.split()
# increase counters
for word in words:
# write the results to STDOUT (standard output);
# what we output here will be the input for the
# Reduce step, i.e. the input for reducer.py
#
# tab-delimited; the trivial word count is 1
if word =='': continue
for c in string.punctuation:
word= word.replace(c,'')
print '%s\t%s' % (word, 1)
여기 내 감속기는 다음과 같습니다.
#!/usr/bin/env python
from operator import itemgetter
import sys
# maps words to their counts
word2count = {}
# input comes from STDIN
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
# parse the input we got from mapper.py
word, count = line.split('\t', 1)
# convert count (currently a string) to int
try:
count = int(count)
word2count[word] = word2count.get(word, 0) + count
except ValueError:
pass
sorted_word2count = sorted(word2count.iteritems(),
key=lambda(k,v):(v,k),reverse=True)
# write the results to STDOUT (standard output)
for word, count in sorted_word2count:
print '%s\t%s'% (word, count)
'Hello World Hello Hello World ...'와 같은 작은 샘플 작은 문자열을 파이프 할 때마다 순위 목록의 적절한 출력을 얻습니다. 그러나 작은 HTML 파일을 사용하고 고양이를 사용하여 HTML을 내 맵퍼에 파이프로 파이프로 사용하려고하면 다음 오류가 발생합니다 (Input2는 일부 HTML 코드가 포함되어 있음).
rohanbk@hadoop:~$ cat input2 | /home/rohanbk/mapper.py | sort | /home/rohanbk/reducer.py
Traceback (most recent call last):
File "/home/rohanbk/reducer.py", line 15, in <module>
word, count = line.split('\t', 1)
ValueError: need more than 1 value to unpack
누구든지 내가 왜 이것을 얻고 있는지 설명 할 수 있습니까? 또한 MapReduce 작업 프로그램을 디버깅하는 좋은 방법은 무엇입니까?
해결책
당신은 단지 다음에도 불구하고 버그를 재현 할 수 있습니다.
echo "hello - world" | ./mapper.py | sort | ./reducer.py
문제는 다음과 같습니다.
if word =='': continue
for c in string.punctuation:
word= word.replace(c,'')
만약에 word
위의 입력 (분할 후)의 경우와 마찬가지로 단일 구두점 마크입니다. 그런 다음 빈 문자열로 변환됩니다. 따라서 교체 후 빈 문자열에 대한 검사를 이동하십시오.
제휴하지 않습니다 StackOverflow