欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

python連接hive批量補充時間分區特征

系統 1743 0

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元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲视频观看 | 国产你懂得 | 91精品久久久久久久久久 | 小泽玛利亚一区二区 | 国产这里只有精品 | 亚洲图片欧洲电影 | 91在线免费观看 | 欧美线人一区二区三区 | 亚洲午夜无码毛片AV久久 | 久色视频在线观看 | 久久精品一区 | 日韩经典视频 | 一级黄a| 国产91亚洲精品 | 精品久久久久久无码中文字幕 | 亚洲午夜免费 | 精品福利在线视频 | 五月天黄色网址 | 最新日本中文字幕在线观看 | 91精品国产综合久久福利软件 | 新神奇四侠免费完整版在线观看 | 国产麻豆剧传媒精品好看的片 | 激情九月 | 欧美亚洲一区二区三区 | 99视频网 | 久草视频免费看 | 亚洲日日干 | 日韩伦理一区 | 色视频在线免费观看 | 天天躁日日躁狠狠躁中文字幕 | 太平公主一级艳史播放高清 | 五月色丁香综缴合 | 国产传媒在线播放 | 日韩午夜激情视频 | 亚洲欧美成人中文在线网站 | 99热久久是国产免费66 | 在线精品亚洲欧美日韩国产 | 免费国产精品视频在线 | 高清人人天天夜夜曰狠狠狠狠 | 日本精品二区 | 香蕉久草视频 |