【问题标题】:How to create a key value map when there are duplicate keys?存在重复键时如何创建键值映射?
【发布时间】:2017-05-19 12:40:08
【问题描述】:

我是猪新手。我有以下输出。

(001,Kumar,Jayasuriya,1123456754,Matara)
(001,Kumar,Sangakkara,112722892,Kandy)
(001,Rajiv,Reddy,9848022337,Hyderabad)
(002,siddarth,Battacharya,9848022338,Kolkata)
(003,Rajesh,Khanna,9848022339,Delhi)
(004,Preethi,Agarwal,9848022330,Pune)
(005,Trupthi,Mohanthy,9848022336,Bhuwaneshwar)
(006,Archana,Mishra,9848022335,Chennai)
(007,Kumar,Dharmasena,758922419,Colombo)
(008,Mahela,Jayawerdana,765557103,Colombo)

如何创建上述地图,以便输出看起来像,

001#{(Kumar,Jayasuriya,1123456754,Matara),(Kumar,Sangakkara,112722892,Kandy),(001,Rajiv,Reddy,9848022337,Hyderabad)}
002#{(siddarth,Battacharya,9848022338,Kolkata)}

我尝试了 ToMap 功能。

 mapped_students = FOREACH students GENERATE TOMAP($0,$1..);

但我无法转储上述命令的输出,因为该过程会引发错误并停在那里。任何帮助将不胜感激。

【问题讨论】:

    标签: mapreduce bigdata apache-pig


    【解决方案1】:

    我认为您正在尝试实现的是将记录分组到具有相同 id 的元组中。

    根据 TOMAP 函数,它将键/值表达式对转换为映射,因此您将无法对其余记录进行分组,并会导致无法打开别名的迭代器。

    根据您想要的输出,这里是一段代码。

    A = LOAD 'path_to_data/data.txt' USING PigStorage(',') AS (id:chararray,first:chararray,last:chararray,phone:chararray,city:chararray);
    

    如果你不想给出架构,那么:

    A = LOAD 'path_to_data/data.txt' USING PigStorage(',');
    
    B = GROUP A BY $0; (this relation will group all your records based on your first column)
    
    DESCRIBE B; (this will show your described schema)
    
    DUMP B;
    

    希望这会有所帮助..

    【讨论】:

    • 问题是,我不知道“A”中列的名称,如您所指出的。该脚本应该能够处理具有任意列数的任何表。你能想出一个办法吗?
    • 您好,感谢您的回复。但是,当您按原样转储 B 时,您不会得到我需要的输出。因为现在您将在组和值列中都有键。也就是说,{001. {001,Kumar,Jayasuritha...}}。其中 id 将在输出中出现两次。我需要它,{001, {Kumar,Jayasuriya..}}
    猜你喜欢
    • 1970-01-01
    • 2020-07-31
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多