我正在尝试使用通过 python 脚本从 xml 文件获取的变量。获得变量后,我想将它们传递给由 shell 脚本运行的命令。需要帮助
我的 Python 脚本如下所示
#!/usr/bin/python
import os
import xml.etree.ElementTree as etree
from hive import ThriftHive
filename = "mood_ib_history_parameters_DEV.xml"
__currentlocation__ = os.getcwd()
__fullpath__ = os.path.join(__currentlocation__,filename)
tree = etree.parse(__fullpath__)
root = tree.getroot()
hive_db = root.find("hive_db").text
EDGE_HIVE_CONN = root.find("EDGE_HIVE_CONN").text
target_dir = root.find("target_dir").text
to_email_alias = root.find("to_email_alias").text
to_email_cc = root.find("to_email_cc").text
from_email_alias = root.find("from_email_alias").text
dburl = root.find("dburl").text
SQOOP_EDGE_CONN = root.find("SQOOP_EDGE_CONN").text
user_name = root.find("user_name").text
password = root.find("password").text
IB_log_table = root.find("IB_log_table").text
SR_DG_master_table = root.find("SR_DG_master_table").text
SR_DG_table = root.find("SR_DG_table").text
print hive_db
print EDGE_HIVE_CONN
print target_dir
print to_email_alias
print to_email_cc
print from_email_alias
print dburl
print SQOOP_EDGE_CONN
print user_name
print password
print IB_log_table
print SR_DG_master_table
print SR_DG_table
变量将是
- hive 数据库
- EDGE_HIVE_CONN
- 目标目录
- to_email_alias
- to_email_cc
- from_email_alias
- dburl
- SQOOP_EDGE_CONN
- 用户名
- 密码
- IB_log_table
- SR_DG_master_table
- SR_DG_table
将在shell脚本中运行的shell命令如下
ssh -i /apps/phodisvc/.ssh/id_rsa_edge_node ${SQOOP_EDGE_CONN} "sqoop import -D mapred.child.java.opts='\-Djava.security.egd=file:/dev/../dev/urandom' --connect '${dburl}' --username ${user_name} --password ${password} --query \"select CONTRACT_ID,CONTRACT_NUMBER,CONTRACT_STS_CODE,CONTRACT_STATUS,SERVICE_LINE_ID from XXCCS where \\\$CONDITIONS \" --split-by CONTRACT_NUMBER -m 4 --null-string '\\\\N' --null-non-string '\\\\N' --hive-delims-replacement '<EOL>' --boundary-query 'select (select min(CONTRACT_NUMBER) from XXCCS_DS_SAHDR_CORE) as minid ,(select max(CONTRACT_NUMBER) from XXCCS_DS_SAHDR_CORE) as maxid from dual' --target-dir ${target_dir}/XXCCS_DS_SAHDR_CORE --hive-import --hive-overwrite --hive-table ${hive_db}.XXCCS --map-column-hive CONTRACT_ID=BIGINT,SERVICE_LINE_ID=BIGINT"
我不想在第一个脚本中使用 call() 或 subprocess() 方法,因为我想将任务分成模块。需要帮助。
最佳答案
一旦知道了变量,您只需将其传递给 shell 中的命令行即可。
cmd = 'ssh -i /apps/phodisvc/.ssh/id_rsa_edge_node {0} "sqoop import -D mapred.child.java.opts='\-Djava.security.egd=file:/dev/../dev/urandom' --connect '{1}' --username {2} --password {3} --query \"select CONTRACT_ID,CONTRACT_NUMBER,CONTRACT_STS_CODE,CONTRACT_STATUS,SERVICE_LINE_ID from XXCCS where \\\$CONDITIONS \" --split-by CONTRACT_NUMBER -m 4 --null-string '\\\\N' --null-non-string '\\\\N' --hive-delims-replacement '<EOL>' --boundary-query 'select (select min(CONTRACT_NUMBER) from XXCCS_DS_SAHDR_CORE) as minid ,(select max(CONTRACT_NUMBER) from XXCCS_DS_SAHDR_CORE) as maxid from dual' --target-dir ${target_dir}/XXCCS_DS_SAHDR_CORE --hive-import --hive-overwrite --hive-table ${hive_db}.XXCCS --map-column-hive CONTRACT_ID=BIGINT,SERVICE_LINE_ID=BIGINT"'.format(SQOOP_EDGE_CONN,user_name,password,...)
os.sytem(Cmd)
医生
关于python - 将成员/变量从 python 脚本传递到 shell 脚本或命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34852908/