【问题标题】:Hadoop Pig: Passing Command Line ArgumentsHadoop Pig:传递命令行参数
【发布时间】:2011-05-09 03:29:53
【问题描述】:

有没有办法做到这一点?例如,传递要处理的文件的名称等?

【问题讨论】:

    标签: hadoop apache-pig


    【解决方案1】:

    这出现在another question中,但是你可以在命令行中指明输入参数并在加载时使用,例如:

    命令行:

    pig -f script.pig -param input=somefile.txt

    脚本.猪:

    raw = LOAD '$input' AS (...);

    请注意,如果您使用的是 Amazon Web Services Elastic Map Reduce,那么对于您提供的任何输入,“$input”都是传递给脚本的内容。

    【讨论】:

    • 如果我们必须以 (2015-08-06 04:54:49) 的格式传递时间戳怎么办,在我的情况下它会抛出错误 - 绝对 URI 中的相对路径:04:36:33 .这是因为我们在时间戳中使用了冒号。
    【解决方案2】:

    您可以使用...
    1. 如果参数很少,则使用-param (-p)
    2.如果有很多参数则使用-param_file (-m)

    您可以根据命令行参数值的性质使用任何一种方法,我在开发和测试脚本时使用 -param。一旦 pig 脚本准备好进行批处理或通过 crontab 运行,我使用 -param_file 以便如果需要任何更改,我可以轻松更新 params.init 文件。

    man pig 会告诉你所有可用的选项。

    -m, -param_file path to the parameter file
    -p, -param key value pair of the form param=val
    

    这里是示例代码...

    students.txt(输入数据)

    001,Rajiv,Reddy,21,9848022337,Hyderabad
    002,siddarth,Battacharya,22,9848022338,Kolkata
    003,Rajesh,Khanna,22,9848022339,Delhi
    004,Preethi,Agarwal,21,9848022330,Pune
    005,Trupthi,Mohanthy,23,9848022336,Bhuwaneshwar
    006,Archana,Mishra,23,9848022335,Chennai
    007,Komal,Nayak,24,9848022334,trivendram
    008,Bharathi,Nambiayar,24,9848022333,Chennai
    

    params.init(保存所有参数的文件)

    fileName='hdfs://horton/user/jgosalia/students.txt'
    cityName='Chennai'
    

    过滤器.猪

    students = LOAD '$fileName' USING PigStorage(',') AS (id:int, firstname:chararray, lastname:chararray, age:int, phone:chararray, city:chararray);
    students = FILTER students BY city == '$cityName';
    DUMP students;
    

    OPT 1:在命令行上使用参数(-param 或 -p)和输出

    pig -param fileName='hdfs://horton/user/jgosalia/students.txt' -param cityName='Chennai' filter.pig
    
    ... Trimming the logs ...
    
    (6,Archana,Mishra,23,9848022335,Chennai)
    (8,Bharathi,Nambiayar,24,9848022333,Chennai)
    

    OPT 2:在命令行上使用参数文件(-param_file 或 -m)和输出

    pig -param_file params.init filter.pig
    
    ... Trimming the logs ...
    
    (6,Archana,Mishra,23,9848022335,Chennai)
    (8,Bharathi,Nambiayar,24,9848022333,Chennai)
    

    注意:使用绝对路径作为文件路径(作为参数和将参数文件路径提供给 -param_file (-m) 时)。

    【讨论】:

    • python 用户注意事项:如果使用 subprocess.call(pig_cmd.split()),请将 pig_cmd 用双引号括起来,例如pig_cmd = "pig -param input_file -param output_file pigscript.pig"
    【解决方案3】:

    将参数传递给 PIG 脚本很简单。

    首先使用'$'在pig中标记你的变量,例如$input_file。然后使用 pig -params input_file='/path/to/data' 将参数传递给您的脚本

    更多信息请看这里:http://wiki.apache.org/pig/ParameterSubstitution

    【讨论】:

      【解决方案4】:

      是的。

      您可以使用 pig 的 param 选项沿命令行选项传递参数。

      --customparam.pig
      --load hdfs/local fs data 
       original = load '$input' using PigStorage('$delimiter');
      --filter a specific field value into another bag  
       filtered = foreach original generate $split; 
      --storing data into hdfs/local fs 
        store filtered into '$output';
      

      pig -x local -f customparam.pig -param input=Pig.csv -param output=OUT/pig -param delimiter="," -param split='$1'

      欲了解更多信息:check this

      【讨论】:

      • 请在此处添加您在其他网站上的帖子的要点。如果其他站点的链接失效,就会发生链接失效。
      猜你喜欢
      • 1970-01-01
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 2018-04-07
      • 1970-01-01
      相关资源
      最近更新 更多