我正在尝试运行一个嵌入式 Pig 脚本(嵌入在 Python 中),我需要在其中获取脚本的输出/结果并将其作为输入反馈到脚本中。我确信有一种简单的方法可以做到这一点,但所有示例似乎都过于简单化并且使用的是单列示例。
我的输入如下所示:networkMap.csv:
NodeH,4,-0.4
NodeH,5,0.2
NodeO,6,0.1
Link,W_1_4,0.2,1,4
Link,W_1_5,-0.3,1,5
Link,W_2_4,0.4,2,4
Link,W_2_5,0.1,2,5
Link,W_3_4,-0.5,3,4
Link,W_3_5,-0.2,3,5
Link,W_4_6,-0.3,4,6
Link,W_5_6,-0.2,5,6
LR,LR,0.9
Target,Target,1
让我们举一个超级简单的例子来说明我想要做什么,去掉所有的应用程序逻辑,只关注输入/输出问题:
#!/usr/bin/python
from org.apache.pig.scripting import *
P = Pig.compile("""
A = LOAD '$input' using PigStorage(',') AS (type:chararray, name:chararray, val:double,iName:chararray,jName:chararray);
STORE A INTO '$outFile' USING PigStorage (',');
""")
params = { 'input': 'networkMap.csv'}
for i in range(2):
outDir = "out_" + str(i + 1)
inputString = ""
params["outFile"] = "out_" + str(i + 1)
bound = P.bind(params)
stats = bound.runSingle()
if not stats.isSuccessful():
raise 'failed'
params["input"] = stats.result("Output1")
我希望我可以说输入=输出,但这不起作用。我也试过:
input = "";
iter = stats.result("A").iterator()
while iter.hasNext():
tuple = iter.next()
input = input + "(" +tuple.toDelimitedString(",") + ")"
params["input"] = input
这确实将输出推回输入,但是 LOAD 函数无法读取它。因为它看起来像一个大记录-
A = LOAD '(NodeI,1,1.0,,)(NodeI,2,0.0,,)(NodeI,3,1.0,,)(NodeH,4,-0.4,,)(NodeH,5,0.2,,)(NodeO,6,0.1,,)(Link,W_1_4,0.2,1,4)(Link,W_1_5,-0.3,1,5)(Link,W_2_4,0.4,2,4)(Link,W_2_5,0.1,2,5)(Link,W_3_4,-0.5,3,4)(Link,W_3_5,-0.2,3,5)(Link,W_4_6,-0.3,4,6)(Link,W_5_6,-0.2,5,6)(LR,LR,0.9,,)(Target,Target,1.0,,)' using PigStorage(',') AS (type:chararray, name:chararray, val:double,iName:chararray,jName:chararray);
我确定我错过了一些简单的方法。