Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache pig 使用pig脚本查找类似条目_Apache Pig - Fatal编程技术网

Apache pig 使用pig脚本查找类似条目

Apache pig 使用pig脚本查找类似条目,apache-pig,Apache Pig,我有如下数据 1,ref1,1200,USD,CR 2,ref1,1200,USD,DR 3,ref2,2100,USD,DR 4,ref2,800,USD,CR 5,ref2,700,USD,CR 6,ref2,600,USD,CR 我想将这些记录分组,其中field2匹配,sum(field3)匹配,field5相反(意味着如果lhs是“CR”,那么rhs应该是“DR”,反之亦然) 如何使用pig脚本实现这一点?我不确定是否理解您的要求,但您可以加载数据,将其分为两

我有如下数据

1,ref1,1200,USD,CR  
2,ref1,1200,USD,DR  
3,ref2,2100,USD,DR  
4,ref2,800,USD,CR  
5,ref2,700,USD,CR  
6,ref2,600,USD,CR  
我想将这些记录分组,其中field2匹配,sum(field3)匹配,field5相反(意味着如果lhs是“CR”,那么rhs应该是“DR”,反之亦然)


如何使用pig脚本实现这一点?

我不确定是否理解您的要求,但您可以加载数据,将其分为两组(筛选/拆分)和cogroup,例如:

data = load ... as (field1: int, field2: chararray, field3: int, field4: chararray, field5: chararray);

crs= filter data by field5='CR';
crs_grp = group crs by field1;
crs_agg = foreach crs_grp generate group.field1 as field1, sum(crs.field3);
drs = filter data by field5='DR';
drs_grp = group drs by field1;
drs_agg = foreach drs_grp generate group.field1 as field1, sum(drs.field3);
g = COGROUP crs_agg BY (field1, field3), drs_agg BY (field1, field3);

您也可以这样做:

data = LOAD 'myData' USING PigStorage(',') AS
       (field1: int, field2: chararray,
        field3: int, field4: chararray,
        field5: chararray) ;

B = FOREACH (GROUP data BY (field2, field5)) GENERATE group.field2, data ;

-- Since in B there will always be two sets of field2 (one for CR and one for DR)
-- grouping by field2 again will get all pairs of CR and DR
-- (where the sums are equal of course)
C = GROUP B BY (field2, SUM(field3)) ; 
最后一步的架构和输出:

C: {group: (field2: chararray,long),B: {(field2: chararray,data: {(field1: int,field2: chararray,field3: int,field4: chararray,field5: chararray)})}}
((ref1,1200),{(ref1,{(1,ref1,1200,USD,CR)}),(ref1,{(2,ref1,1200,USD,DR)})})
((ref2,2100),{(ref2,{(4,ref2,800,USD,CR),(5,ref2,700,USD,CR),(6,ref2,600,USD,CR)}),(ref2,{(3,ref2,2100,USD,DR)})})
输出put现在有点笨拙,但这将清除它:

 -- Make sure to look at the schema for C above
 D = FOREACH C { 
        -- B is a bag containing tuples in the form: B: {(field2, data)}  
        -- What we want is to just extract out the data field and nothing else
        -- so we can go over each tuple in the bag and pull out
        -- the second element (the data we want).                     
        justbag = FOREACH B GENERATE FLATTEN($1) ;  
        -- Without FLATTEN the schema for justbag would be:
        -- justbag: {(data: (field1, ...))}  
        -- FLATTEN makes it easier to access the fields by removing data:
        -- justbag: {(field1, ...)}
        GENERATE justbag ;                                                  
 } 
为此:

D: {justbag: {(data::field1: int,data::field2: chararray,data::field3: int,data::field4: chararray,data::field5: chararray)}}
({(1,ref1,1200,USD,CR),(2,ref1,1200,USD,DR)})
({(4,ref2,800,USD,CR),(5,ref2,700,USD,CR),(6,ref2,600,USD,CR),(3,ref2,2100,USD,DR)})

谢谢Neumann。是的,这是一个起点。在这之后,我需要总结字段3,它属于同一个字段2,但在字段5中有相反的指示器。然后将所有这些记录组合在一起,其中总和相等。事实上,我需要的结果是-将记录1和2组合为一个集合,将记录3、4、5和6组合为另一个集合。好的,我想我明白了-所以你需要过滤,然后组合并计算总和,然后将cogroup,见答案,格式化为代码。你能给出一个求和字段3的示例吗?另外,我假设最后一组是成对的(因为字段5必须是相反的)。谢谢。我会试试这个,让你知道这是否能达到预期的效果。是的,它起作用了。这正是我想要的,非常感谢。但请你澄清一下你计算“D”的部分好吗。它到底是做什么的?对一个包进行FOREACH创建一个新包,并且新包的数据格式使其更易于使用。有关更多详细信息,请参阅我添加到代码中的注释。