清管器:分组依据、平均值和排序依据

b0zn9rqh  于 2021-06-25  发布在  Pig
关注(0)|答案(1)|浏览(406)

我是新来的Pig,我有一个文本文件,其中每一行包含一个不同的信息记录,格式如下:

name, year, count, uniquecount

例如:

Zverkov winced_VERB 2004    8   8
Zverkov winced_VERB 2008    4   4
Zverkov winced_VERB 2009    1   1
zvlastni _ADV_  1913    1   1
zvlastni _ADV_  1928    2   2
zvlastni _ADV_  1929    3   2

我想按唯一名称对所有记录进行分组,然后为每个唯一名称计算count/uniquecount,最后按此计算值对输出进行排序。
以下是我一直在尝试的:

bigrams = LOAD 'input/bigram/zv.gz' AS (bigram:chararray, year:int, count:float, books:float);
group_bigrams = GROUP bigrams BY bigram;
average_bigrams = FOREACH group_bigrams GENERATE group, SUM(bigrams.count) / SUM(bigrams.books) AS average;
sorted_bigrams = ORDER average_bigrams BY average;
7qhs6swi

7qhs6swi1#

似乎我的原始代码确实产生了所需的输出,只做了一个小改动:

bigrams = LOAD 'input/bigram/zv.gz' AS (bigram:chararray, year:int, count:float, books:float);
group_bigrams = GROUP bigrams BY bigram;
average_bigrams = FOREACH group_bigrams GENERATE group, SUM(bigrams.count)/SUM(bigrams.books) AS average;
sorted_bigrams = ORDER average_bigrams BY average DESC, group ASC;

相关问题