Pythonを使用してHadoopの簡単なMapReduceプログラムを作成する方法

62312 ワード

この例では、Pythonを
http://asfr.blogbus.com/logs/44208067.html簡単な
Hadoop
プログラム.
にもかかわらず
MapReduceフレームワークはJavaで作成されていますが、C++、Pythonなどの言語で実現する必要があります.
Hadoopプログラム.にもかかわらず
Hadoop公式サイトのサンプルプログラムはJythonを使用してJarファイルに作成され、パッケージ化されています.これは明らかに不便ですが、必ずしも実現する必要はありません.Pythonと
Hadoop関連プログラミング
を参照してください/src/examples/python/WordCount.pyの例では、私が何を言っているのかわかります.何がしたいの?
簡単な
Hadoopプログラムは、Jythonが作成してjarパッケージにパッケージ化したプログラムではなく、C-Pythonを使用しています.
私たちのこの例は
MapReduceはPythonを用いて実現され、例はテキストファイルを読み取ることによって単語の出現回数を統計する.結果もテキスト形式で出力され、各行に1つの単語と単語が現れる回数が含まれ、両者の間にタブを使って間隔を考えます.
前提条件
このプログラムを書く前に,君はちゃんと準備しなければならない.
WordCountクラスタは、後期の作業で盲目にならないようにします.架設されていない場合は、Ubuntu Linuxでの構築を説明する簡単なチュートリアルがあります(他のリリースlinux、unixにも適用されます).
Hadoop
Hadoop Distributed File System(HDFS)を使用してUbuntu Linuxで単一ノードのHadoopクラスタを構築する方法
PythonのMapReduceコード
Pythonを使ってMapReduceコードを書くテクニックは、私たちが使っていることです.
Hadoop Distributed File System(HDFS)を使用してUbuntu LinuxでマルチノードのHadoopクラスタを構築する方法は、stdIN(標準入力)とstdOUT(標準出力)を介して、MapとReduceの間でデータを転送するのに役立つ.Pythonの
sys.stdinはデータを入力し、
sys.stdoutはデータを出力します.これはHadoopStreamingが私たちを助けてくれるからです.
その他のこと.これは本当だ,信じないではいけない.
Map: mapper.py
次のコードを
/home/hadoop/mapper.pyでは、stdINからデータを読み込み、単語を行に分けてリストマッピングし、発生回数との関係を生成します.
注意:このスクリプトに十分な権限があることを確認します.
(chmod +x/home/hadoop/mapper.py).
#!/usr/bin/env python
 
import sys
 
# input comes from STDIN (standard input)
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
# 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
print '%s\\t%s' % (word, 1)

このスクリプトでは、単語の合計数は計算されず、「1」が出力されます.は入力に複数回現れる可能性がありますが、計算は後のReduceステップ(またはプログラム)に残して実現されます.もちろん、コードスタイルを変えて、習慣を完全に尊重することができます.
Reduce: reducer.py
コードを
/home/hadoop/reducer.pyでは、このスクリプトの役割は
mapper.pyのstdINで結果を読み出し、各単語の出現回数の合計を計算してstdOUTに出力します.
同様に、スクリプト権限に注意してください.
chmod +x/home/hadoop/reducer.py
#!/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:
# count was not a number, so silently
# ignore/discard this line
pass
 
# sort the words lexigraphically;
#
# this step is NOT required, we just do it so that our
# final output will look more like the official Hadoop
# word count examples
sorted_word2count = sorted(word2count.items(), key=itemgetter(0))
 
# write the results to STDOUT (standard output)
for word, count in sorted_word2count:
print '%s\\t%s'% (word, count)

コードをテストします(
cat data|map|sort|reduce)MapReduce jobテストを実行する前に手動でテストすることをお勧めします.
mapper.pyと
reducer.pyスクリプトを使用して、戻り結果が得られないようにします.
ここでは、MapとReduceの機能をテストする方法についていくつかのアドバイスがあります.
——————————————————————————————————————————————
\r
 # very basic test
hadoop@ubuntu:~$ echo "foo foo quux labs foo bar quux" | /home/hadoop/mapper.py
foo 1
foo 1
quux 1
labs 1
foo 1
bar 1
——————————————————————————————————————————————
hadoop@ubuntu:~$ echo "foo foo quux labs foo bar quux" | /home/hadoop/mapper.py | sort | /home/hadoop/reducer.py
bar 1
foo 3
labs 1
——————————————————————————————————————————————

# using one of the ebooks as example input
# (see below on where to get the ebooks)
hadoop@ubuntu:~$ cat /tmp/gutenberg/20417-8.txt | /home/hadoop/mapper.py
The 1
Project 1
Gutenberg 1
EBook 1
of 1
[...]
(you get the idea)

quux 2

quux 1
——————————————————————————————————————————————

Hadoop Python

, :


HadoopStreaming\r
The Outline of Science, Vol. 1 (of 4) by J. Arthur Thomson\r
The Notebooks of Leonardo Da Vinci
ダウンロードして使用
US-ascii符号化は解凍後のファイルを格納し、一時ディレクトリに保存する.例えば
/tmp/gutenberg.
 hadoop@ubuntu:~$ ls -l /tmp/gutenberg/
total 3592
-rw-r--r-- 1 hadoop hadoop 674425 2007-01-22 12:56 20417-8.txt
-rw-r--r-- 1 hadoop hadoop 1423808 2006-08-03 16:36 7ldvc10.txt
-rw-r--r-- 1 hadoop hadoop 1561677 2004-11-26 09:48 ulyss12.txt
hadoop@ubuntu:~$

HDFS

MapReduce job , HDFS :

hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop dfs -copyFromLocal /tmp/gutenberg gutenberg
hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop dfs -ls
Found 1 items
/user/hadoop/gutenberg <dir>
hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop dfs -ls gutenberg
Found 3 items
/user/hadoop/gutenberg/20417-8.txt <r 1> 674425
/user/hadoop/gutenberg/7ldvc10.txt <r 1> 1423808
/user/hadoop/gutenberg/ulyss12.txt <r 1> 1561677

MapReduce job

, , Python MapReduce job Hadoop 。 ,
HadoopStreaming Map Reduce STDIN STDOUT, 。


hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop jar contrib/streaming/hadoop-0.19.1-streaming.jar
 -mapper /home/hadoop/mapper.py -reducer /home/hadoop/reducer.py -input gutenberg/*
-output gutenberg-output
, Hadoop , Reduce , “-jobconf” :

hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop jar contrib/streaming/hadoop-0.19.1-streaming.jar
-jobconf mapred.reduce.tasks=16 -mapper ...

Hadoop does not honor mapred.map.tasks
HDFS gutenberg , , HDFS
gutenberg-output


hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop jar contrib/streaming/hadoop-0.19.1-streaming.jar
-mapper /home/hadoop/mapper.py -reducer /home/hadoop/reducer.py -input gutenberg/*
-output gutenberg-output

additionalConfSpec_:null
null=@@@userJobConfProps_.get(stream.shipped.hadoopstreaming
packageJobJar: [/usr/local/hadoop-datastore/hadoop-hadoop/hadoop-unjar54543/]
[] /tmp/streamjob54544.jar tmpDir=null
[...] INFO mapred.FileInputFormat: Total input paths to process : 7
[...] INFO streaming.StreamJob: getLocalDirs(): [/usr/local/hadoop-datastore/hadoop-hadoop/mapred/local]
[...] INFO streaming.StreamJob: Running job: job_200803031615_0021
[...]
[...] INFO streaming.StreamJob: map 0% reduce 0%
[...] INFO streaming.StreamJob: map 43% reduce 0%
[...] INFO streaming.StreamJob: map 86% reduce 0%
[...] INFO streaming.StreamJob: map 100% reduce 0%
[...] INFO streaming.StreamJob: map 100% reduce 33%
[...] INFO streaming.StreamJob: map 100% reduce 70%
[...] INFO streaming.StreamJob: map 100% reduce 77%
[...] INFO streaming.StreamJob: map 100% reduce 100%
[...] INFO streaming.StreamJob: Job complete: job_200803031615_0021


[...] INFO streaming.StreamJob: Output: gutenberg-output hadoop@ubuntu:/usr/local/hadoop$


,Hadoop WEB 。
Hadoop , http://localhost:50030/  , :


  Python  Hadoop MapReduce   - ASFR! - ASFR s Blog-on mywy

HDFS gutenberg-output

hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop dfs -ls gutenberg-output
Found 1 items
/user/hadoop/gutenberg-output/part-00000 <r 1> 903193 2007-09-21 13:00
hadoop@ubuntu:/usr/local/hadoop$

dfs -cat

hadoop@ubuntu:/usr/local/hadoop$ bin/hadoop dfs -cat gutenberg-output/part-00000
"(Lo)cra" 1
"1490 1
"1498," 1
"35" 1
"40," 1
"A 2
"AS-IS". 2
"A_ 1
"Absoluti 1
[...]
hadoop@ubuntu:/usr/local/hadoop$

, (") Hadoop 。


Mapper Reducer Python iterators generators

Python iterators and generators


http://www.michael-noll.com/wiki/Writing_An_Hadoop_MapReduce_Program_In_Python#What_we_want_to_do
\r