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 str