在Hadoop上运行Python脚本

之前已经配置好了Hadoop以及Yarn,可那只是第一步。下面还要在上面运行各种程序,这才是最重要的。

Ubuntu安装时默认已经安装了Python, 可以通过Python –version 查询其版本。
这里写图片描述
因此我们可以直接运行python的脚本了。

Python MapReduce Code

这里我们要用到 Hadoop Streaming API, 通过STIDN(Standard input)和 STDOUT(Standard output)来向Map代码、Reduce代码传递数据。
Python有sys.stdin可以直接读取数据,sys.stdout来输出数据。

1 . 首先建立mapper.py.

用VIM建立mapper.py, 将文件存在/home/hadoop路径下, 代码如下:

#!/usr/bin/env python

import sys

# input comes from STDIN (standard input)
for line in sys.stdin:
    # remove leading and trailing whitespace
    line = line.strip()
    # split the line into words
    words = line.split()
    # increase counters
    for word in words:
        # write the results to STDOUT (standard output);
        # what we output here will be the input for the
        # Reduce step, i.e. the input for reducer.py
        #
        # tab-delimited; the trivial word count is 1
        print '%s\t%s' % (word, 1)

注意,保存时存为unix编码的,可以参考另一篇文章:
编码问题

文件保存后,请注意将其权限作出相应修改:

chmod a+x /home/hadoop/mapper.py

2 . 建立reduce.py
用VIM建立reduce.py, 将文件存在/home/hadoop路径下, 代码如下:

#!/usr/bin/env python

from operator import itemgetter
import sys

current_word = None
current_count = 0
word = None

# input comes from STDIN
for line in sys.stdin:
    # remove leading and trailing whitespace
    line = line.strip()

    # parse the input we got from mapper.py
    word, count = line.split('\t', 1)

    # convert count (currently a string) to int
    try:
        count = int(count)
    except ValueError:
        # count was not a number, so silently
        # ignore/discard this line
        continue

    # this IF-switch only works because Hadoop sorts map output
    # by key (here: word) before it is passed to the reducer
    if current_word == word:
        current_count += count
    else:
        if current_word:
            # write result to STDOUT
            print '%s\t%s' % (current_word, current_count)
        current_count = count
        current_word = word

# do not forget to output the last word if needed!
if current_word == word:
    print '%s\t%s' % (current_word, current_count)

文件保存后,请注意将其权限作出相应修改:

chmod a+x /home/hadoop/reduce.py

首先可以在本机上测试以上代码,这样如果有问题可以及时发现:

~$ echo "foo foo quux labs foo bar quux" | /home/hduser/mapper.py

运行结果如下:
这里写图片描述

再运行以下包含reducer.py的代码:

~$ echo "foo foo quux labs foo bar quux" | /home/hduser/mapper.py | sort -k1,1 | /home/hduser/reducer.py

结果如下:
这里写图片描述

在Hadoop上运行Python代码

准备工作:
下载文本文件:

~$ mkdir tmp/guteberg
cd tmp/guteberg
 wget http://www.gutenberg.org/files/5000/5000-8.txt
 wget http://www.gutenberg.org/cache/epub/20417/pg20417.txt

然后把这二本书上传到hdfs文件系统上:

$ hdfs dfs -mkdir /user/input # 在hdfs上的该用户目录下创建一个输入文件的文件夹
$ hdfs dfs -put /home/hadoop/tmp/gutenberg/*.txt /user/input # 上传文档到hdfs上的输入文件夹中

寻找你的streaming的jar文件存放地址,注意2.6的版本放到share目录下了,可以进入hadoop安装目录寻找该文件:

$ cd $HADOOP_HOME
$ find ./ -name "*streaming*.jar"

然后就会找到我们的share文件夹中的hadoop-straming*.jar文件:
这里写图片描述

由于这个文件的路径比较长,因此我们可以将它写入到环境变量:

vi ~/.bashrc  # 打开环境变量配置文件
# 在里面写入streaming路径
export STREAM=$HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-*.jar

由于通过streaming接口运行的脚本太长了,因此直接建立一个shell名称为run.sh来运行:

hadoop jar $STREAM  \
-files /home/hadoop/mapper.py, /home/hadoop/reducer.py \
-mapper  /home/hadoop/mapper.py \
-reducer  /home/hadoop/reducer.py \
-input /user/input/*.txt \
 -output /user/output

然后”source run.sh”来执行mapreduce。结果就响当当的出来啦。
这里写图片描述

这里写图片描述

用cat来看一下输出结果如下:
这里写图片描述


参考 :
http://www.cnblogs.com/wing1995/p/hadoop.html?https://hadoop.apache.org/docs/r1.2.1/streaming.html
http://hustlijian.github.io/tutorial/2015/06/19/Hadoop%E5%85%A5%E9%97%A8%E4%BD%BF%E7%94%A8.html
http://www.michael-noll.com/tutorials/writing-an-hadoop-mapreduce-program-in-python/

  • 7
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Hadoop YARN上运行PySpark,需要按照以下步骤操作: 1. 安装Hadoop和Spark,并配置好环境变量。 2. 在Hadoop集群上启动YARN服务。 3. 在Spark的conf目录下,创建一个新的spark-defaults.conf文件,并添加以下配置: ``` spark.master yarn spark.submit.deployMode client spark.driver.memory 1g spark.executor.memory 1g spark.executor.instances 2 spark.yarn.appMasterEnv.PYSPARK_PYTHON /usr/bin/python3 ``` 其中,`spark.master`设置为`yarn`,表示使用YARN作为Spark的资源管理器;`spark.submit.deployMode`设置为`client`,表示在客户端模式下提交应用程序;`spark.driver.memory`和`spark.executor.memory`分别设置为1g,表示每个Driver和Executor的内存大小;`spark.executor.instances`设置为2,表示启动2个Executor;`spark.yarn.appMasterEnv.PYSPARK_PYTHON`设置为`/usr/bin/python3`,表示使用Python3作为PySpark的解释器。 4. 在PySpark脚本中,添加以下代码: ``` from pyspark.sql import SparkSession spark = SparkSession.builder.appName("PySparkExample").getOrCreate() # 在这里编写PySpark代码 spark.stop() ``` 其中,`SparkSession`用于创建Spark应用程序的入口点;`appName`设置应用程序的名称;`getOrCreate`方法用于获取现有的SparkSession或创建一个新的SparkSession。 5. 在命令行中,使用以下命令提交PySpark应用程序: ``` spark-submit --master yarn --deploy-mode client --py-files <path-to-py-files> <path-to-pyspark-script> ``` 其中,`--master`设置为`yarn`,表示使用YARN作为Spark的资源管理器;`--deploy-mode`设置为`client`,表示在客户端模式下提交应用程序;`--py-files`指定需要上传到集群的Python文件;`<path-to-pyspark-script>`指定PySpark脚本的路径。 6. 提交应用程序后,可以在YARN的Web界面上查看应用程序的运行情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值