gpt4 book ai didi

python - 将 JAR 文件添加到 Python 脚本

转载 作者:可可西里 更新时间:2023-11-01 14:53:42 25 4
gpt4 key购买 nike

我正在尝试使用 JAR 文件并将其功能导入到我的 python 脚本中。 jar 文件与我的 python 脚本和 pig 脚本位于同一目录中

脚本.py

import sys
sys.path.append('/home/hadoop/scripts/jyson-1.0.2.jar')
from com.xhaus.jyson import JysonCodec as json

@outputSchema('output_field_name:chararray')
def get_team(arg0):
return json.loads(arg0)

script.pig

register 'script.py' using jython as script_udf;

a = LOAD 'data.json' USING PigStorage('*') as (line:chararray);
teams = FOREACH a GENERATE script_udf.get_team(line);
dump teams;

我尝试使用的是一个非常简单的 UDF,但由于某种原因,我总是收到一条错误消息,提示“没有名为 xhaus 的模块”。这是该 jar 中的所有类。

$ jar tf jyson-1.0.2.jar 
META-INF/
META-INF/MANIFEST.MF
com/
com/xhaus/
com/xhaus/jyson/
com/xhaus/jyson/JSONDecodeError.class
com/xhaus/jyson/JSONEncodeError.class
com/xhaus/jyson/JSONError.class
com/xhaus/jyson/JysonCodec.class
com/xhaus/jyson/JysonDecoder.class
com/xhaus/jyson/JysonEncoder.class

所以 xhaus 存在于 jar 中,但出于某种原因,它没有被拾取。当我查看一些教程时,他们能够很好地运行这些脚本。我可能遗漏了一个愚蠢的细节,请帮忙。

编辑:

此脚本由 pig 执行。所以pig脚本调用python脚本。并且 python 脚本使用 JysonCodec 类。

pig script.pig

最佳答案

如果您在 pig map reduce 模式下运行此脚本,您需要使 jar 在作业运行时可用。在你的 pig 脚本的顶部,你需要添加以下行

REGISTER /home/hadoop/scripts/jyson-1.0.2.jar;

然后需要注释掉sys.path.append('/home/hadoop/scripts/jyson-1.0.2.jar')从你的 udf 脚本。 jar 中的类将已经可供 udf 使用,因为您已经使用 pig 脚本注册了它。所以需要改变sys.path

希望对您有所帮助。

关于python - 将 JAR 文件添加到 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26794753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com