hadoop python 統計單詞出現的頻率

2021-06-28 02:12:42 字數 1497 閱讀 9196

map函式如下

import sys  

# 呼叫標準輸入流

for line in sys.stdin:

# 讀取文字內容

line = line.strip()

# 對文字內容分詞,形成乙個列表

words = line.split()

# 讀取列表中每乙個元素的值

for word in words:

# map函式輸出,key為word,下一步將進行shuffle過程,將按照key排序,輸出,這兩步為map階段工作為,在本地節點進行

print '%s\t%s' % (word, 1)

reducer函式如下,注意這裡必須設定只有乙個reduce任務

#!/usr/bin/env python  

from operator import itemgetter

import sys

current_word = none

current_count = 0

word = none

sum_count=0

record_word=

record_count=

for line in sys.stdin:

line = line.strip()

word, count = line.split('\t', 1)

sum_count=sum_count+1

try:

count = int(count)

except valueerror:

continue

if current_word == word:

current_count += count

else:

if current_word:

current_count = count

current_word = word

if current_word == word:

for i in range(len(record_word)):

print '%s\t%s' % (record_word[i],float(record_count[i])/sum_count)

輸入檔案1:txt1.txt

hello my name is pan

hello hadoop

hello world

123輸入檔案2:txt2.txt

hello 

123輸出:

單錶多字段統計

在表cccommoncompany中 有province欄位,licensestartdate 開始日期字段,licenseenddate 結束日期字段,我們的要求是按照licensestartdate和licenseenddate所在的年份來統計.見sql select province,sum ...

單鏈表統計偶數個數

單鏈表統計偶數個數 6分 本題要求實現乙個函式,返回帶頭結點的單鏈表中偶數的個數。int evennumber linklist l l是帶頭結點的單鏈表的頭指標,函式evennumber返回l中偶數的個數。如果單鏈表為空,返回0。其中linklist結構定義如下 typedef struct ln...

用python統計多個文字中你想統計的單詞

import collections 計數器 import os import string path users u workspace python learning show me the code 0006 diary diary dir dir os.listdir path 讀取目錄 s...