用PIG计算不同字符串的数量

时间:2017-05-25 07:44:03

标签: apache-pig

我是PIG的新手并尝试计算以下数据集中不同国家/地区的数量(您可以从this link下载):

Athlete Country Year    Sport   Gold    Silver  Bronze  Total
Yang Yilin  China   2008    Gymnastics  1   0   2   3
Leisel Jones    Australia   2000    Swimming    0   2   0   2
Go Gi-Hyeon South Korea 2002    Short-Track Speed Skating   1   1   0   2
Chen Ruolin China   2008    Diving  2   0   0   2
Katie Ledecky   United States   2012    Swimming    1   0   0   1
Ruta Meilutyte  Lithuania   2012    Swimming    1   0   0   1

到目前为止我尝试了什么:

athletes = LOAD '/data/OlympicAthletes.csv' USING org.apache.pig.piggybank.storage.CSVExcelStorage(',', 'YES_MULTILINE', 'NOCHANGE', 'SKIP_INPUT_HEADER') AS (athlete:chararray, country:chararray, year:int, sport:chararray, gold:int, silver:int, bronze:int, total:int);
distinct_countries= distinct (foreach athletes generate country);
country_count_try1 = COUNT(distinct_countries);
country_count_try2 = FOREACH distinct_countries GENERATE COUNT(country);
country_count_try3 = FOREACH (GROUP athletes country) GENERATE count(athletes.country) as total_country;

1 个答案:

答案 0 :(得分:1)

您需要将整个数据集分组才能计算。

distinct_countries= distinct (foreach athletes generate country);
country_count_try4 = foreach (group distinct_countries all) generate COUNT(distinct_countries) as cnt;