2017-02-20 621 views
0

我正在閱讀我公司的另一個團隊的一些Hive腳本,並且無法理解其特定部分。有問題的部分是:where dt='${product_dt}',它可以在下面的代碼塊底部的第三行中找到。Hive語法:花括號和美元符號的用途

我從來沒有見過這種語法,我也無法通過谷歌搜索找到任何東西(可能是因爲我不知道使用正確的搜索條件)。任何洞察什麼where行過濾器步驟正在做什麼,將不勝感激。

set hive.security.authorization.enabled=false; 
add jar /opt/mobiletl/prod_workflow_dir/lib/hiveudf_hash.jar; 
create temporary function hash_string as 'HashString'; 

drop table 00_truthset_product_email_uid_pid; 
create table 00_truthset_product_email_uid_pid as 
select distinct email,   
     concat_ws('|', hash_string(lower(email), "SHA-1"), 
         hash_string(lower(email), "MD5"), 
         hash_string(upper(email), "SHA-1"), 
         hash_string(upper(email), "MD5")) as hashed_email, 
     uid, address_id, confidencescore 
from product.prod_vintages 
where dt='${product_dt}' 
     and email is not null and email != '' 
     and address_id is not null and address_id != ''; 

我試過set product_dt = 2014-12;,但似乎並沒有工作:

hive> SELECT dt FROM enabilink.prod_vintages GROUP BY dt LIMIT 10; 
. . . 
dt 
2014-12 
2015-01 
2015-02 
2015-03 
2015-05 
2015-07 
2015-10 
2016-01 
2016-02 
2016-03 

hive> set product_dt = 2014-12; 

hive> SELECT email FROM product.prod_vintages WHERE dt='${product_dt}'; 
. . . 
Total MapReduce CPU Time Spent: 2 seconds 570 msec 
OK 
email 
Time taken: 25.801 seconds 
+0

'$ {product_dt}'是腳本中使用的變量。您可以使用! echo $ {product_dt}以打印其值。嘗試使用TO_DATE('date')進行比較。 –

+0

@AshishSingh好的,我做'show tables'並看一張表:'product_20161029'。我嘗試:'set prod_dt = 20161029'然後'描述產品_ $ {prod_dt}',但什麼也沒有。我嘗試'! echo $ {prod_dt}'並獲得'$ {prod_dt}'。 – user2205916

+0

嘗試使用'dt =(from_unixtime(unix_timestamp($ {product_dt},'yyyyMMdd'),'yyyy -MM'))'作爲比較 –

回答

1

那些在蜂巢設置變量。如果您在查詢之前設定的變量(在同一個會話),蜂巢將與指定的值

例如更換

set product_dt=03-11-2012 

編輯 確保你在你的DT刪除空格字段(使用修剪UDF)。另外,設置不帶空格的變量。

+0

我試過了(在更新後的文章中顯示),但查詢結果變爲空,儘管肯定有滿足'product_dt'要求的行。難道我做錯了什麼? – user2205916

+0

你可以嘗試設置product_dt = 2014-12;我不確定Hive是否會刪除開始時的空間或者在兩側應用修剪版本時,它應該可以正常工作:p – hlagos

+0

沒有工作。我嘗試了很多排列,其中包括之前或之後沒有空格的排列。 – user2205916