【问题标题】:How to convert Python script to JVM in snaplogic如何在 snaplogic 中将 Python 脚本转换为 JVM
【发布时间】:2018-12-05 12:37:09
【问题描述】:

下面提到的代码工作正常,但我想将下面提到的 python 脚本集成到 JVM 脚本中以在 SnapLogic 工具中运行。任何线索都会非常有帮助。

import os
import sys

def execute():

    file=open("C:/Python27/snaplogic_file.txt","r")
    header=next(file)
    new_file1=open("C:/Python27/snaplogic_processed_file1.txt",mode='w+')
    new_file1.write(header)
    new_file1.close()
    for line in file:
       new_file=open("C:/Python27/snaplogic_processed_file.txt",mode='w+')
       new_file.write(line)


execute()   

【问题讨论】:

    标签: jvm jython snaplogic snaplogic-script-snap


    【解决方案1】:

    当您在 Script 快照中选择 Python 时,它实际上意味着 Jython。因此,您基本上可以在脚本中导入 Java 类。

    以下是我在其中一个节点的 /tmp 文件夹中编写虚拟文件的实现。

    # Import the interface required by the Script snap.
    from com.snaplogic.scripting.language import ScriptHook
    import java.util
    import com.fasterxml.jackson.databind
    import java.io
    
    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()
    
                    om = com.fasterxml.jackson.databind.ObjectMapper()
                    target_file = java.io.File("/tmp/" + in_doc['filename'])
                    om.writeValue(target_file, in_doc['content']);
    
                    wrapper['original'] = in_doc
                    wrapper['status'] = "success"
    
                    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)
    

    以下是 Script 快照的输入 JSON。

    [{"filename":"write_test.txt","content":{"id":123,"message":"xyz","valid":true}}]
    

    检查节点中的文件。

    $ cd /tmp
    $ cat write_test.txt
    [{"filename":"write_test.txt","content":{"id":123,"message":"xyz","valid":true}}]
    

    注意:我使用 Jackson 的 ObjectMapper,因为我主要处理 JSON。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 2011-02-19
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多