【问题标题】:Writing a UDF in Python using Pandas throwing error使用 Pandas 抛出错误在 Python 中编写 UDF
【发布时间】:2018-12-20 07:26:29
【问题描述】:

我们正在尝试用 Python 编写 Hive 的 UDF 来清理数据。我们尝试的 UDF 使用的是 Pandas,它正在抛出错误。

当我们尝试在没有 Pandas 的情况下使用另一个 python 代码时,它工作正常。请帮助理解问题。在下面提供 Pandas 代码:

我们已经尝试了 Pandas 的各种方式,但不幸的是没有运气。由于其他没有 Pandas 的 Python 代码运行良好,我们很困惑为什么它会失败?

import sys
import pandas as pd
import numpy as np
for line in sys.stdin:
    df = line.split('\t')
    df1 = pd.DataFrame(df)
    df2=df1.T
    df2[0] = np.where(df2[0].str.isalpha(), df2[0], np.nan)
    df2[1] = np.where(df2[1].astype(str).str.isdigit(), df2[1], np.nan)
    df2[2] = np.where(df2[2].astype(str).str.len() != 10, np.nan, 
    df2[2].astype(str))
    #df2[3] = np.where(df2[3].astype(str).str.isdigit(), df2[3], np.nan)
    df2 = df2.dropna()
    print(df2)

我收到此错误:

FAILED: Execution Error, return code 20003 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask. An error occurred when trying to close the Operator running your custom script.
MapReduce Jobs Launched: 
Stage-Stage-1: Map: 1   HDFS Read: 0 HDFS Write: 0 FAIL
Total MapReduce CPU Time Spent: 0 msec

【问题讨论】:

  • 您不能将 pandas.DataFrame 对象作为 Python UDF 的输出返回,为了使其正常工作,如果您需要多行输出,您应该返回带有制表符作为字段分隔符和 \n 作为行分隔符的字符串,例如1\t2\n3\t4。所以你需要将你的df2 转换为字符串

标签: python pandas hive hive-udf


【解决方案1】:

我认为您需要查看详细的作业日志以获取更多信息。 我的第一个猜测是 Pandas 没有安装在数据节点上。

如果您打算将依赖项与您的工作捆绑在一起,这个答案看起来很适合您:https://stackoverflow.com/a/2869974/7379644

【讨论】:

    猜你喜欢
    • 2014-03-25
    • 2022-09-30
    • 2020-03-01
    • 2021-01-25
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多