【问题标题】:ImportError Python Hive UDFImportError Python Hive UDF
【发布时间】:2018-10-29 23:50:56
【问题描述】:

我想将一些常量放在一个 Python 文件中并将其导入到另一个文件中。我创建了两个文件,一个带有常量,一个导入它,一切都在本地运行良好:

constants.py

CONST = "hi guy"

test_constants.py

from constants import CONST
import sys

for line in sys.stdin:
    print(CONST)

本地测试

$ echo "dummy" | python test_constants.py
hi guy

使用 Hive 进行测试(直线)

hive> add file hdfs://path/.../test_constants.py;
No rows affected (0.191 seconds)
hive> add file hdfs://path/.../constants.py;
No rows affected (0.049 seconds)
hive> list files;
resource
/tmp/bb09f878-7e36-4aa2-8566-a30950072bcb_resources/test_constants.py
/tmp/bb09f878-7e36-4aa2-8566-a30950072bcb_resources/constants.py
2 rows selected (0.179 seconds)
hive> with t as (select 1 as dummy) 
  select transform (dummy) 
  using 'python test_constants.py' 
  as dummy_out 
  from t;
Error: org.apache.hive.service.cli.HiveSQLException: 
Error while processing statement: FAILED: 
Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.tez.TezTask. 
Vertex failed, vertexName=Map 1, vertexId=vertex_1535407036047_170618_1_00, diagnostics=[Task failed, taskId=task_1535407036047_170618_1_00_000000, diagnostics=[TaskAttempt 0 failed, info=[Error: Error while running task ( failure ) : attempt_1535407036047_170618_1_00_000000_0:
java.lang.RuntimeException: java.lang.RuntimeException: Hive Runtime Error while closing operators

日志如下所示:

Log Type: stderr
Log Upload Time: Mon Oct 29 15:50:42 -0700 2018
Log Length: 251

2018-10-29 15:45:16 Starting to run new task attempt: attempt_1535407036047_170618_1_00_000000_3
Traceback (most recent call last):
  File "test_constants.py", line 1, in <module>
    from constants import CONST
ImportError: No module named constants

这两个文件似乎在同一个文件夹中,因此导入似乎应该可以工作,但它没有。

于 2018 年 10 月 30 日添加:

@serge_k 的回答有效,但是,我最初遇到了麻烦,因为我拥有 Python UDF 的路径最初不适用于 hive。将所有文件移动到 HDFS 上的 /tmp 后,一切都按预期工作。

hive> add file hdfs://dev/tmp/transforms;
No rows affected (0.108 seconds)
hive> list files;
resource
/tmp/61ecb363-ead6-4679-8f58-3611db9487b2_resources/transforms
1 row selected (0.202 seconds)
hive> select transform (col) using 'python transforms/test_constants.py' as dummy_out from dummy.test;
dummy_out
hi guy
hi guy
hi guy
hi guy
hi guy
hi guy
hi guy
hi guy
hi guy
hi guy
10 rows selected (63.734 seconds)

【问题讨论】:

    标签: python hive hiveql hive-udf


    【解决方案1】:

    将您的 python 脚本放在一个文件夹中,例如files,将整个文件夹添加到分布式缓存中,并将脚本调用为python files/script_name.py

    hive> add file ./files;
    Added resources: [./files]
    hive> with t as (select 1 as dummy) select transform (dummy) 
          using 'python files/test_constants.py' as dummy_out from t;
    
    OK
    hi guy
    

    【讨论】:

    • 我最初收到关于权限的错误,因为beeline 用户没有对整个文件夹的执行权限。通过将转换文件夹复制到 HDFS 上的/tmp,它就可以工作了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多