标签搜索

【NIFI】nifi 自用的script,配置脚本,用于split json

dforel
2021-04-22 / 0 评论 / 0 阅读 / 正在检测是否收录...


//读取内容在写入内容
var InputStreamCallback =  Java.type("org.apache.nifi.processor.io.InputStreamCallback");
var OutputStreamCallback =  Java.type("org.apache.nifi.processor.io.OutputStreamCallback");
var IOUtils = Java.type("org.apache.commons.io.IOUtils");
var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
        
var flowFile = session.get();
if (flowFile != null) {
    var i = 0;
    try {
        var text = ""; 
       // 读取flowFile中内容
        session.read(flowFile,new InputStreamCallback(function(inputStream) {
            var str = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
            var arr = JSON.parse(str);  
            if( (arr instanceof Array) && arr.length > 0 ){  
                for (; i < arr.length; i = i + 1) { 
                    text = JSON.stringify(arr[i]); 
                    var newFlowFile = session.create(flowFile); 
                    // 向flowFile中写入内容
                    newFlowFile = session.write(newFlowFile, new OutputStreamCallback(function(outputStream) {
                        outputStream.write( text.getBytes(StandardCharsets.UTF_8))
                    })); 
                    session.transfer(newFlowFile, REL_SUCCESS)  
                }
            } 
        }));  
        session.transfer(flowFile, REL_FAILURE);   
    } catch(e) {
        log.error('Something went wrong', e) 
        // session.transfer(flowFile, REL_FAILURE)
    } 
}




0

评论 (0)