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 ...
随机推荐
- static关键字(二)作用总结
静态变量和静态方法 static关键字最基本的用法是: 1.被static修饰的变量属于类变量,可以通过类名.变量名直接引用,而不需要new出一个类来 2.被static修饰的方法属于类方法,可以通过 ...
- [smarty] 在smarty模板中使用smarty变量初始化 javascript 变量的问题
// 总结:// 1/ 在smarty 模板文件中,使用从php中assign过来的smarty变量,一定需要使用双引号或单引号来括住smarty变量,如:var title="<!- ...
- How To Use XDOLoader to Manage, Download and Upload Files? (文档 ID 469585.1)
Applies to: BI Publisher (formerly XML Publisher) - Version 5.6.3 to 5.6.3 [Release 5] Information ...
- Win10下安装msi程序包时报2503、2502错误问题及其解决办法
Win10系统下安装TortoiseSvn.Node.js时(.msi后缀的安装文件),在点击安装时老是提示2503,2502错误,因此无法安装上. 搜索了下一般都提到是权限不够引起的该问题.但是右键 ...
- abp + angular $http + webapi 服务
什么是angular $http服务 http是angularjs的一个核心服务,用于读取远程服务器的数据,也就是封装了浏览器原生的xhtmlrequest对象,可以直接同外部进行通信. 怎样使用an ...
- wpf(使用定时器)使用定时器操作UI界面
在项目实践中,我们 可能会遇到需要将一些控件上显示的内容只显示一段时间过后清空. 下面我们来实现这种操作: 首先需要注意的是:在wpf中涉及到界面操作的话,一定要使用定时器DispatcherTime ...
- 解决:无法将文件“obj\x86\Debug\Windows123.exe”复制到“bin\Debug\Windows123.exe”。
警告 加载属性“OutputPath”失败. 输入的路径不是有效的输出路径. 解决方案: 先项目打包备份一下哦,再执行以下操作哦. 1.右键项目属性, 发布,发布位置修改成 publish\ 生成 , ...
- 请教如何用ASP.NET实现http://abc.com/orderID这样的URL???
我查看了一下微信二维码的内容是:https://u.wechat.com/XXXXXXXXX这种格式. 我现在想把我们的订单URL也做成 http://abc.com/orderID这样子,做成二维码 ...
- R语言和RStudio的一些用法,常用命令等
控制台: Up/down 回忆之前的命令 Ctrl+Up 回顾命令列表(可先输入前缀进行查找) 焦点: ctrl+ 移动焦点到source编辑器 ctrl+ 移动焦点到console ctrl+L 清 ...
- java中关键字static和final
面向对象的不足 凡是有利必有弊,强对象编程,使得语法简单统一,但也有其缺点,而且有很多.我们在接下来的课程里会一点点接触到.我们今天先看第一个. 有些变量和函数确实没必要定义在一个类里.强行规定这些函 ...