apache-nifiapache-calcite

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.


Solution

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

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

    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)

    SELECT name, tags 
    FROM FLOWFILE 
    WHERE tags_str LIKE '%/test2/%'
    

    output (JSON):

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