我正在尝试从文件中读取密码并传递到SAP连接中。由于我有50多个脚本使用相同的密码,因此如果密码被更改,则无需输入每个脚本并进行更改,而只需更改一个文件即可。
from pyspark.context import SparkContext
from pyspark.sql import HiveContext
from pyspark.sql.functions import *
from pyspark.sql.types import *
sc = SparkContext()
sqlContext = HiveContext(sc)
sqlContext.setConf("spark.sql.tungsten.enabled", "false")
ZTGLINT011_query = """ (select * from ZTGLINT011) a """
#passwd = sc.textFile("file:///root/Documents/pwd.txt")
passwd=cat /root/Documents/pwd.txt
ZTGLINT011 = sqlContext.read.format("jdbc").options(url="jdbc:sap://myIP:30015",currentschema="SAPABAP1",user="loginName",password=passwd,dbtable=ZTGLINT011_query).load()
ZTGLINT011.write.format("parquet").save("/user/admin/sqoop/base/sap/ZTGLINT011/")
答案 0 :(得分:1)
在txt文件中输入内容:
{ “密码”:“您的密码” }
复制粘贴的代码即可使用:
from pyspark.context import SparkContext
from pyspark.sql import HiveContext
from pyspark.sql.functions import *
from pyspark.sql.types import *
import json
sc = SparkContext()
sqlContext = HiveContext(sc)
sqlContext.setConf("spark.sql.tungsten.enabled", "false")
ZTGLINT011_query = """ (select * from ZTGLINT011) a """
passwfile = open("/root/Documents/pwd.txt", "r")
contents = passwfile.read()
info = json.dumps(contents)
passwd = info['Password']
ZTGLINT011 = sqlContext.read.format("jdbc").options(url="jdbc:sap://myIP:30015",currentschema="SAPABAP1",user="loginName",password=passwd,dbtable=ZTGLINT011_query).load()
ZTGLINT011.write.format("parquet").save("/user/admin/sqoop/base/sap/ZTGLINT011/")
答案 1 :(得分:0)
from pyspark.context import SparkContext
from pyspark.sql import HiveContext
from pyspark.sql.functions import *
from pyspark.sql.types import *
sc = SparkContext()
sqlContext = HiveContext(sc)
sqlContext.setConf("spark.sql.tungsten.enabled", "false")
ZTGLINT011_query = """ (select * from ZTGLINT011) a """
passwfile = open("/root/Documents/pwd.txt", "r")
for passwd in passwfile:
ZTGLINT011 = sqlContext.read.format("jdbc").options(url="jdbc:sap://myIP:30015",currentschema="SAPABAP1",user="loginName",password=passwd,dbtable=ZTGLINT011_query).load()
ZTGLINT011.write.format("parquet").save("/user/admin/sqoop/base/sap/ZTGLINT011/")
答案 2 :(得分:0)
最好的做法是将密码,用户名,IP,PORT等存储为/root/Documents/pwd.txt,以
============ pwd.txt:内容============
{ “ IP”:“ myIP”, “端口”:8080, “ Usernme”:“用户”, “密码”:“通过” }
================================================
在您的代码中:
import json
f= open('/root/Documents/pwd.txt','r')
contents = f.read()
server_details = json.loads(contents)
password = server_details['Password']
username=server_details['Password']
Port =server_details["PORT"]
IP=server_details["IP"]
ZTGLINT011 = sqlContext.read.format("jdbc").options(url="jdbc:sap://"+IP+":"+Port,currentschema="SAPABAP1",user=username,password=password,dbtable=ZTGLINT011_query).load()
您也可以在txt文件中添加currentschema和dbtable