【问题标题】:How to remove duplicates on a column basis in Pig如何在 Pig 中按列删除重复项
【发布时间】:2018-04-15 19:25:48
【问题描述】:

谁能帮我从我的 csv 文件中删除旧记录并使用 Pig 保留最近的记录。

EX:输入

Key1 日期

XXXXX P38 17-10-2017

XXXXX P38 12-10-2017

YYYY P38 11-10-2017

YYYY P38 23-09-2017

YYYY P38 14-09-2017

ZZZZZ P38 25-10-2017

ZZZZZ P38 10-10-2017

我的预期输出是

Key1 日期

XXXXX P38 17-10-2017

YYYY P38 11-10-2017

ZZZZZ P38 25-10-2017

并且标题也包含在输出中。

请建议我怎样才能做到这一点?

【问题讨论】:

    标签: hadoop bigdata apache-pig


    【解决方案1】:

    以下内容对你有用。

    a = load 'pig.txt' USING PigStorage(' ') AS (name:chararray,code:chararray,x1:chararray);
    b = FOREACH a GENERATE name,code,ToDate(x1,'dd-mm-yyyy') AS x1;
    grpd = GROUP b BY name;
    firstrecords = FOREACH grpd {
            sorted = order b by x1 desc;
            toprecord    = limit sorted 1;
            generate group,FLATTEN(toprecord);
    };
    dump firstrecords;
    

    【讨论】:

      【解决方案2】:

      这种情况下可以使用嵌套的foreach,

      A = LOAD '....' AS (
      B =
          FOREACH (GROUP A BY key1) {
              orderd = ORDER A BY date DESC;
              ltsrow = LIMIT orderd 1;
              GENERATE FLATTEN(ltsrow);
          };
      STORE B into 'output' using PigStorage('\t', '-schema');
      

      要了解嵌套 foreach,请看这个, https://shrikantbang.wordpress.com/2014/01/14/apache-pig-group-by-nested-foreach-join-example/ https://community.mapr.com/thread/22034-apache-pig-nested-foreach-explaination

      在使用模式保存输出时, https://hadoopified.wordpress.com/2012/04/22/pigstorage-options-schema-and-source-tagging/

      【讨论】:

      • 亲爱的 Suresh,这对我来说非常有用。谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 2019-12-07
      • 1970-01-01
      相关资源
      最近更新 更多