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 scenario is as follows:
I will be having a CSV file with few fields with integers and few fields as total String values. For the fields that have a String value, I want to insert that as a pre-defined integer number into my database table. For Example:
My CSV looks like the following:
Field1, Field2, Field3, Field4,Field5
1,2,abc,45,John
23,12,pqr,28,Sam
98,75,lmn,87,Smith
So now in MySQL table, the Field3 and Field4 have datatype as Integer, so before inserting the CSV into the Database I want to Change the string value into Pre defined Integer value I.e. For Field3 abc value should be entered into the MySQL Table as 0, pqr as 1, lmn as 2 and so on…. similarly the value in Field4 should be entered in the MySQL table as John as 10, Sam as 20 and Smith as 30…so my above csv should be stored as follows in my database:
Field1, Field2, Field3, Field4,Field5
1,2,0,45,10
23,12,1,28,20
98,75,2,87,30
I think I can achieve this with ReplaceText. But if there is any better way to achieve this That would be great. Because My CSV will have 1000+ columns and let’s say 50+ columns would have a String value that I need to process and convert into a pre-defined integer value. Is it possible to achieve this in NiFi?
If yes then what configs to set and what will be the best approach?
Thank you!
2
Answers
Use
QueryRecord
processor and configure/enable Reader/Writer controller servicescustom sql query
as new property to the processorQueryRecord configs:
The output flowfile from QueryRecord processor will have your desired result
Use this template for your reference to the above flow and QueryRecord processor uses Apache Calcite sql parser.
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 aSimpleKeyValueLookupService
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