hadoop - 使用 pig latin 选择不同的计数

标签 hadoop apache-pig

我需要有关此 pig 脚本的帮助。我只是得到一个记录。我正在选择 2 列并对另一列进行计数(不同),同时还使用 where like 子句来查找特定描述 (desc)。

这是我正在尝试编写的带有 pig 的 sql。

 /*
    For example in sql:
    select domain, count(distinct(segment)) as segment_cnt
    from table
    where desc='ABC123'
    group by domain
    order by segment_count desc;
    */

    A = LOAD 'myoutputfile' USING PigStorage('\u0005')
            AS (
                domain:chararray,
                segment:chararray,
                desc:chararray
                );
B = filter A by (desc=='ABC123');
C = foreach B generate domain, segment;
D = DISTINCT C;
E = group D all;
F = foreach E generate group, COUNT(D) as segment_cnt;
G = order F by segment_cnt DESC;

最佳答案

您可以对每个域进行 GROUP,然后使用 nested FOREACH 计算每个组中不同元素的数量。语法:

D = group C by domain;
E = foreach D { 
    unique_segments = DISTINCT C.segment;
    generate group, COUNT(unique_segments) as segment_cnt;
};

关于hadoop - 使用 pig latin 选择不同的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9247296/

相关文章:

hadoop - 按当前日期过滤结果-在Hadoop PIG中为2天

hadoop - 使用相同的 Pig 脚本存储多个文件

apache-pig - Apache pig : Merging list of attributes into a single tuple

hadoop - 在 UDF 中加载文件

hadoop - 使用配置单元中的数据测量加载表的时间(可能吗?)

hadoop - 运行Pig时出现错误:无法找到pig-core-h2.jar。做 'ant -Dhadoopversion=23 jar',然后再试一次

hadoop - Apache-PIG 脚本 : ERROR Invalid field projection on joined variable

python - 在 Pig 中使用 Python UDF 时,如何让 Hadoop 找到导入的 Python 模块?

hadoop - org.apache.hadoop.hbase.TableNotFoundException : SYSTEM. 目录异常与凤凰 4.5.2

hadoop - 我可以使用 Hadoop 进行集群分析吗?我可以将 Hadoop 连接到 MongoDB