記事ディレクトリ
前書き
- Apacheのファンドプロジェクトとして、Hadoopはビッグデータの処理時間が長いという問題を解決します。その中でも、MapReduce並列処理フレームワークはHadoopの重要なメンバーです。HadoopアーキテクチャはJAVAによって実装されているため、大きなデータを処理するときにJAVAプログラムがより多く使用されます。ただし、MapReduceで深層学習アルゴリズムを使用する場合、Pythonは深層学習とデータマイニングでデータを処理するのに簡単です。上記の考慮事項に基づいて、この記事では、Pythonを使用してMapReduceでWordCount実験を実装する方法を紹介します。記事の内容(コード部分)は、ブロガーのCSDNブログからのものです。参照リンクは最後にあります。
Hadoopストリーム
Hadoopが提供するHadoopStreamingの主な用途は、まず、HadoopStreamを紹介することです。
ストリーミングの役割
- Hadoopストリーミングフレームワークの最大の利点は、任意の言語で記述されたmapおよびreduceプログラムをHadoopクラスターで実行できることです。map/ reduceプログラムは、標準の入力stdinから読み取り、標準の出力stdoutに書き込むだけで済みます。
- 第二に、スタンドアロンのデバッグを簡単に実行できます。ストリーミングは、パイプを前後に接続することでシミュレートでき、map / reduceプログラムはローカルでデバッグできます。
#cat inputfile | mapper | sort | reducer> output - 最後に、ストリーミングフレームワークは、Java言語を使用して変更することなく、ストリーミングパラメータを介して、ジョブの送信中に豊富なパラメータ制御も提供します。mapreduceの多くの高レベルの機能は、スチームパラメータを調整することで完了できます。
ストリーミングの制限
ストリーミングでは、デフォルトでテキストデータTextfileのみを処理できます。バイナリデータの場合、バイナリキーと値をbase64エンコードしてテキストに変換することをお勧めします。
マッパーとレデューサーの前後で、データを含む標準入力と標準出力を変換する必要があります。コピーと解析は、ある程度のオーバーヘッドをもたらします。
ストリーミングコマンドの関連パラメータ
# hadoop jar hadoop-streaming-2.6.5.jar \ [普通选项] [Streaming选项]
一般的なオプションとストリームオプションについては、次のURLを参照してください:https:
//www.cnblogs.com/shay-zhangjin/p/7714868.html
PythonはMapReduceのWordCountを実装しています
- まず、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ステップ(またはプログラム)に任されて実装されます。mapper.pyに実行権限を付与することを忘れないでください:chmod 777 mapper.py
- reducer.pyスクリプト
#!/usr/bin/env python
from operator import itemgetter
import sys
current_word = None
current_count = 0
word = None
# 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)
except ValueError:
# count was not a number, so silently
# ignore/discard this line
continue
# this IF-switch only works because Hadoop sorts map output
# by key (here: word) before it is passed to the reducer
if current_word == word:
current_count += count
else:
if current_word:
# write result to STDOUT
print '%s\t%s' % (current_word, current_count)
current_count = count
current_word = word
# do not forget to output the last word if needed!
if current_word == word:
print '%s\t%s' % (current_word, current_count)
コードを/usr/local/hadoop/reducer.pyに保存します。このスクリプトの機能は、mapper.pyのSTDINから結果を読み取り、各単語の出現の合計を計算して、結果をSTDOUTに出力することです。
また、スクリプトのアクセス許可にも注意してください:chmod 777 reducer.py
- MapReduceタスクを実行するときに、スクリプトが正しく実行されることをテストすることをお勧めします。
root@localhost:/root/pythonHadoop$ echo "foo foo quux labs foo bar quux" | ./mapper.py
foo 1
foo 1
quux 1
labs 1
foo 1
bar 1
quux 1
root@localhost:/root/pythonHadoop$ echo "foo foo quux labs foo bar quux" |./mapper.py | sort |./reducer.py
bar 1
foo 3
labs 1
quux 2
実装効果が上記のとおりであれば、実現可能であることがわかります。MapReduceを実行できます。
- Hadoopプラットフォームでpythonスクリプトを実行します。
[root@node01 pythonHadoop]
hadoop jar contrib/hadoop-streaming-2.6.5.jar
-mapper mapper.py
-file mapper.py
-reducer reducer.py
-file reducer.py
-input /ooxx/*
-output /ooxx/output/
- 最後に、hdfs dfs -cat / ooxx / output / part-00000を実行して、出力結果を表示します。
結果は表示されません。hello.txtファイルの場合は、echoを使用して自分で作成するか、インターネットからテストファイルをダウンロードできます。テスト結果の場合、異なるデータセットの結果は同じではありません。
参考記事:https://blog.csdn.net/crazyhacking/article/details/43304499