gpt4 book ai didi

PIG 的 Python UDF 给出错误

转载 作者:可可西里 更新时间:2023-11-01 16:47:32 24 4
gpt4 key购买 nike

我有一个 Python UDF,可以将数据从十六进制转换为字符串。当我尝试在多个字段上调用 ​​UDF 时,出现错误。这是我的 Python UDF。脚本是hex_to_str.py

#!/usr/bin/python

@outputSchema("field:chararray")
def hextoStr(field):
if(field!=""):
return field.decode("hex")

我正在以下面的方式调用我的 pig 脚本。

register file:/home/myuser/myfolder/hex_to_str.py using jython as convert;
data = LOAD '/user/abc/hexfile' using PigStorage(',') as (Name:chararray, age:chararray);
hex = foreach data generate convert.hextoStr(Name),convert.hextoStr(age);
dump hex;

这是我在运行脚本时遇到的错误。

INFO  org.apache.pig.scripting.jython.JythonFunction - Schema 'field:chararray' defined for func hextoStr
ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1108:
<line 2, column 19> Duplicate schema alias: field

日志文件中的错误也没有说明什么。

<line 2, column 19> Duplicate schema alias: field
at org.apache.pig.newplan.logical.visitor.SchemaAliasVisitor.validate(SchemaAliasVisitor.java:75)
at org.apache.pig.newplan.logical.visitor.SchemaAliasVisitor.visit(SchemaAliasVisitor.java:105)
at org.apache.pig.newplan.logical.relational.LOGenerate.accept(LOGenerate.java:246)

但是,如果我只在一个字段上运行相同的脚本,那么它就可以工作。

register file:/home/myuser/myfolder/hex_to_str.py using jython as convert;
data = LOAD '/user/abc/hexfile' using PigStorage(',') as (Name:chararray, age:chararray);
hex = foreach data generate Name,convert.hextoStr(age);
dump hex;

最佳答案

我怀疑这是因为 @outputSchema("field:chararray") 装饰器指定了 的名称(别名)和数据类型(默认) UDF。当您调用它两次时,您将在 GENERATE 中使用相同的别名两次,因此会导致 Duplicate schema alias: field 错误结果。

您可以运行两个单独的 GENERATE,但我怀疑如果您重新别名,您将能够使用该函数两次。

例如:

hex = foreach data generate convert.hextoStr(Name) as field1,convert.hextoStr(age) as field2;

然后每个结果都会有自己的别名,并且该错误应该会消失。如果没有重新别名,Pig 将无法区分您在 GENERATE 语句中其他地方引用的结果。

回复 OP 的评论:

我怀疑您可以将装饰器中的 field 替换为您想要的任何特定字符串,但是您仍然会遇到使用相同别名在两个不同字段上调用它两次的问题,因此您仍然需要重新别名。我认为不可能在装饰器中使用变量,但在您的 Pig 脚本中重新别名可以实现完全动态。例如您可以将它们别名为 agename 或类似名称以匹配实际的字段名称。

有关 Python UDFs 的更多详细信息,其中部分表示:

Sample Schema String - y:{t:(word:chararray,num:long)}, variable names inside a schema string are not used anywhere, they just make the syntax identifiable to the parser.

关于PIG 的 Python UDF 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35735715/

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