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 ...
随机推荐
- [php] cookie 跨域共享
<?php // m.hinabian.com class Controller_Cookie extends Hnb_Base_Controller { public function ini ...
- LeetCode145:Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
- 关于C_Sharp集中处理异常
1.写在前面 “异常意味着什么?”,想必不用对此做多余的解释,我们有理由相信在任何情况下任何应用程序都有可能出现异常,若在程序中没有对异常进行处理,则操作系统会以粗暴的方式处理掉它(弹出错误提示框), ...
- 基于Quartz.net的远程任务管理系统 一
在上一篇绪中,已经介绍了整个项目的情况下了,接下来就是开始一步步做起来了. 首先:先整个我们的Job任务表,以及Job执行日志表.SQL如下: drop table if exists job_inf ...
- C# wpf InkCanvas 保存图片jpg
前端xaml页面代码 <Window x:Class="WpfApplication6.MainWindow" xmlns="http://schemas.micr ...
- JEECG(一) 如何配置自己的业务包
使用自己的业务包开发,不在com.jeecg业务包下 请首先阅读这篇文章[官网] http://www.jeecg.org/forum.php?mod=viewthread&tid=1832& ...
- 程序媛计划——mysql连接表
#inner join等值连接/内连接 mysql> select * from info; +------+-------------+----------+ | name | phone | ...
- 【AUTO Uninstaller 中文版-详细使用教程】AUTODESK系列软件MAYA/CAD/3DSMAX/INVENTOR终极完美修复卸载工具 Windows x64位 【原创搬家】
小伙伴是不是遇到 MAYA/CAD/3DSMAX/INVENTOR/REVIT 安装失败或者安装不了的问题了呢?AUTODESK系列软件着实令人头疼,MAYA/CAD/3DSMAX/INVENTOR/ ...
- jQuery基础笔记(1)
day54 参考:https://www.cnblogs.com/liwenzhou/p/8178806.html 1. 为什么要学jQuery? MySQL Python ...
- poj3070 Fibonacci(矩阵快速幂)
矩阵快速幂基本应用. 对于矩阵乘法与递推式之间的关系: 如:在斐波那契数列之中 f[i] = 1*f[i-1]+1*f[i-2] f[i-1] = 1*f[i-1] + 0*f[i-2].即 所以, ...