如何用日数据构造年数据?

How to used daily data to construct yearly data?

我有一组结构如下的数据:

[uid, product,   currency,  platform,  date]
[100, product_1, USA,       desktop,   2019-01-01]
[100, product_2, USA,       desktop,   2019-01-03]
[200, product_3, CAN,       mobile,    2019-01-02]
[300, product_1, GBP,       desktop,   2019-01-01]
and so on...

必须每年汇总数据:

[year, product,   currency, platform,  uid_count]
[2019, product_1, USA,      desktop,   1000]
[2019, product_2, USA,      desktop,   2000]
[2019, product_3, GBP,      mobile,    5000]

在研究了一个解决方案后,我阅读了草图算法,这似乎是正确的方向。本质上,数据太大,无法批量加载,所以我需要增量处理,例如每天,所以我 not 运行 a SQL 查询如:

SELECT year(date), product, currency, platform, count(distinct uid) FROM tbl_name GROUP BY 1, 2, 3, 4

SELECT year(date), product, currency, platform, count(distinct uid) FROM tbl_name GROUP BY 1, 2, 3, 4
with cube

不幸的是,count(distinct uid) 不是可加的,您需要重新迭代全年数据集,您不能计算不同的一天并将其添加到现有的累计不同年份计数中。因为如果相同的 UID 在许多不同的日子里存在,那么第一天的 count(distinct uid) + 第二天的 count(distinct uid) 不等于这两天计算的 count(distinct uid)。这使得 count(distinct) 不可扩展。

但如果估算适用,您可能可以根据草图算法进行一些近似估算。

可供使用的 Hive 草图算法实现很少。

  1. Hive 的这个 HyperLogLog:HllHiveUDFs Sketches library from Yahoo

  2. Brickhouse sketch UDFs - "K-minimum values" 素描算法。

  3. 又一个实现:https://github.com/MLnick/hive-udf/wiki