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
data_cur.to_csv(path_result, index=False,header=None, float_format = '%.4f', encoding="utf8")
# cmd_hive = '''hive -e "load data local inpath \'''' + c_path + '''/'''+tab_name+'''.csv\' OVERWRITE into table '''+tab_name+''' partition(c_type='%s',freq_type='%s',ds='%s')"''' % (c_type,freq_type,ds)
cmd_hive = '''hive -e "load data local inpath \'''' + path_result+'''\' OVERWRITE into table '''+tab_name+''' partition(c_type='%s',freq_type='%s',ds='%s')"''' % (c_type,freq_type,ds)
# 导入hive库
print cmd_hive
subprocess.call(cmd_hive, shell=True)
print '生成文件:%s.csv' % (tab_name)

python 调用shell hive sql的更多相关文章

  1. python 调用 shell 命令方法

    python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等   ...

  2. python调用shell, shell 引用python

    python 调用 shell get_line_num="wc -l as_uniq_info | awk '{print $1}'" ###get the lines of & ...

  3. python 调用shell命令三种方法

    #!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: #!/usr/bin/env python这种用法是为了防止操作系统用户没有将pyth ...

  4. python 调用 shell 命令

    记录 python 调用 shell 命令的方法 加载 os 模块, 使用 os 类 import os; os.system("ls /");

  5. Python 调用 Shell脚本的方法

    Python 调用 Shell脚本的方法 1.os模块的popen方法 通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出. > ...

  6. 用Python调用Shell命令

    Python经常被称作“胶水语言”,因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库,也当然可以用Python调用Shell命令. 用Python调用Shell命令有如下几种方式: 第一种 ...

  7. Python调用shell命令常用方法

    Python调用shell指令 方法一.使用os模块的system方法:os.system(cmd),其返回值是shell指令运行后返回的状态码,int类型,0表示shell指令成功执行,256表示未 ...

  8. python 调用shell命令的方法

    在python程序中调用shell命令,是件很酷且常用的事情…… 1. os.system(command) 此函数会启动子进程,在子进程中执行command,并返回command命令执行完毕后的退出 ...

  9. python调用shell脚本时需要切换目录

    最近遇到了一个问题,就是python代码调用shell脚本时,发现输入输出的文件,总是和自己预想的有偏差,但是单独在linux下执行命令的时候,却没有错误.后来发现是相对路径的问题,因为执行pytho ...

随机推荐

  1. ThreeJs 绘制点、线、面

    所有的三位物体都是由点构成,两点构成线,三点构成面,ThreeJs又如何绘制出点.线.面呢 ? 在ThreeJs中: 模型由几何体和材质构成 模型以何种形式(点.线.面)展示取决于渲染方式 1. 几何 ...

  2. Docker镜像操作命令

    一.使用Docker镜像 1. 获取镜像 可以使用docker pull命令直接从Docker Hub镜像源来下载镜像.该命令的格式为docker pull NAME[:TAG].其中,NAME是镜像 ...

  3. 小白安装openvas总结-原创20181213

    先对该工具进行熟悉: OpenVAS 介绍 1.关于OpenVAS OpenVAS(Open Vulnerability Assessment System)是一套开源的漏洞扫描系统,早期Nessus ...

  4. Linux安装Intel Threading Building Blocks(TBB)

    编译安装: wget https://codeload.github.com/01org/tbb/tar.gz/2019_U3 tar zxvf 2019_U3 cd tbb-2019_U3 make ...

  5. angualrjs添加ngTouch

    angularjs没有touch时间需要添加directive 插件代码如下 "use strict"; angular.module("ngTouch", [ ...

  6. linux 之 shell

    echo 输出命令 语法:echo [选项][输出内容] 选项 -e:支持反斜线控制的字符转换 范例 vi hello.sh #!/bin/bash #author liuch echo " ...

  7. 一个简单的struts2上传图片的例子

    https://www.cnblogs.com/yeqrblog/p/4398914.html 在我的大创项目中有对应的应用

  8. SpringCloud-day06-Ribbon负载均衡

    6.3.Ribbon负载均衡 为了实现真正的负载均衡,我们需要集群3个服务提供给者,而在这之前我们只有一个服务提供者1001,那么我们新建模块microservice-station-provider ...

  9. IoU

    IoU #include <cstdio> #include <algorithm> #define re(i,a,b) for(int i=a;i<=b;i++) us ...

  10. pl/sql报错快速解决方法(新手推荐)

    一:简介 今天登录数据库时莫名报了个奇怪的错:ORA-01033: ORACLE initialization or shutdown in progress可能是昨天匆忙电脑没正常关机导致的... ...