Pig:如何计算 Group By 中多个变量的分位数?
Pig: how to compute quantiles for several variables in a Group By?
我正在使用 Apache DataFu
(http://datafu.incubator.apache.org/docs/datafu/1.3.0/datafu/pig/stats/Quantile.html)
计算数据中多个变量的分位数。
我的理解是在调用quantile
之前需要对数据进行排序。
但是,如果我需要为同一 GROUP BY 序列中的多个变量计算分位数怎么办?鉴于分位数变量的创建发生在 GENERATE 之后,因此只会考虑一个排序(最后一个),如这个简单示例所示:
-- input: 9,10,2,3,5,8,1,4,6,7
input = LOAD 'input' AS (val:int);
grouped = GROUP input ALL;
-- produces: (1,5.5,10)
quantiles = FOREACH grouped {
sorted = ORDER input BY val;
GENERATE Quantile(sorted);
}
适用于一个变量,但如果我想要每个 groupby 中两个不同变量的分位数,我该如何修改此代码?
比如说,如果数据看起来像
group col1 col2
A 1 2
A 3 1
B 1 0
B 9 -2
对于每个组,我想要 col1 和 col2 的分位数?
您可以在嵌套的 FOREACH
中包含多个 ORDER
语句,例如:
x = FOREACH grouped {
sorted_by_col1 = ORDER input BY col1;
sorted_by_col2 = ORDER input by col2;
GENERATE
group,
Quantile(sorted_by_col1.col1),
Quantile(sorted_by_col2.col2);
}
我正在使用 Apache DataFu (http://datafu.incubator.apache.org/docs/datafu/1.3.0/datafu/pig/stats/Quantile.html)
计算数据中多个变量的分位数。
我的理解是在调用quantile
之前需要对数据进行排序。
但是,如果我需要为同一 GROUP BY 序列中的多个变量计算分位数怎么办?鉴于分位数变量的创建发生在 GENERATE 之后,因此只会考虑一个排序(最后一个),如这个简单示例所示:
-- input: 9,10,2,3,5,8,1,4,6,7
input = LOAD 'input' AS (val:int);
grouped = GROUP input ALL;
-- produces: (1,5.5,10)
quantiles = FOREACH grouped {
sorted = ORDER input BY val;
GENERATE Quantile(sorted);
}
适用于一个变量,但如果我想要每个 groupby 中两个不同变量的分位数,我该如何修改此代码?
比如说,如果数据看起来像
group col1 col2
A 1 2
A 3 1
B 1 0
B 9 -2
对于每个组,我想要 col1 和 col2 的分位数?
您可以在嵌套的 FOREACH
中包含多个 ORDER
语句,例如:
x = FOREACH grouped {
sorted_by_col1 = ORDER input BY col1;
sorted_by_col2 = ORDER input by col2;
GENERATE
group,
Quantile(sorted_by_col1.col1),
Quantile(sorted_by_col2.col2);
}