Replace string value to integer value in NiFi within a same column

后端 未结 2 1468
栀梦
栀梦 2021-01-21 12:26

I want to replace a \'string value\' and enter that value as an Integer Value using Nifi ReplaceText. I do not know how to achieve this in NiFi.

So my

相关标签:
2条回答
  • 2021-01-21 13:24

    Use QueryRecord processor and configure/enable Reader/Writer controller services

    • Add custom sql query as new property to the processor

    QueryRecord configs:

    select Field1,Field2,
        CASE WHEN Field3='abc' THEN '0'
             WHEN Field3='pqr' THEN '1'
             WHEN Field3='lmn' THEN '2'
        end Field3,
            Field4,
        CASE WHEN Field5='John' THEN '10'
             WHEN Field5='Sam' THEN '20'
             WHEN Field5='Smith' THEN '30'
        end Field5 
    from FLowfile
    

    The output flowfile from QueryRecord processor will have your desired result

    Field1,Field2,Field3,Field4,Field5
    1,2,0,45,10
    23,12,1,28,20
    98,75,2,87,30
    

    Use this template for your reference to the above flow and QueryRecord processor uses Apache Calcite sql parser.

    0 讨论(0)
  • 2021-01-21 13:27

    What @Shu had suggested would work well but in case the number of lookups to be done is more or increasing, I would highly suggest to use the LookupRecord processor and configure it with a SimpleKeyValueLookupService for starters since your CASE statement would grow heavy and troublesome.

    So your flow can be altered to something like

    For a look on how to use, take a look at the simple template provided here : https://gist.github.com/zenfenan/37eb1e4cd0e59a63e85b0400040720b6

    Useful links:

    • https://community.hortonworks.com/articles/138632/data-flow-enrichment-with-nifi-lookuprecord-proces.html

    • https://medium.com/@abdelkrim.hadjidj/data-flow-enrichment-with-apache-nifi-d221f1dde419

    0 讨论(0)
提交回复
热议问题