第八次作业

# coding=gb2312
import sys
import string

if len(sys.argv) == 1 or sys.argv[1] in {"-h", "--help"}:
    print("usage: uniqueword filename_1 filename_2 ... filename_n")
    sys.exit()
 
else:
    words = {}
    strip = string.whitespace + string.punctuation + string.digits + "\"'"
 
    for filename in sys.argv[1:]:
        for line in open(filename):
            for word in line.split():
                word = word.strip(strip)
                if len(word) >= 1:
                    words[word] = words.get(word, 0) + 1





在Ubuntu中实现运行。

  • 准备txt文件
  • 编写py文件
  • python3运行py文件分析txt文件。

 

2.用MapReduce实现词频统计

2.1编写Map函数

  • 编写mapper.py
  • 授予可运行权限
  • 本地测试mapper.py

 

 

2.2编写Reduce函数

  • 编写reducer.py
  • 授予可运行权限
  • 本地测试reducer.py

     

     

     

     

     

    2.3分布式运行自带词频统计示例

    • 启动HDFS与YARN
    • 准备待处理文件
    • 上传HDFS
    • 运行hadoop-mapreduce-examples-2.7.1.jar
    • 查看结果

 

 

 

 

 

  • 2.4 分布式运行自写的词频统计

    • 停止HDFS与YARN

 

posted on 2021-11-23 08:44  探路者0639  阅读(18)  评论(0)    收藏  举报

导航