<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: What's the best way to update rows in a database with before and after values by NiFi in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/What-s-the-best-way-to-update-rows-in-a-database-with-before/m-p/390133#M247202</link>
    <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/80381"&gt;@SAMSAL&lt;/a&gt;&amp;nbsp;, do you think this way is simpler for production and support than Groovy? As an experiment, I created my solution with Groovy yesterday (four processors: Kafka -&amp;gt; Groovy -&amp;gt; ExtractText -&amp;gt; PutSql). My boss believes that we need only simple NiFi blocks. I have about 120 tables with 2000 columns. I need to convert milliseconds to timestamps, escape quotes, replace nulls with SQL 'NULL', and get a list of primary keys from the database. Can I do this with simple blocks? For me, Groovy is the simplest way. Why does the NiFi community not recommend using Groovy? Maybe I need an API creator script to generate simple block assemblies? What is the best practice in the NiFi world?&lt;/P&gt;</description>
    <pubDate>Thu, 11 Jul 2024 07:14:39 GMT</pubDate>
    <dc:creator>ageMLex</dc:creator>
    <dc:date>2024-07-11T07:14:39Z</dc:date>
    <item>
      <title>What's the best way to update rows in a database with before and after values by NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/What-s-the-best-way-to-update-rows-in-a-database-with-before/m-p/390079#M247175</link>
      <description>&lt;P&gt;Hi everyone!&lt;/P&gt;&lt;P&gt;I have json from kafka:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;[ {
"before" : {
"old_key1" : old_value1,
"old_key2" : old_value2,
"old_key3" : old_value3,
"old_key4" : old_value4
},
"after" : {
"new_key1" : new_value1,
"new_key2" : new_value2,
"new_key3" : new_value3,
"new_key4" : new_value4
},
"table" : "table_name",
"op" : "u", -- operation ['u-update', 'c-insert', 'd-delete']
"tm" : 1720539441000000000 - timestamp of changes
} ]&lt;/LI-CODE&gt;&lt;P&gt;I need execute on database sql like:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;update table_name
set new_key1=new_value1,
  new_key2=new_value2, 
  new_key3=new_value3,
  new_key4=new_value4
where
  old_key1=old_value1 and 
  old_key2=old_value2 and 
  old_key3=old_value3 and 
  old_key4=old_value4;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;What's the best way to update rows in a database with before and after values by NiFi. I can`t use a groovy script &lt;SPAN&gt;because our team know sql only and if I produce it on groovy, I must support this alone. I have tried create sql by jolt, but all values without quotes and I can`t&amp;nbsp; test a type.&amp;nbsp; I think a have two ways: 1) insert all data to sql and create sql by sql 2) Create new Processor with ability of create the sql (But &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I can do it by groovy)&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 11:47:21 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/What-s-the-best-way-to-update-rows-in-a-database-with-before/m-p/390079#M247175</guid>
      <dc:creator>ageMLex</dc:creator>
      <dc:date>2024-07-10T11:47:21Z</dc:date>
    </item>
    <item>
      <title>Re: What's the best way to update rows in a database with before and after values by NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/What-s-the-best-way-to-update-rows-in-a-database-with-before/m-p/390092#M247177</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/111526"&gt;@ageMLex&lt;/a&gt; ,&lt;/P&gt;&lt;P&gt;Which version of Nifi are you using. If you are using Nifi version 1.19 or higher then you can take advantage of the jslt transformation using the&amp;nbsp; JSLTTransformJson processor. Its another json transformation language that is very powerful and work just like xquery in xml. Unlike jolt the result can be none json&amp;nbsp; like stirng, integer , boolean...etc.&lt;/P&gt;&lt;P&gt;Assuming you have the following json:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[
	{
		"before": {
			"old_key1": "old_value1",
			"old_key2": "old_value2",
			"old_key3": "old_value3",
			"old_key4": "old_value4"
		},
		"after": {
			"new_key1": "new_value1",
			"new_key2": "new_value2",
			"new_key3": "new_value3",
			"new_key4": "new_value4"
		},
		"table": "table_name",
		"op": "u",
		"tm": 1720539441000000000
	}
]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The Jslt script would look like this:&lt;/P&gt;&lt;P&gt;This will produce a sting like this when you can later use in ExecuteSQL,PUTSQL ..etc:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="ruby"&gt;let table = .[0].table 
let oldValues = [for (.[0].before) .key+"="+.value ]
let newValues = [for (.[0].after) .key+"="+.value ]
let sqlUpdate = "update "+$table +" set "+ join($newValues,",") + " where " + join($oldValues," and ")
$sqlUpdate &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note: when using the above scipt in the jslt processor try to have it in one line. For some reason the processor will give syntax error if the script is broken into different lines. I think this is a bug&amp;nbsp; in the processor not accounting correctly for&amp;nbsp; newline.&lt;/P&gt;&lt;P&gt;This will produce the following output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;"update table_name set new_key1=new_value1,new_key2=new_value2,new_key3=new_value3,new_key4=new_value4 where old_key1=old_value1 and old_key2=old_value2 and old_key3=old_value3 and old_key4=old_value4"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can use ExtractText with the following pattern to get rid of double quotation in the beginning and the end using regex:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;^"(.*)"$&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For more info about jslt please check this &lt;A href="https://github.com/schibsted/jslt" target="_self"&gt;site&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 13:42:16 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/What-s-the-best-way-to-update-rows-in-a-database-with-before/m-p/390092#M247177</guid>
      <dc:creator>SAMSAL</dc:creator>
      <dc:date>2024-07-10T13:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: What's the best way to update rows in a database with before and after values by NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/What-s-the-best-way-to-update-rows-in-a-database-with-before/m-p/390095#M247180</link>
      <description>&lt;P&gt;Thank you for your replay! Version is&amp;nbsp;&lt;SPAN&gt;2.0.0-M3&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV&gt;&lt;P&gt;Tagged&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;nifi-2.0.0-M3-RC1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;From&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;f2215c6&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;on branch&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;UNKNOWN. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I will to try your solution tomorrow. Thank you again!&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 10 Jul 2024 13:56:12 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/What-s-the-best-way-to-update-rows-in-a-database-with-before/m-p/390095#M247180</guid>
      <dc:creator>ageMLex</dc:creator>
      <dc:date>2024-07-10T13:56:12Z</dc:date>
    </item>
    <item>
      <title>Re: What's the best way to update rows in a database with before and after values by NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/What-s-the-best-way-to-update-rows-in-a-database-with-before/m-p/390133#M247202</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/80381"&gt;@SAMSAL&lt;/a&gt;&amp;nbsp;, do you think this way is simpler for production and support than Groovy? As an experiment, I created my solution with Groovy yesterday (four processors: Kafka -&amp;gt; Groovy -&amp;gt; ExtractText -&amp;gt; PutSql). My boss believes that we need only simple NiFi blocks. I have about 120 tables with 2000 columns. I need to convert milliseconds to timestamps, escape quotes, replace nulls with SQL 'NULL', and get a list of primary keys from the database. Can I do this with simple blocks? For me, Groovy is the simplest way. Why does the NiFi community not recommend using Groovy? Maybe I need an API creator script to generate simple block assemblies? What is the best practice in the NiFi world?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2024 07:14:39 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/What-s-the-best-way-to-update-rows-in-a-database-with-before/m-p/390133#M247202</guid>
      <dc:creator>ageMLex</dc:creator>
      <dc:date>2024-07-11T07:14:39Z</dc:date>
    </item>
    <item>
      <title>Re: What's the best way to update rows in a database with before and after values by NiFi</title>
      <link>https://community.cloudera.com/t5/Support-Questions/What-s-the-best-way-to-update-rows-in-a-database-with-before/m-p/390139#M247206</link>
      <description>&lt;P&gt;It all depends on the complexity of the data you are working with. If you are taking about data transformation (converting to timestamp, replacing quote, ...etc.) then maybe groovy is the way to go. JSLT has some function that can you help you accomplish this as well like string replace and parse-time functons but Im not sure that is everything. Im not sure where did you get the impression that the nifi community doesnt recommend using groovey and if you find an article about that please share.&amp;nbsp;I think its more of an issue with your Boss not wanting you to do any scripting to avoid not having this supported by others than you.&lt;/P&gt;&lt;P&gt;The processor is there for you to use. Actually there is a dedicated processor for groovey called ExecuteGroovyScript. I think the ExecuteScript processor might get deprecated since its redundant. The only issue that&amp;nbsp; I can find that warns about this processor is the fact the script is getting compiled for every flowfile and that might get expensive and impact the performance if you have a big script and working with large data volume. To avoid running into those scenarios, Nifi provides other alternative like InvokeScriptedProcessor (using groovey as well) or develop your custom processor in java (.nar) where the code is compiled once and done.&lt;/P&gt;&lt;P&gt;The jslt processor also re compiles the script but it uses caching to avoid having to do that every time. In terms which performs better: groovey or jslt? Im not sure and I have never tested but you can do some stress testing and let us know :).&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2024 09:57:26 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/What-s-the-best-way-to-update-rows-in-a-database-with-before/m-p/390139#M247206</guid>
      <dc:creator>SAMSAL</dc:creator>
      <dc:date>2024-07-11T09:57:26Z</dc:date>
    </item>
  </channel>
</rss>

