python执行hivesql_python 调用shell hive sql

该博客介绍如何使用Python脚本调用shell命令执行Hive SQL,包括从Hive表中导出数据,替换字段分隔符,以及将处理后的数据加载回Hive表。示例代码展示了如何利用`subprocess`模块执行Hive命令。

def generate_csv_source(data_file):

#判断文件是否存在

if not os.path.exists(data_file):

# 拉取hive表数据

cmd_sql = 'hive -e "set hive.cli.print.header=true; \

select * from dw.full_d_usr_channel_sum_v3 where ds<>\'\' \

and type in (\'super\',\'9k9\',\'taobao\',\'sc\',\'zy\',\'ad\',\'licai\') \

" >%s' % (data_file)

print cmd_sql

subprocess.call(cmd_sql, shell=True)

# 替换其中的字段分隔符/t为,

cmd_sed = 'sed -i "s/\t/,/g" %s' % (data_file)

subprocess.call(cmd_sed, shell=True)

print "文件已生成:"+data_file

else:

print "最新文件已存在:"+data_file

-- 写人hive 表

def insert_to_table(data_cur, c_path,tab_name, ds,freq_type,c_type):

# data_cur.to_csv('./user_value_auto_compute_result.csv', index=False, header=None, encoding="utf8")

path_result = '%s/%s_%s_%s.csv' % (c_path,tab_name,freq_type,c_type)

print '生成的结果csv文件:',path_result</

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值