【发布时间】:2014-06-12 01:05:36
【问题描述】:
我正在用 Python 为 Hadoop 上的 Hive 查询编写 UDF。我的表有几个bigint 字段和几个string 字段。
我的 UDF 修改了 bigint 字段,将修改后的版本减去到一个新列(也应该是数字),并保持 string 字段不变。
当我在查询中运行我的 UDF 时,结果都是 string 列。
如何在我的 UDF 输出中保留或指定类型?
更多细节:
我的 Python UDF:
import sys
for line in sys.stdin:
# pre-process row
line = line.strip()
inputs = line.split('\t')
# modify numeric fields, calculate new field
inputs[0], inputs[1], new_field = process(int(inputs[0]), int(inputs[1]))
# leave rest of inputs as is; they are string fields.
# output row
outputs = [new_field]
outputs.extend(inputs)
print '\t'.join([str(i) for i in outputs]) # doesn't preserve types!
我将此 UDF 保存为 myudf.py 并将其添加到 Hive。
我的 Hive 查询:
CREATE TABLE calculated_tbl AS
SELECT TRANSFORM(bigintfield1, bigintfield2, stringfield1, stringfield2)
USING 'python myudf.py'
AS (calculated_int, modified_bif1, modified_bif2, stringfield1, stringfield2)
FROM original_tbl;
【问题讨论】:
标签: python hadoop hive apache-pig hadoop-streaming