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 ...
随机推荐
- Python学习-29.Python中列表的一些操作
in关键字: 注意这个是关键字,用来判断元素是否在集合中存在. list = ['a','b','c'] print('a' in list) print('f' in list) 将依次输出 Tru ...
- 如何在Windows应用商店中提交您的Windows 8.1 应用更新
翘首以盼的Windows 8.1 不负众望的与大家见面了,与此同时也带来了全新的应用商店,小伙伴儿们要赶紧升级系统啦! 今天给大家介绍下如何提交一个Windows 8.1 的应用,其实微软针对这次系统 ...
- 解决 Eclipse Indigo 3.7、ADT 中文字体偏小,完美 Consolas 微软雅黑混合字体!
Eclipse是著名的跨平台的自由集成开发环境(IDE).6月22日Eclipse 3.7 正式发布,代号是 Indigo . 在 Windows 7 下初始后化,发现界面变化不大,但中文字体却面目全 ...
- Backbone学习笔记 - Model篇
2 Model 在Backbone中,Model用于存储核心数据,可以将数据交互相关的逻辑代码放在这里.基本形式如下: var Human = Backbone.Model.extend({ init ...
- 在QT中用git做版本管理时遇到的一些问题
1. 安装git sudo apt-get install git 2. 安装gitk sudo apt-get install gitk 要提交代码,点击 工具->git->local ...
- python 函数中使用全局变量
python 函数中如果需要使用全局变量,需要使用 global + 变量名 进行声明, 如果不声明,那么就是重新定义一个局部变量,并不会改变全局变量的值 n [1]: a = 3 In [2]: d ...
- 3.jquery在js文件中获取选择器对象
一.常用的选择器有一下几种: 1.标签选择器 2.类选择器 3.id选择器 4.并集选择器 5.层级选择器 二.如何获取选择器对象: <!DOCTYPE html> <html la ...
- Two Pointers-349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- SpringBoot入门之事件监听
spring boot在启动过程中增加事件监听机制,为用户功能拓展提供极大的便利,sptingboot支持的事件类型有以下五种: ApplicationStartingEvent Applicatio ...
- PHP之旅3 php数组以及遍历数组 以及each() list() foreach()
php的数组的定义 <?php //php中定义数组时可以通过索引直接进行赋值: $mArr[0]="哈哈"; $mArr[1]=70; $mArr[2]='haha'; e ...