我有一个简单的SQL文件,我想使用SnapLogic中的Python快照来读取和执行该文件。我创建了一个表达式库文件来引用Redshift帐户,并将它作为一个参数包含在管道中。
下面是另一篇文章的代码。是否有一种方法可以引用管道参数来连接到Redshift数据库,读取上传的SQL文件并执行命令?
fd = open('shared/PythonExecuteTest.sql', 'r')
sqlFile = fd.read()
fd.close()
sqlCommands = sqlFile.split(';')
for command in sqlCommands:
try:
c.execute(command)
except OperationalError, msg:
print "Command skipped: ", msg发布于 2018-10-22 20:17:54
您可以使用$_访问脚本中的管道参数。
比如说,您有一个管道参数executionId。然后,要在脚本中访问它,可以执行$_executionId。
下面是一个测试管道。

具有以下管道参数。

以下是测试数据。

以下是脚本
# Import the interface required by the Script snap.
from com.snaplogic.scripting.language import ScriptHook
import java.util
class TransformScript(ScriptHook):
def __init__(self, input, output, error, log):
self.input = input
self.output = output
self.error = error
self.log = log
# The "execute()" method is called once when the pipeline is started
# and allowed to process its inputs or just send data to its outputs.
def execute(self):
self.log.info("Executing Transform script")
while self.input.hasNext():
try:
# Read the next document, wrap it in a map and write out the wrapper
in_doc = self.input.next()
wrapper = java.util.HashMap()
wrapper['output'] = in_doc
wrapper['output']['executionId'] = $_executionId
self.output.write(in_doc, wrapper)
except Exception as e:
errWrapper = {
'errMsg' : str(e.args)
}
self.log.error("Error in python script")
self.error.write(errWrapper)
self.log.info("Finished executing the Transform script")
# The Script Snap will look for a ScriptHook object in the "hook"
# variable. The snap will then call the hook's "execute" method.
hook = TransformScript(input, output, error, log)输出:

在这里,您可以看到executionId是从管道参数读取的。
注意:从脚本访问管道参数的是一个有效的场景,但是从脚本访问其他外部系统很复杂(因为您需要加载所需的库),因此不建议这样做。使用SnapLogic提供的快照访问外部系统。另外,如果您想在脚本中使用其他库,请尝试坚持使用Javascript,而不是使用python,因为您可以在脚本中使用许多开放源码can。
此外,不能直接从脚本访问任何已配置的表达式库。如果您需要脚本中的一些逻辑,您可以将它保存在脚本中,而不是在其他地方。而且,访问脚本(或映射器)中的帐户名没有意义,因为即使您知道帐户名,也不能直接使用存储在该帐户中的凭据/配置;这是由SnapLogic处理的。尽可能多地使用提供的快照和映射器。
更新#1
更新#2
这一要求的最简单解决办法如下。
;的拆分https://stackoverflow.com/questions/52936600
复制相似问题