【问题标题】:Apache Pig - Removing the pseudo-column added by -tagFileApache Pig - 删除 -tagFile 添加的伪列
【发布时间】:2015-08-04 18:32:18
【问题描述】:

我有 test_YYYYMM.txt 格式的文件。我正在使用 '-tagFile' 和 SUBSTRING() 来提取年份和月份以用于我的猪脚本。

文件名作为伪列添加到元组的开头。

在执行 DUMP 之前,我想删除该列。只使用我需要的列进行 FOREACH ... GENERATE 不起作用,它仍然保留伪列。

有没有办法删除此列?

我的示例脚本如下

raw_data = LOAD 'test_201501.txt' using PigStorage('|', '-tagFile') as
              col1: chararray, col2: chararray; 

data_with_yearmonth = FOREACH raw_data GENERATE 
                      SUBSTRING($0,5,11) as yearmonth,
                      'TEST_DATA' as test,
                      col1,
                      col2;

DUMP data_with_yearmonth;

预期输出: 201501, TEST_DATA, col1, col2

电流输出: 201501, TEST_DATA, test_YYYYMM.txt, col1, col2

【问题讨论】:

    标签: apache-pig


    【解决方案1】:

    首先,如果 col1 和 col2 是字符串,那么你应该在 Pig 中将它们声明为 CHARARRAY。 另外,我猜你当前的输出实际上是:201501,TEST_DATA,test_YYYYMM.txt,col1。 如果我错了,请告诉我,但是当您使用“-TagFile”时,第一列是文件标题,这就是为什么您在 SUBSTRING 中使用 $0 访问它的原因。

    你可以试试这段代码:

    raw_data = LOAD 'text_201505.txt' 
               USING PigStorage('|', '-tagFile') 
               AS (title: CHARARRAY, col1: CHARARRAY, col2: CHARARRAY); 
    
    data_with_yearmonth = FOREACH raw_data 
                             GENERATE 
                                 SUBSTRING($0,5,11) AS yearmonth,
                                 'TEST_DATA' AS test,
                                 col1,
                                 col2;
    
     DUMP data_with_yearmonth;
    

    【讨论】:

    • "string" 是一个错字。它应该是chararray。我使用 $0 访问它以从文件名(201501 部分)中提取年份和月份。我已经编辑了我的问题以显示正确的数据类型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多