首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在snaplogic中将Python脚本转换为JVM

如何在snaplogic中将Python脚本转换为JVM
EN

Stack Overflow用户
提问于 2018-12-05 12:37:09
回答 1查看 196关注 0票数 2

下面提到的代码运行良好,但我希望将下面提到的python脚本集成到JVM脚本中,以便在SnapLogic工具中运行。任何线索都会很有帮助。

代码语言:javascript
复制
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()   
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-22 20:09:56

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

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

代码语言:javascript
复制
# 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)

下面是脚本快照的输入JSON。

代码语言:javascript
复制
[{"filename":"write_test.txt","content":{"id":123,"message":"xyz","valid":true}}]

检查节点中的文件。

代码语言:javascript
复制
$ cd /tmp
$ cat write_test.txt
[{"filename":"write_test.txt","content":{"id":123,"message":"xyz","valid":true}}]

备注:我使用杰克逊的ObjectMapper,因为我主要处理JSON。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53632522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档