about 10 years ago
All in one link
其實官方wiki寫的很清楚了,
直接到那邊就可以看到如何安裝、如何測試等等。不過我這邊還是記錄一下我的步驟,以及我遇到的困難及解法。
Overview
既然是要做map-reduce,所以先來份code。
這份code裡面就是基本的word count,
function version
def mapper(key, value):
for word in value.split():
yield word, 1
def reducer(key, values):
yield key, sum(values)
if __name__ == "__main__":
import dumbo
dumbo.run(mapper, reducer)
可以看到mapper function負責map, reducer function負責reduce,是透過dumbo.run來註冊。
class version
你也可以用class來做mapper和reduce的動作,就可以定義__init__在initilize的時候執行一次就好,減少一些overhead。
class Mapper:
def __init__(self):
file = open("excludes.txt", "r")
self.excludes = set(line.strip() for line in file)
file.close()
def __call__(self, key, value):
for word in value.split():
if not word in self.excludes:
yield word, 1
def reducer(key, values):
yield key, sum(values)
if __name__ == "__main__":
import dumbo
dumbo.run(Mapper, reducer, reducer)
可以看到__init__是constructor,一開始會被執行一次;__call__是真正執行的function。
Installation
https://github.com/klbostee/dumbo/wiki/Building-and-installing
Note
若你打算使用國網中心的hadoop,直接看到virtual Python environment的解法即可。
Running
dumbo允許你單機進行測試,單基測完沒問題可以放到hadoop上測試,減少開發時間。
詳細說明
https://github.com/klbostee/dumbo/wiki/Running-programs
Notes
有關Running這邊實在吃了不少苦頭,於是把一些常見問題放在這裡以資各位參考。
- 共同
- 每次都要刪除舊檔案很麻煩 -> 參數加上 -overwrite yes
- 出現Memory error -> 記憶體不足,因為預設的記憶體限制很小,參數加上 -memlimit 1073741824
- Hadoop only
- Output出現亂碼 -> 參數加上 -outputformat text
- 只用到兩個mapper, 一個reducer -> 預設參數是這樣,要設定參數: -nummaptasks [a number]