gpt4 book ai didi

hadoop - 使用 pig 从键值对中获取 n 个值

转载 作者:可可西里 更新时间:2023-11-01 16:33:39 25 4
gpt4 key购买 nike

我有一个测试文件,其中的键和值由逗号分隔。我怎样才能使用 pig 脚本为每个键获取 10 个值。

示例输入:john|str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str2,首选输出:john|str1,str2,str3,str4,str5,str6,str7,str8,str9,str10

最佳答案

有很多不同的方法可以做到这一点,具体取决于您拥有的输入和需要的输出。我假设您只需要前十个值,而可以丢弃其余值。

这就是我的做法 (CL)。它比短路径 (CF) 稍长,但代码对我来说更清晰,并且允许更灵活地命名模式:

A = LOAD 'myData' USING PigStorage('|') AS (name: chararray, vals: chararray) ;  
B = FOREACH A GENERATE name, STRSPLIT(vals, ',') AS svals: () ;
CL = FOREACH B GENERATE name,
svals.($0, $1, $2, $3, $4, $5, $6, $7, $8, $9) AS ten ;
-- ten can have a schema, like ten: (a1: chararray, etc.)
-- After giving it a schema, you can also flatten it to
-- make it like the output of CF, but with better types

这是 CL 的结果模式和输出:

CL: {name: chararray,ten: ()}
(john,(str1,str2,str3,str4,str5,str6,str7,str8,str9,str10))

这种方式稍微短一些,但更难将模式应用于值:

-- Uses the same A  
B = FOREACH A GENERATE name AS name, FLATTEN(STRSPLIT(vals, ',')) ;
CF = FOREACH B GENERATE $0 AS name: chararray, $1, $2 .. $10 ;

CF 的架构和输出:

CF: {name: chararray,bytearray,bytearray,bytearray,bytearray,bytearray,bytearray,bytearray,bytearray,bytearray,bytearray}
(john,str1,str2,str3,str4,str5,str6,str7,str8,str9,str10)

关于hadoop - 使用 pig 从键值对中获取 n 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17487090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com