1. python連接數據庫
from pyhive import hive
conn = hive.Connection(
host= 'xxx xxx',
port= xxx,
auth='CUSTOM',
username='your user name',
password='your password',
database='default')
cursor = conn.cursor()
2. SQL代碼封裝
設計為可傳參方式
def get_sql(d):
d = "'"+ d + "'"
add_sql_horizon = """
CREATE table tmp.horizon_feature as
with
tmp_dlr_cstm as(
select dealer_id,
sum(
case when DateDiff("""+d+""",created_time)<=30 and date_from not in (0,3) then 1
else 0
end
) as pcust_acc_1m_dlr_off
from dcs.nt_tp_p_customers
group by dealer_id
)
select
nvl(cast(d1.dealer_id as string),'') as dealer_id,
nvl(cast(d1.dealer_code as string),'') as dealer_code,
nvl(cast(d1.company_id as string),'') as company_id,
nvl(cast(tmp_dlr_cstm.pcust_acc_1m_dlr_off as string),'') as pcust_acc_1m_dlr_off
from dcs.nt_BB_DEALERS d1
left join tmp_dlr_cstm on d1.dealer_id = tmp_dlr_cstm.dealer_id
where d1.biz_status=10310005"""
add_sql_verticle ="""
insert into table tmp.dwb_dealer_feature_dd
select dealer_id,dealer_code,company_id,t.feat_name,t.feat_value,"""+d+""" as prod_time
from tmp.horizon_feature
lateral view explode(
map(
'pcust_acc_1m_dlr_off', pcust_acc_1m_dlr_off
)
)t as feat_name, feat_value
"""
return add_sql_horizon,add_sql_verticle
以上操作完成了新建一個橫表,然后轉化為縱表的操作。
3. 獲取日期參數
import datetime
def getEveryDay(begin_date,end_date):
date_list = []
begin_date = datetime.datetime.strptime(begin_date, "%Y-%m-%d")
end_date = datetime.datetime.strptime(end_date,"%Y-%m-%d")
while begin_date <= end_date:
date_str = begin_date.strftime("%Y-%m-%d")
date_list.append(date_str)
begin_date += datetime.timedelta(days=1)
return date_list
4. 循環執行SQL腳本
for d in getEveryDay('2017-01-01','2019-08-07'):
cursor.execute("""drop table if exists tmp.horizon_feature""")
conn.commit()
horizon_sql,verticle_sql = get_sql(d)
cursor.execute(horizon_sql)
conn.commit()
cursor = conn.cursor()
cursor.execute(verticle_sql)
conn.commit()
cursor = conn.cursor()
cursor.close()
成功插入數據!
棒!
歡迎關注微信公眾號“數據分析師手記”,一起進步!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
