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. MongoDB 集合(Collection)对应的物理文件

    dbpath下是清一色的collection-n-***与index-n-***开头的物理文件,如何知道某一个集合与其对应与其对应的物理文件? db.collection_name.stats() 返 ...

  2. 基础算法简单实现-python

    目录 Python(版本3.6+)-Anna-Lena Popkes

  3. linux下open-vswitch安装卸载操作

    一. ovs 从源码编译安装: 安装依赖项: ? 1 2 3 4 5 6 7 8 9 10 11 # apt install make # apt install gcc # apt install ...

  4. regex正则表达式学习

    正则表达式 动机1. 处理文本成为计算机常见工作之一2. 对文本内容的搜索提取是一项比较复杂困难的工作3. 为了快速方便处理上述问题,正则表达式技术诞生,主键发展为一个被众多语言使用的独立技术 定义: ...

  5. 项目管理利器maven学习笔记(二):

  6. pycharm 配置svn

    1.找到本地svn.exe 2.配置svn路径 3.输入svn路径,然后会进行密码校验 问题 本地的TortoiseSVN在bin目录没有发现svn.exe 原因: 安装TortoiseSVN的时候 ...

  7. 初步了解Spring

    1.了解“控制反转” 控制反转也称为依赖注入,是面向对象编程中的一种设计理念,用来降低程序代码之间的耦合度. 下面是一个最简单的打印机实例 纸张接口,纸张有大小 继承它的有两个类,两种纸张大小的类 A ...

  8. Ubuntu16.04 藍牙連上,但是聲音裏面找不到設備

    解決辦法: 1. sudo apt-get install blueman bluez* 2. sudo vim /etc/pulse/default.pa 注釋掉下面的代碼: #.ifexists ...

  9. LeetCode至 少有 1 位重复的数字

    给定正整数 N,返回小于等于 N 且具有至少 1 位重复数字的正整数. 示例 1: 输入:20 输出:1 解释:具有至少 1 位重复数字的正数(<= 20)只有 11 . 示例 2: 输入:10 ...

  10. Find out where to contain the smartforms

    Go to table E071 and give smarforms name and it will give the transport req for that. Run SE03, choo ...