在其他pig文件中导入公共常量声明的pig文件

r3i60tvu  于 2021-06-25  发布在  Pig
关注(0)|答案(2)|浏览(552)

目标:在constants.pig中定义常量(%declare和%default)语句以实现代码模块化,并在其他pig文件中导入相同的语句。
根据文件:http://pig.apache.org/docs/r0.12.0/cont.html#import-宏、%declare和%default是宏中的有效语句。
面临的问题:pig无法找到声明的参数。
pig文件:constants.pig

  1. %declare ACTIVE_VALUES 'UK';

pig文件:a.pig

  1. IMPORT 'constants.pig';
  2. A = LOAD 'a.csv' using PigStorage(',') AS (country_code:chararray, country_name:chararray);
  3. B = FILTER A BY country_code == '$ACTIVE_VALUES';
  4. dump B;

输入:a.csv

  1. IN,India
  2. US,United States
  3. UK,United Kingdom

错误

  1. Error before Pig is launched
  2. ----------------------------
  3. ERROR 2997: Encountered IOException. org.apache.pig.tools.parameters.ParameterSubstitutionException: Undefined parameter : ACTIVE_VALUES
  4. java.io.IOException: org.apache.pig.tools.parameters.ParameterSubstitutionException: Undefined parameter : ACTIVE_VALUES
  5. at org.apache.pig.impl.PigContext.doParamSubstitution(PigContext.java:414)
  6. at org.apache.pig.Main.runParamPreprocessor(Main.java:810)
  7. at org.apache.pig.Main.run(Main.java:588)
  8. at org.apache.pig.Main.main(Main.java:170)
  9. Caused by: org.apache.pig.tools.parameters.ParameterSubstitutionException: Undefined parameter : ACTIVE_VALUES
  10. at org.apache.pig.tools.parameters.PreprocessorContext.substitute(PreprocessorContext.java:355)
  11. at org.apache.pig.tools.parameters.PreprocessorContext.substitute(PreprocessorContext.java:303)
  12. at org.apache.pig.tools.parameters.PigFileParser.input(PigFileParser.java:67)
  13. at org.apache.pig.tools.parameters.PigFileParser.Parse(PigFileParser.java:43)
  14. at org.apache.pig.tools.parameters.ParameterSubstitutionPreprocessor.parsePigFile(ParameterSubstitutionPreprocessor.java:95)
  15. at org.apache.pig.tools.parameters.ParameterSubstitutionPreprocessor.genSubstitutedFile(ParameterSubstitutionPreprocessor.java:76)
  16. at org.apache.pig.impl.PigContext.doParamSubstitution(PigContext.java:410)
  17. ... 3 more

我对导入的理解是,导入的pig的内容将被执行,并可从调用的pig脚本中获得。如果是这种情况,那么声明的参数应该在导入的pig文件中可用。
关于拥有一个公共pig脚本文件的任何输入/想法,该文件将声明常量,并在其他pig文件中导入相同内容以实现代码模块化。
更新:
在这方面已经提出了一个jira问题。详情请参考以下链接
https://issues.apache.org/jira/browse/pig-2469?jql=text%20~%20%22宏%20%25默认值%22
http://grokbase.com/t/pig/user/121c685c55/error-using-define-in-a-macro
http://mail-archives.apache.org/mod_mbox/pig-user/201201.mbox/%3ccab-acjn+hnazn3aws5ushw+at9rk=oftyb26gxvkxbkjynaodg@mail.gmail.com%3e

rpppsulh

rpppsulh1#

尽管有黑客之嫌,但另一种可能的解决方案是使用python控制器,并在该python控制器中连接这两个文件。您可以在这里阅读有关控制器的信息。
这可能就是它可能看起来的样子,并且将至少破坏您当前的结构:

  1. # !/usr/bin/python
  2. from org.apache.pig.scripting import Pig
  3. def readfile(f):
  4. out = []
  5. with open(f, 'r') as infile:
  6. for line in infile:
  7. out.append(file)
  8. return out
  9. constants = readfile('constants.pig')
  10. script = readfile('a.pig')
  11. # Compile
  12. P = Pig.compile('\n'.join(constants + scripts))
  13. # Run
  14. result = P.bind({}).runSingle()

但是,您也可以尝试在作为 bind 方法。这与使用参数替换的过程相同,我建议这样做。

展开查看全部
kkbh8khc

kkbh8khc2#

这个 IMPORT 关键字用于导入宏,而不是常量。 %declare 以及 %default 是预处理器语句,其作用域是脚本中的所有剩余行。如果您在脚本中声明它,但从其他脚本导入它,它将无法工作,因为它超出了范围。
只要在宏中使用声明的变量,这两个语句在宏中都是有效的。如果需要在脚本之外定义常量以实现模块化,则需要使用参数文件:

  1. ACTIVE_VALUES = 'UK'

然后按以下方式运行pig脚本:

  1. pig -param_file your_params_file.properties -f your_script.pig

如果你真的想用 IMPORT ,您可以创建一个宏,用该常量值进行过滤:

  1. %declare ACTIVE_VALUES 'UK';
  2. DEFINE my_custom_filter(A) RETURNS B {
  3. $B = FILTER $A BY $0 == '$ACTIVE_VALUES ';
  4. };

然后像在脚本中那样导入它,而不是调用 FILTER 函数,调用自己的宏:

  1. IMPORT 'macro.pig';
  2. A = LOAD 'a.csv' using PigStorage(',') AS (country_code:chararray, country_name:chararray);
  3. B = my_custom_filter(A);
  4. dump B;
展开查看全部

相关问题