词频统计任务编程实践
编写一个MapReduce词频统计程序,你需要使用Hadoop或其他MapReduce框架。以下是一个简单的Python示例,使用Hadoop Streaming来执行词频统计任务。请确保你已经安装了Hadoop和配置了Hadoop Streaming。
假设你已经创建了两个文本文件wordfile1.txt
和wordfile2.txt
,然后你可以编写以下Map和Reduce脚本:
python
#!/usr/bin/env python
import sys
import re
# 使用正则表达式来拆分单词
word_pattern = re.compile(r'\w+')
for line in sys.stdin:
# 移除开头和结尾的空格,并转换为小写
line = line.strip().lower()
words = word_pattern.findall(line)
for word in words:
# 输出键值对(单词, 1)
print(f"{word}\t1")
python
#!/usr/bin/env python
import sys
current_word = None
current_count = 0
for line in sys.stdin:
word, count = line.strip().split('\t', 1)
count = int(count)
if current_word == word:
current_count += count
else:
if current_word:
print(f"{current_word}\t{current_count}")
current_word = word
current_count = count
if current_word:
print(f"{current_word}\t{current_count}")
接下来,你可以使用Hadoop Streaming来运行MapReduce任务:
bash
hadoop jar /path/to/hadoop-streaming.jar \
-files mapper.py,reducer.py \
-mapper mapper.py \
-reducer reducer.py \
-input wordfile1.txt,wordfile2.txt \
-output wordcount_output
这将在Hadoop集群上运行你的MapReduce任务,统计单词的词频,最后的结果将存储在wordcount_output
文件夹中。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~