python - 在python中使用Hadoop处理大型csv文件

标签 python hadoop amazon-web-services mapreduce

我有一个巨大的 CSV 文件,我想在 Amazon EMR (python) 上使用 Hadoop MapReduce 进行处理。

该文件有 7 个字段,但是,我只查看日期数量 字段。

 "date" "receiptId" "productId" "quantity"  "price" "posId" "cashierId"

首先是我的mapper.py

import sys

def main(argv):
    line = sys.stdin.readline()
    try:
        while line:
            list = line.split('\t')

            #If date meets criteria, add quantity to express key
                if int(list[0][11:13])>=17 and int(list[0][11:13])<=19:
                    print '%s\t%s' % ("Express", int(list[3]))
            #Else, add quantity to non-express key
                else:
                    print '%s\t%s' % ("Non-express", int(list[3]))

            line =  sys.stdin.readline()
except "end of file":
        return None
if __name__ == "__main__":
        main(sys.argv)

对于 reducer ,我将使用流式命令:聚合。

问题:

  1. 我的代码正确吗?我在 Amazon EMR 中运行它,但输出为空。

  2. 所以我的最终结果应该是: express ,XXX 和非 express ,YYY。我可以让它在返回结果之前做一个除法运算吗?只是 XXX/YYY 的结果。我应该把这段代码放在哪里? reducer ??

  3. 另外,这是一个巨大的CSV文件,所以映射会把它分成几个分区吗?或者我是否需要显式调用 FileSplit?如果是这样,我该怎么做?

最佳答案

在这里回答我自己的问题!

  1. 代码有误。如果您使用聚合库来减少,您的输出不会遵循通常的键值对。它需要一个“前缀”。

    if int(list[0][11:13])>=17 and int(list[0][11:13])<=19:
        #This is the correct way of printing for aggregate library
        #Print all as a string.
        print  "LongValueSum:" + "Express" + "\t" + list[3]
    

    其他可用的“前缀”有:DoubleValueSum、LongValueMax、LongValueMin、StringValueMax、StringValueMin、UniqValueCount、ValueHistogram。有关更多信息,请查看此处 http://hadoop.apache.org/common/docs/r0.15.2/api/org/apache/hadoop/mapred/lib/aggregate/package-summary.html .

  2. 是的,如果您想做的不仅仅是基本的 sum、min、max 或 count,您需要编写自己的 reducer。

  3. 我还没有答案。

关于python - 在python中使用Hadoop处理大型csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9255973/

相关文章:

python - 如何在考虑到每个点的位置和方向的情况下连接点

apache - 是否可以将 Apache Helix 与任何其他共识服务一起使用?

Hadoop block 大小和文件大小问题?

linux - 用于 Apache 和 EC2 用户访问的 AWS Wordpress 配置

amazon-web-services - MWAA Airflow 缩放 : what do I do when I have to run frequent & time consuming scripts? (Negsignal.SIGKILL)

amazon-web-services - 如何设置 AWS EKS 节点使用 gp3

python - spyder matplotlib UserWarning : This call to matplotlib. use() 无效,因为已经选择了后端

python - os.path.abspath ('file1.txt' ) 没有返回正确的路径

python - 强制即时 matplotlib 图形更新

hadoop - Reduce函数中的值列表是否确定排序?