python hive.py
#!/usr/bin/env python
# -- coding:utf-8 --
import os
import sys
from subprocess import call
from pyspark import SparkContext, SparkConf
from pyspark.sql import SparkSession
#master = spark://spark:7077
master = os.environ.get("SPARK_MASTER_URL")
spark = SparkSession.builder \
.master(master) \
.appName("hive") \
.enableHiveSupport() \
.getOrCreate()
TIMESTAMP_COLUMNS = ['created', 'date', 'create', 'time', 'launchDate']
def refresh_model(model):
df = spark.sql('select * from {model}'.format(model=model))
df.show()
first = df.first() or []
time_columns = filter(lambda key: key in first, TIMESTAMP_COLUMNS)
partition_column = None
if time_columns:
partition_column = time_columns[0]
if 'id' in first:
partition_column = 'id'
if not time_columns:
return
spark.sql('drop table if exists {model}'.format(model=model))
df.repartition(time_columns[0]).write.saveAsTable(model)
def run(filePath):
filePath = os.path.join(os.getcwd(), filePath)
executor = None
if 'postsql' in filePath:
executor = '/data/spark-2.2.0-bin-hadoop2.7/bin/spark-sql'
else:
executor = '/data/apache-hive-2.1.1-bin/bin/hive'
call("{} -f {}".format(filePath, executor),shell=True)
model = os.path.splitext(os.path.basename(filePath))[0]
if executor == 'hive':
print('model', model)
refresh_model(model)
if __name__ == '__main__':
if len(sys.argv) == 2:
run(sys.argv[1])
else:
valid_dirs = ['sql', 'postsql']
for dir in valid_dirs:
for dirpath,dirnames,filenames in os.walk(dir):
for filename in filenames:
run(os.path.join(dirpath,filename))
主要理解os.path.join()、os.walk()、os.getcwd()几个方法的用法,进行路径拼接。
注意一个地方的写法:
call("{} -f {}".format(filePath, executor),shell=True)
当然也可以写成subprocess.call("{} -f {}".format(filePath, executor),shell=True)
shell=True是后加上的,如果没有shell=True,call("{} -f {}".format(filePath, executor))使用pipeline创建任务执行是会报错。
pipeline {call("{} -f {}".format(filePath, executor),shell=True)
agent {label 'spark' }
stages {
stage('hive sql'){
steps{
dir('/data/sftp/huoqiu/script'){
sh 'python hive.py'
}
}
}
}
}
执行后就会报下面的错:
Traceback (most recent call last):
File "./marp.py", line 82, in <module>
programs = [ subprocess.Popen(c) for c in commands ]
File "/usr/lib/python2.6/subprocess.py", line 595, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1092, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
解决放方法就是:
在最后加上shell=True,就不会报错,能够正确执行。
python hive.py的更多相关文章
- python调用py中rar的路径问题。
1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...
- python gettitle.py
#!/usr/bin/env python # coding=utf-8 import threading import requests import Queue import sys import ...
- Python pydoc.py
1. 查看帮助,我们可以在python命令行交互环境下用 help函数,比如: 查看 math 模块: >>> help('math')Help on built-in module ...
- django 1.7之后python manage.py syncdb没有了
在命令行输入python manage.py createsuperuser按照提示输入即可记得先初始化表. django>1.7 python manage.py makemigrations ...
- Python安装mysql-python错误提示python setup.py egg_info
做python项目,需要用到mysql,一般用python-mysql,安装时遇到错误提示如下: Command "python setup.py egg_info" failed ...
- python __init__.py用途
转自http://www.cnpythoner.com/post/2.html Python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文 件,在另一个文件中需要import时, ...
- python setup.py uninstall
I have installed a python package with python setup.py install How do I uninstall it? ============== ...
- python 运行python manege.py runserver时报错:“no module named djangorestframework” 的解决方案
python 运行python manege.py runserver时报错:“no module named djangorestframework” 的解决方案 importerror:no mo ...
- Python Web.py
安装Web.py root@bt:~# sudo pip install web.py Downloading/unpacking web.py Downloading web.py-0.37.tar ...
随机推荐
- matlab的特殊字符(上下标和希腊字母等)
'T=25\circC',(摄氏度) 下标用 _(下划线) 上标用^ (尖号) 希腊字母等特殊字符用 α \alpha β \beta γ \gamma θ \theta Θ \Theta Г \Ga ...
- Nodejs书写爬虫工具
看了几天的nodejs,的确是好用,全当是练手了,就写了一个爬虫工具. 爬虫思路都是一致的,先抓取页面数据,然后分析页面,获取到所需要的数据,最后获得这些数据,是写入到硬盘,还是显示到网页,自己看着办 ...
- IDEA13 项目配置
之前用了一段时间的idea,有些老的代码,用eclipse跑了一下,比较麻烦,于是试用一下idea,最后,项目可以顺利跑起来. 对项目的配置,主要是在F4中,即:Module Setting,在模块的 ...
- 微信Web APP应用
微信Web APP即微信公众账号,对web APP的提供者来说这是一个门槛极低,容易到达数亿真实用户且确保用户黏性的分发平台;对用户来说,这是一种前所未有及其简单的应用使用方式;对腾讯来 说,将形成微 ...
- Windows2012 显示我的电脑删除群集
rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0 在正常删除Cluster 节点之后,再添加节点时,报“节点已经加入群集”,无法加入,注册表信息删 ...
- 记一次IIS站点出错的解决过程
记一次IIS站点出错的解决过程 以前一直都是人家用着系统出问题了反馈过来这边改,没想到这回就发生在自己使用的过程中 问题经过 我正在执行一个操作,保存了没有返回提示,打开浏览器控制台查看网络,请求返回 ...
- 【mysql】Windows环境搭建(适用5.7以上)
1 下载MySQL 登录 https://dev.mysql.com/downloads/mysql/ 2 配置 下载好了zip文件,解压至任意非中文目录,在根目录下新建my.ini: 输入以下内容( ...
- c# 调用短信平台接口,给手机发送短信
项目上要做个发手机短信的功能.网上找找了,用的微米的短信接口. 注册后,获得UID和UID key,C#代码中需要这个 调用代码很简单 ", con = "[微米]您的验证码是:6 ...
- nginx安装和遇到的问题
nginx安装步骤和遇到的问题 tar -xvf nginx-.tar.gz cd nginx- ./configrue make make install 在configure中可能遇到的问题: ( ...
- 点分治&&动态点分治学习笔记
突然发现网上关于点分和动态点分的教程好像很少……蒟蒻开篇blog记录一下吧……因为这是个大傻逼,可能有很多地方写错,欢迎在下面提出 参考文献:https://www.cnblogs.com/LadyL ...