从pig调用python udf

llmtgqce  于 2021-06-24  发布在  Pig
关注(0)|答案(1)|浏览(240)

我在用pig拉丁语调用python udf时遇到了一个问题。我正在做一个ascii到二进制的转换,并用python编写了一个脚本,它在python shell中工作,但是如果我们在pig中将它称为python udf,就会得到一个错误,说“namererror:global name‘format’is not defined”。有人能告诉我你对此的看法吗?
----python脚本

@outputSchema("str:chararray") 
def asciitobinary(st):        
               str = ''.join(format(ord(i),'b').zfill(8) for i in st) 
 return str

--Pig手稿

REGISTER 'asctobin.py' USING jython as pyudf 
A = LOAD 'data.txt' USING PigStorage(); 
B = FOREACH A GENERATE  pyudf.asciitobinary($0); 
DUMP B;
Input: 00080
Expected Value: 0011000000110000001100000011100000110000
js4nwp54

js4nwp541#

我认为pig版本使用了jython2.5.3,它不支持str.format。
尝试以下操作:

'%b' % ord(i)

此外,还有一个新的pig版本comming,它使用jython2.7。

相关问题