![](/img/trans.png)
[英]Programmatically stop execution of python script after running condition statement
[英]Execution of python script success/fail based on condition
我有一个python
脚本,我将使用Pyspark
执行该脚本。 python
文件如下所示
#!/usr/bin/env python
from datetime import datetime
from pyspark import SparkContext, SparkConf
from pyspark.sql import HiveContext
conf = SparkConf()
sc = SparkContext(conf=conf)
sqlContext = HiveContext(sc)
df = sqlContext.sql("select id, name, start_date from testing.user_123")
hivedb='test'
table='abc_123'
# Register the Data Frame as a TempTable
df.registerTempTable('mytempTable')
# Create Table in Hive using the temptable
status = 'success'
try:
sqlContext.sql("create table {}.`{}` as select * from mytempTable".format(hivedb,table))
except:
status = 'fail'
sc.stop()
我得到了想要的结果。 现在,当我在shell script
中使用spark -submit
-submit 执行此python
文件时,我一直处于success
状态。
Python script execution as failed if the status message is fail
, success if the status is success
我需要在脚本中更改什么才能获得预期结果。
因为hivedb
和table
都是硬编码的,并且mytempTable
已经存在, "create table {}.
{} as select * from mytempTable"
将永远成功,如果找不到任何值,它只会创建一个空表。 你需要一个不同的条件来检查; 也许你的select
查询的长度?
只需在python脚本的末尾添加一个assert语句。 如果状态变量值不是“成功”,这将使python脚本失败。
assert status == 'success', 'status should be success'
尝试在 try 块中添加 sys.exit(1)
try:
sqlContext.sql("create table {}.`{}` as select * from mytempTable".format(hivedb,table))
except:
status = 'fail'
sys.exit(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.