2013-03-04 83 views
0

我正在將統計數據從MySQL切換到Amazon DynamoDB和Elastic MapReduce。Hive多個子查詢和組由

我有查詢波紋管與MySQL的工作,我有蜂巢上的同一個表,並需要在MySQL(last_week,last_month和last_year的產品視圖)相同的結果。

SELECT product_id, 
SELECT COUNT(product_id) from dev_product_views_hive as P2 where P2.product_id=P.product_id and created >= DATE_SUB(NOW(), INTERVAL 1 WEEK) as weekly, 
SELECT count(product_id) from dev_product_views_hive as P3 where P3.product_id=P.product_id and created >= DATE_SUB(NOW(), INTERVAL 1 MONTH) as monthly, 
SELECT count(product_id) from dev_product_views_hive as P4 where P4.product_id=P.product_id and created >= DATE_SUB(NOW(), INTERVAL 1 YEAR) as yearly 
from dev_product_views_hive as P group by product_id; 

我想通了,如何獲得具有蜂巢例如結果上個月:

SELECT product_id, COUNT(product_id) as views from dev_product_views_hive WHERE created >= UNIX_TIMESTAMP(CONCAT(DATE_SUB(FROM_UNIXTIME(UNIX_TIMESTAMP()), 31)," ","00:00:00")) GROUP BY product_id; 

,但我需要分組結果就像我得到與MySQL:

product_id views_last_week views_last_month views_last_year 
2     564    2460   29967 
4     980    3986   54982 

是否有可能與蜂巢做到這一點?

謝謝你在前進,

阿米爾

+0

你試過了嗎? – 2013-03-04 12:39:26

+0

是的,總是得到解析錯誤,我不知道如何在hiveql中正確編寫這個MySQL子查詢。 Hiveql不像MySQL那樣的語法。 – trkich 2013-03-04 14:07:25

+0

hive ql僅支持子句 – 2013-03-04 17:25:16

回答

1

您可以case whensum()count()

例如,做到這一點。

select product_id, 
sum(case when created >= concat(date_sub(to_date(from_unixtime(unix_timestamp())), 7)," 00:00:00") then 1 else 0 end) as weekly, 
sum(case when created >= concat(date_sub(to_date(from_unixtime(unix_timestamp())), 31)," 00:00:00") then 1 else 0 end) as monthly, 
sum(case when created >= concat(date_sub(to_date(from_unixtime(unix_timestamp())), 365)," 00:00:00") then 1 else 0 end) as yearly 
from dev_product_views_hive 
group by product_id; 

concat(date_sub(to_date(from_unixtime(unix_timestamp())), days)," 00:00:00")將返回當前時間過去的格式化字符串。創建>=了您的預期

您也可以與蜂巢內置函數做count()只計算那些行返回非NULL

天時

case when將reterun 1