【问题标题】:Pig: How to load the output of an hdfs ls into an alias?Pig:如何将 hdfs ls 的输出加载到别名中?
【发布时间】:2013-05-29 21:50:57
【问题描述】:

我正在尝试查看我的 hdfs 中的文件并评估哪些文件早于某个日期。我想做一个hdfs ls 并将其输出传递给猪LOAD 命令。

在对How Can I Load Every File In a Folder Using PIG? 的回答中,@DonaldMiner 包含一个输出文件名的 shell 脚本;我借用它来传递文件名列表。但是,我不想加载文件的内容,我只想加载ls 命令的输出并将文件名视为文本。

这里是 myfirstscript.pig:

test = LOAD '$files' as (moddate:chararray, modtime:chararray, filename:chararray);

illustrate test;

我这样称呼它:

pig -p files="`./filesysoutput.sh`" myfirstscript.pig 

filesysoutput.sh 包含的位置:

hadoop fs -ls -R /hbase/imagestore | grep '\-rw' | awk 'BEGIN { FS = ",[ \t]*|[ \t]+" } {print $6, $7, $8}' | tr '\n' ','

这会生成如下输出:

2012-07-27 17:56 /hbase/imagestore/.tableinfo.0000000001,2012-04-23 19:27 /hbase/imagestore/08e36507d743367e1de57c359360b64c/.regioninfo,2012-05-10 12:13 /hbase/imagestore/08e36507d743367e1de57c359360b64c/0/7818124910159371133,2012-05-10 15:01 /hbase/imagestore/08e36507d743367e1de57c359360b64c/1/5537238047267916113,2012-05-09 19:40 /hbase/imagestore/08e36507d743367e1de57c359360b64c/2/6836317764645542272,2012-05-10 07:04 /hbase/imagestore/08e36507d743367e1de57c359360b64c/3/7276147895747401630,...

由于我想要的只是日期和时间以及文件名,因此我只在输入到 pig 脚本的输出中包含这些字段。当我尝试运行它时,它肯定是在尝试将实际文件加载到 test 别名中:

 $ pig -p files="`./filesysoutput.sh`" myfirstscript.pig 
2013-05-29 17:40:10.773 java[50830:1203] Unable to load realm info from SCDynamicStore
2013-05-29 17:40:10.827 java[50830:1203] Unable to load realm info from SCDynamicStore
2013-05-29 17:40:20,570 [main] INFO  org.apache.pig.Main - Logging error messages to: /Users/username/Environment/pig-0.9.2-cdh4.0.1/scripts/test/pig_1369863620569.log
2013-05-29 17:40:20,769 [main] INFO  org.apache.pig.backend.hadoop.executionengine.HExecutionEngine - Connecting to hadoop file system at: hdfs://stage-hadoop101.cluster:8020
2013-05-29 17:40:20,771 [main] WARN  org.apache.hadoop.conf.Configuration - mapred.used.genericoptionsparser is deprecated. Instead, use mapreduce.client.genericoptionsparser.used
2013-05-29 17:40:20,773 [main] WARN  org.apache.hadoop.conf.Configuration - fs.default.name is deprecated. Instead, use fs.defaultFS
2013-05-29 17:40:20.836 java[50847:1203] Unable to load realm info from SCDynamicStore
2013-05-29 17:40:20.879 java[50847:1203] Unable to load realm info from SCDynamicStore
2013-05-29 17:40:21,138 [main] WARN  org.apache.hadoop.conf.Configuration - fs.default.name is deprecated. Instead, use fs.defaultFS
2013-05-29 17:40:21,452 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1200: Pig script failed to parse: 
<file myfirstscript.pig, line 3, column 7> pig script failed to validate: java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: 2012-07-27 17:56%20/hbase/imagestore/.tableinfo.0000000001
Details at logfile: /Users/username/Environment/pig-0.9.2-cdh4.0.1/scripts/test/pig_1369863620569.log

【问题讨论】:

标签: hadoop hdfs apache-pig


【解决方案1】:

您可以尝试另一种方法 - 使用 dummy.txt 输入文件(单行),然后使用 STREAM alias THROUGH 命令处理 hadoop fs -ls 的输出,就像您目前一样:

grunt> dummy = load '/tmp/dummy.txt';   
grunt> fs -cat /tmp/dummy.txt;
dummy
grunt> files = STREAM dummy THROUGH 
    `hadoop fs -ls -R /hbase/imagestore | grep '\-rw' | awk 'BEGIN { OFS="\t"; FS = ",[ \t]*|[ \t]+" } {print $6, $7, $8}'` 
    AS (moddate:chararray, modtime:chararray, filename:chararray);

请注意,以上内容未经测试 - 我用本地模式 pig 模拟了一些类似的东西,它可以工作(注意我在 awk OFS 中添加了一些选项,并且不得不稍微更改 grep):

grunt> files = STREAM dummy THROUGH \
    `ls -l | grep "\\-rw" | awk 'BEGIN { OFS = "\t"; FS = ",[ \t]*|[ \t]+" } {print $6, $7, $9}'` \
     AS (month:chararray, day:chararray, file:chararray);

grunt> dump files

(Dec,31,CTX.DAT)
(Dec,23,examples.desktop)
(Feb,8,installer.img.gz)
(Feb,8,install.py)
(Apr,25,mapred-site.xml)
(Apr,14,sqlite)

【讨论】:

  • 这是一个非常酷的想法,但是在玩了很多之后,我觉得这行不通。我能够让它运行并且不会失败;但是虽然它声称它“成功地将记录存储在:'file:/tmp/temp168676408/tmp-77338624'”中,但我的系统上不存在该位置。在本地或 mapreduce 模式下会出现相同的结果。我想我将不得不尝试使用 Perl 创建自己的外部脚本。
  • 是的! - 我不明白 dummy.txt 中的任何内容都会通过脚本流式传输,或者反引号中的任何内容;我有一个空的 dummy.txt 文件,所以没有任何东西可以通过脚本流式传输;所以脚本中没有任何内容被分配给别名。我确实看到你的内容中有内容,但我不明白为什么这很重要,所以我没有包含它。一旦我把它放进去,我就从我的脚本中得到了内容。希望我说得通。谢谢@ChrisWhite,你是赢家!
【解决方案2】:

使用基于java或者python的Embedded pig怎么样?

Embedded pig

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多