千家信息网

python map reduce的方法是什么

发表于:2025-02-07 作者:千家信息网编辑
千家信息网最后更新 2025年02月07日,本篇内容主要讲解"python map reduce的方法是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"python map reduce的方法是什
千家信息网最后更新 2025年02月07日python map reduce的方法是什么

本篇内容主要讲解"python map reduce的方法是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"python map reduce的方法是什么"吧!

执行shell脚本run.sh

#! /bin/bashIN_DIR="/input/20140221"OUT_DIR="/output/20140221"HPHOME=$HADOOP_HOME/bin/JAR_PACKAGE=$HADOOP_HOME/contrib/streaming/hadoop-streaming-0.20.2-cdh4u0.jarMAP_FILE=$(pwd)/map.pyRED_FILE=$(pwd)/red.py${HPHOME}hadoop fs -rmr $OUT_DIR${HPHOME}hadoop jar $JAR_PACKAGE \        -numReduceTasks 1 \        -jobconf mapred.job.queue.name=platform \        -input $IN_DIR \        -output $OUT_DIR  \        -file $MAP_FILE  \        -file $RED_FILE \        -mapper $MAP_FILE \        -reducer $RED_FILE \        -inputformat SequenceFileAsTextInputFormatexit $?

map.py

#!/usr/bin/env pythonimport sysfor line in sys.stdin:        line = line.strip()        if line.find("str1")!=-1:                item = line.split('\01')                key = item[3]                print '%s\t%s' % (key, 'str3')        elif line.find("str2")!=-1:                item = line.split('\01')                key = item[2]                value = item[5]                print '%s\t%s\t%s' % (key, 'str3', value)        else:                pass

red.py

#!/usr/bin/env pythonfrom operator import itemgetterimport sysword2count = {}total_count = 0total_money = 0.0total_error = 0pre_pid = ''pre_money = 0.0is_pre_push = 0for line in sys.stdin:        line = line.strip()        if line.find('str3')!=-1:                item = line.split('\t')                total_money += float(item[2])                total_count += 1        else:                passprint total_count, total_error, total_money ##最后才打印

仅作为参考

hadoop默认以'\t'分隔key和value,第一个字段为key

到reduce时,相同key的都在一起被同一个red.py处理,故可以连续读取相同key的每一行。(java中相同key的都以数组的形式放在一次reduce中了,而python却仍要读取多行,但相同key的都排在一起)

到此,相信大家对"python map reduce的方法是什么"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0