简体   繁体   中英

Nifi QueryRecord on Array of String contains value

Using Apache Nifi i am trying to figure out how to find records which have a string in an array that start with a value

Given the below array, i would like only record which have a tag that start with '/test2/'


[
   {
    "name":"bob",
    "tags":[ "/test1/foo","/alpha"]
   }
   ,
   {
    "name":"bill",
    "tags":[ "/test2/blah","/beta"]
   }

]

SELECT * FROM FLOWFILE WHERE RPATH_STRING(tags, '/') LIKE '/test2/%'

due to java.lang.String cannot be cast to org.apache.nifi.serialization.record.Record: java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.nifi.serialization.record.Record

I've tried a few other permutations, but no luck.

Possible solution with 2 processors ( ScriptedTransformProcessor -> QueryRecord ):

ScriptedTransformProcessor (add new field tags_str - concatenating all of the elements in tags with delimiter | )

  • Script Language : Groovy
  • Script Body :
record.setValue('tags_str', record.getValue('tags').join("|"))
record

Output (JSON):

[ {
  "name" : "bob",
  "tags" : [ "/test1/foo", "/alpha" ],
  "tags_str" : "/test1/foo|/alpha"
}, {
  "name" : "bill",
  "tags" : [ "/test2/blah", "/beta" ],
  "tags_str" : "/test2/blah|/beta"
} ]

QueryRecord (filter)

  • filter (dynamic property):
SELECT name, tags 
FROM FLOWFILE 
WHERE tags_str LIKE '%/test2/%'

output (JSON):

[ {
  "name" : "bill",
  "tags" : [ "/test2/blah", "/beta" ]
} ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM