EMR 上的 python UDF 中没有名为 simplejson 的模块

No module named simplejson in python UDF on EMR

我正在 运行使用 Pig 执行 Amazon Elastic MapReduce (EMR) 作业。我在将 json 或简单 json 模块导入我的 Python 用户定义函数 (UDF) 时遇到问题。

这是我的代码:

#!/usr/bin/env python
import simplejson as json
@outputSchema('m:map[]')
def flattenJSON(text):
    j = json.loads(text)
    ...

当我尝试在 Pig 中注册函数时,我收到一条错误消息 "No module named simplejson"

grunt> register 's3://chopperui-emr/code/flattenDict.py' using jython as flatten;
2015-05-31 16:53:43,041 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1121: Python Error. Traceback (most recent call last):
File "/tmp/pig6071834754384533869tmp/flattenDict.py", line 32, in <module>
import simplejson as json
ImportError: No module named simplejson

但是,我的 Amazon AMI 包含 Python 2.6,其中包含 json 作为标准包(使用 import json 也不起作用)。另外,如果我尝试使用 pip 安装 simplejson 它说它已经安装(在主节点和核心节点上)。

[hadoop@ip-172-31-46-71 ~]$ pip install simplejson
Requirement already satisfied (use --upgrade to upgrade): simplejson in /usr/local/lib64/python2.6/site-packages

此外,如果我 运行 python 从主节点上的命令行以交互方式工作,它工作正常

[hadoop@ip-172-31-46-71 ~]$ python
Python 2.6.9 (unknown, Apr  1 2015, 18:16:00) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> 

EMR 或 Pig 设置 Python 环境的方式肯定有所不同,但是什么?

Pig UDF 使用 jython,它不适用于 simplejson。

你可以试试: Jyson 作为 Json 解析器