【问题标题】:NiFi processor to search and replace multiple words in a txt fileNiFi 处理器在 txt 文件中搜索和替换多个单词
【发布时间】:2021-11-21 05:28:28
【问题描述】:

我有 txt 文件,我正在 NiFi 中使用 FetchSFTP 读取该文件。另外,我有 json 格式的键和值,如下所示在 REST 调用和 JoltTransformJSON 后收到:

[{
    "Key": "k2s2e2",
    "Value": "Ottawa"
}, {
    "Key": "60601",
    "Value": "Chicago"
}, {
    "Key": "",
    "Value": "London"
}]

如何将上面所有出现的匹配键替换为 txt 文件中的值。

示例:abc.txt

000 apple stocks at k2s2e2 888
9000 samsung stocks at 60601 9990377
88 nokia devivces at 78889 790888071 hgj 7

输出:

000 apple stocks at Ottawa 888
9000 samsung stocks at Chicago 9990377
88 nokia devivces at 78889 790888071 hgj 7

我尝试使用 ExecuteGroovyScript:

import static groovy.json.JsonOutput.toJson

import java.nio.charset.StandardCharsets

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

class KeyValue{
    String key
    String value
}

private findAndReplace(KeyValueList) {
    def response
    KeyValueList.each {
    def srcExp = it.key
    def replaceText = it.value

   def inputFilepath = "C:\\Project\\abc.txt"
    def outputFilepath = "C:\\Project\\abc_output.txt"
    
    new File(outputFilepath).withWriter { w ->
        new File(inputFilepath).eachLine { line ->
          w << line.replaceAll(srcExp , replaceText ) << '\n'
        }
      response = w
      }
new File(inputFilepath).text= new File(outputFilepath).text
}
return response;
}

def flowFile = session.get()
if(!flowFile) return

def KeyValueList = []
//try {
        def is = flowFile.read().withReader("UTF-8"){ new JsonSlurper().parse(it) }
        is.each {
               if(it.Key != "") {
                    KeyValue keyValue = new KeyValue(key:it.Key,value:it.Value)
                    KeyValueList.add(keyValue)
                 }
            }
          def retval =  findAndReplace(KeyValueList)
          flowFile = session.write(flowFile, {outputStream ->
               outputStream.write(retval.toString().getBytes(StandardCharsets.UTF_8))
           } as OutputStreamCallback)
    
    session.transfer(flowFile, REL_SUCCESS)
//}catch(Exception e) {
 //   log.info(e.getMessage())
//    REL_FAILURE << flowFile
//}

【问题讨论】:

  • 看起来您需要nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/…,但您的文本文件看起来不像结构化...
  • @daggett 是的,它不是结构化的。基本上我将不得不遍历整个 txt 文件并查找第一个 Key,如果找到则替换为 value,然后在 txt 文件中查找第二个 Key
  • 如果我使用 ExecuteGroovyScript 读取 txt 文件的内容。在 groovy 脚本中逐行读取 txt 文件,然后在第一行和第二行中查找密钥,依此类推。但是,我如何将 txt 文件和 json 键值的内容传递给 groovy 脚本。
  • 我认为您需要使用SimpleKeyValueLookupService 在两个文件之间映射键并获取值
  • 任何错误信息?也许文件进入错误输出?

标签: groovy apache-nifi


【解决方案1】:

这不是对您问题的回应。只是尝试修复您的代码。

如果我理解正确你正在尝试

  • 从流文件中读取 json
  • 从某个文件路径读取文本
  • 将文本替换为流文件

ExecuteGroovyScript 处理器的代码

import groovy.json.JsonSlurper

def ff = session.get()
if(!ff) return

ff.write{rawIn, rawOut->
    def keyValueList = rawIn.withReader("UTF-8"){ new JsonSlurper().parse(it) }

    new File('c:/Project/abc.txt').withReader("UTF-8"){reader->
        rawOut.withWriter("UTF-8"){writer->
            reader.eachLine{line->
               keyValueList.each{ if(it.Key) line = line.replaceAll(it.Key, it.Value) }
               writer << line << '\n'
            }
        }
    }
}

REL_SUCCESS << ff

没时间测试...

【讨论】:

  • 在上面的例子中,我们可以使用 JSch 和 SFTP 从 sftp 服务器读取 .txt 文件。这里唯一的问题是密码是硬编码的。 JSch ssh = new JSch() Session sess = ssh.getSession "user", "server.domain.com", 22 sess.with { setConfig config setPassword "somecomplicatedpassword" connect() Channel chan = openChannel "sftp" chan.connect( )
猜你喜欢
  • 1970-01-01
  • 2016-03-08
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
  • 2010-09-19
  • 2011-09-25
  • 2011-09-13
  • 1970-01-01
相关资源
最近更新 更多