<?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 String to datetime/timestamp conversion generates some faulty values in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/String-to-datetime-timestamp-conversion-generates-some/m-p/368204#M240095</link>
    <description>&lt;P&gt;Hi there,&lt;BR /&gt;&lt;BR /&gt;So, I need your experience with a flow I have been struggling with.&lt;BR /&gt;My Flow is as follows: &lt;STRONG&gt;ConsumeKafka&lt;/STRONG&gt; --&amp;gt; &lt;STRONG&gt;ConvertRecord&lt;/STRONG&gt; --&amp;gt; &lt;STRONG&gt;ConvertJSONToSQL&lt;/STRONG&gt; --&amp;gt; &lt;STRONG&gt;PutSQL&lt;/STRONG&gt;.&lt;BR /&gt;I am extracting some data out of some &lt;STRONG&gt;kafka&lt;/STRONG&gt; &lt;STRONG&gt;brokers&lt;/STRONG&gt;, which come in the following format:&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;{"Column1": "1e39c17d25cb420e8e720aa4b1ae005e","TimeStamp": "2023-04-10T10:43:15.794241429+03:00","Column3": "some_string","Column4": false,"Column5": "some_string","Column6": "some_string","Column7": false,"Column8": "some_string","Column9": "","Column10": 0,"Column11": 0,"Column12": "","Column13": 0,"Column14": 137,"Column15": "","Column16": 0,"Column17": 0,"Column18": "","Column19": 138,"Column20": 1550,"Column21": "some_string","Column22": ""}&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;I am using afterwards an &lt;STRONG&gt;ConverRecord&lt;/STRONG&gt;, with an &lt;STRONG&gt;RecordReader&lt;/STRONG&gt; = &lt;STRONG&gt;JsonTreeReader&lt;/STRONG&gt; (with default configuration: Infer Schema and Root Node) and an &lt;STRONG&gt;RecordWriter&lt;/STRONG&gt; = &lt;STRONG&gt;JsonRecordSetWriter&lt;/STRONG&gt; (with default configuration: Inherit Record Schema and Pretty print json = false). The data comes out in JSON format, as expected, without any modifications.&lt;BR /&gt;&lt;BR /&gt;Next, I am going into a &lt;STRONG&gt;ConvertJSONToSQL&lt;/STRONG&gt;. Here, I have defined a &lt;STRONG&gt;JDBC Connection Pool&lt;/STRONG&gt;, set the &lt;STRONG&gt;Statement Type&lt;/STRONG&gt; = &lt;STRONG&gt;INSERT&lt;/STRONG&gt; and set the &lt;STRONG&gt;Table Name&lt;/STRONG&gt; = &lt;STRONG&gt;my_table&lt;/STRONG&gt;. All the other configurations remained the same. The data comes out as attributes and they look fine.&lt;/P&gt;&lt;P&gt;sql.args.2.type --&amp;gt; 93&lt;BR /&gt;sql.args.2.value --&amp;gt; &lt;SPAN&gt;2023-04-10T10:43:15.794241429+03:00&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;Now, due to the fact that I have to add the data in a &lt;STRONG&gt;PostgreSQL&lt;/STRONG&gt; Database, I am using &lt;STRONG&gt;PutSQL&lt;/STRONG&gt; to save the data. As the column I am inserting that value into is of format&lt;BR /&gt;"timestamp without time zone", I have added one &lt;STRONG&gt;UpdateAttribute&lt;/STRONG&gt; Processors to modify the date.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The first UpdateAttribute is defined with a property named &lt;STRONG&gt;"sql.args.2.value"&lt;/STRONG&gt; and has the following value&lt;STRONG&gt; "${sql.args.2.value:substringBefore('T')} ${sql.args.2.value:substringAfter('T'):substringBefore('+')}"&lt;/STRONG&gt;. The data which now comes out is: " 2023-04-10 10:43:15.794241429". When inserting the data into PostgreSQL, I have a different value as the one present into the attribute: "2023-04-19 15:20:36.429".&lt;BR /&gt;&lt;BR /&gt;To further debug the flow, I have added another UpdateAttribute and defined it with a property named &lt;STRONG&gt;"a3"&lt;/STRONG&gt; and has the following value&lt;STRONG&gt; ${sql.args.2.value:toDate("yyyy-MM-dd HH:mm:ss.SSS"):toNumber():format("yyyy-MM-dd HH:mm:ss.SSS")}&lt;/STRONG&gt;. I have added another property as well, named&lt;STRONG&gt; "a4"&lt;/STRONG&gt; and has the following value &lt;STRONG&gt;"${sql.args.2.value:toDate("yyyy-MM-dd HH:mm:ss.SSS"):toNumber()}"&lt;/STRONG&gt;. Now, the results are pretty strange:&lt;BR /&gt;a3 --&amp;gt; &lt;SPAN&gt;2023-04-19 15:20:36.429&lt;/SPAN&gt;&lt;BR /&gt;a4 --&amp;gt; &lt;SPAN&gt;1681906836429&lt;/SPAN&gt;.&lt;BR /&gt;&lt;BR /&gt;Basically the problems comes when translating that date into the datetime type. Does anybody know why it behaves like this? I am certain that this is not a bug, but something I am not using correctly &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Thank you &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Apr 2023 10:03:28 GMT</pubDate>
    <dc:creator>cotopaul</dc:creator>
    <dc:date>2023-04-11T10:03:28Z</dc:date>
    <item>
      <title>String to datetime/timestamp conversion generates some faulty values</title>
      <link>https://community.cloudera.com/t5/Support-Questions/String-to-datetime-timestamp-conversion-generates-some/m-p/368204#M240095</link>
      <description>&lt;P&gt;Hi there,&lt;BR /&gt;&lt;BR /&gt;So, I need your experience with a flow I have been struggling with.&lt;BR /&gt;My Flow is as follows: &lt;STRONG&gt;ConsumeKafka&lt;/STRONG&gt; --&amp;gt; &lt;STRONG&gt;ConvertRecord&lt;/STRONG&gt; --&amp;gt; &lt;STRONG&gt;ConvertJSONToSQL&lt;/STRONG&gt; --&amp;gt; &lt;STRONG&gt;PutSQL&lt;/STRONG&gt;.&lt;BR /&gt;I am extracting some data out of some &lt;STRONG&gt;kafka&lt;/STRONG&gt; &lt;STRONG&gt;brokers&lt;/STRONG&gt;, which come in the following format:&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;{"Column1": "1e39c17d25cb420e8e720aa4b1ae005e","TimeStamp": "2023-04-10T10:43:15.794241429+03:00","Column3": "some_string","Column4": false,"Column5": "some_string","Column6": "some_string","Column7": false,"Column8": "some_string","Column9": "","Column10": 0,"Column11": 0,"Column12": "","Column13": 0,"Column14": 137,"Column15": "","Column16": 0,"Column17": 0,"Column18": "","Column19": 138,"Column20": 1550,"Column21": "some_string","Column22": ""}&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;I am using afterwards an &lt;STRONG&gt;ConverRecord&lt;/STRONG&gt;, with an &lt;STRONG&gt;RecordReader&lt;/STRONG&gt; = &lt;STRONG&gt;JsonTreeReader&lt;/STRONG&gt; (with default configuration: Infer Schema and Root Node) and an &lt;STRONG&gt;RecordWriter&lt;/STRONG&gt; = &lt;STRONG&gt;JsonRecordSetWriter&lt;/STRONG&gt; (with default configuration: Inherit Record Schema and Pretty print json = false). The data comes out in JSON format, as expected, without any modifications.&lt;BR /&gt;&lt;BR /&gt;Next, I am going into a &lt;STRONG&gt;ConvertJSONToSQL&lt;/STRONG&gt;. Here, I have defined a &lt;STRONG&gt;JDBC Connection Pool&lt;/STRONG&gt;, set the &lt;STRONG&gt;Statement Type&lt;/STRONG&gt; = &lt;STRONG&gt;INSERT&lt;/STRONG&gt; and set the &lt;STRONG&gt;Table Name&lt;/STRONG&gt; = &lt;STRONG&gt;my_table&lt;/STRONG&gt;. All the other configurations remained the same. The data comes out as attributes and they look fine.&lt;/P&gt;&lt;P&gt;sql.args.2.type --&amp;gt; 93&lt;BR /&gt;sql.args.2.value --&amp;gt; &lt;SPAN&gt;2023-04-10T10:43:15.794241429+03:00&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;Now, due to the fact that I have to add the data in a &lt;STRONG&gt;PostgreSQL&lt;/STRONG&gt; Database, I am using &lt;STRONG&gt;PutSQL&lt;/STRONG&gt; to save the data. As the column I am inserting that value into is of format&lt;BR /&gt;"timestamp without time zone", I have added one &lt;STRONG&gt;UpdateAttribute&lt;/STRONG&gt; Processors to modify the date.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The first UpdateAttribute is defined with a property named &lt;STRONG&gt;"sql.args.2.value"&lt;/STRONG&gt; and has the following value&lt;STRONG&gt; "${sql.args.2.value:substringBefore('T')} ${sql.args.2.value:substringAfter('T'):substringBefore('+')}"&lt;/STRONG&gt;. The data which now comes out is: " 2023-04-10 10:43:15.794241429". When inserting the data into PostgreSQL, I have a different value as the one present into the attribute: "2023-04-19 15:20:36.429".&lt;BR /&gt;&lt;BR /&gt;To further debug the flow, I have added another UpdateAttribute and defined it with a property named &lt;STRONG&gt;"a3"&lt;/STRONG&gt; and has the following value&lt;STRONG&gt; ${sql.args.2.value:toDate("yyyy-MM-dd HH:mm:ss.SSS"):toNumber():format("yyyy-MM-dd HH:mm:ss.SSS")}&lt;/STRONG&gt;. I have added another property as well, named&lt;STRONG&gt; "a4"&lt;/STRONG&gt; and has the following value &lt;STRONG&gt;"${sql.args.2.value:toDate("yyyy-MM-dd HH:mm:ss.SSS"):toNumber()}"&lt;/STRONG&gt;. Now, the results are pretty strange:&lt;BR /&gt;a3 --&amp;gt; &lt;SPAN&gt;2023-04-19 15:20:36.429&lt;/SPAN&gt;&lt;BR /&gt;a4 --&amp;gt; &lt;SPAN&gt;1681906836429&lt;/SPAN&gt;.&lt;BR /&gt;&lt;BR /&gt;Basically the problems comes when translating that date into the datetime type. Does anybody know why it behaves like this? I am certain that this is not a bug, but something I am not using correctly &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Thank you &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2023 10:03:28 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/String-to-datetime-timestamp-conversion-generates-some/m-p/368204#M240095</guid>
      <dc:creator>cotopaul</dc:creator>
      <dc:date>2023-04-11T10:03:28Z</dc:date>
    </item>
    <item>
      <title>Re: String to datetime/timestamp conversion generates some faulty values</title>
      <link>https://community.cloudera.com/t5/Support-Questions/String-to-datetime-timestamp-conversion-generates-some/m-p/368312#M240124</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/35454"&gt;@MattWho&lt;/a&gt;, &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/95503"&gt;@steven-matison&lt;/a&gt;: would really appreciate your input as I am struggling with this and I do not know how to solve it, or what to further check and try &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;BR /&gt;I tried to replace &lt;STRONG&gt;ConvertRecord&lt;/STRONG&gt; with an &lt;STRONG&gt;UpdateRecord&amp;nbsp;&lt;/STRONG&gt;where I have tried updating my column&lt;STRONG&gt;&amp;nbsp;/TimeStamp&lt;/STRONG&gt; using the EL&amp;nbsp;&lt;STRONG&gt;${field.value:toDate("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"):format('yyyy-MM-dd HH:mm:ss.SSS')}&lt;/STRONG&gt;. Unfortunately, the same result, a new datetime is generated &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Apr 2023 09:55:02 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/String-to-datetime-timestamp-conversion-generates-some/m-p/368312#M240124</guid>
      <dc:creator>cotopaul</dc:creator>
      <dc:date>2023-04-12T09:55:02Z</dc:date>
    </item>
    <item>
      <title>Re: String to datetime/timestamp conversion generates some faulty values</title>
      <link>https://community.cloudera.com/t5/Support-Questions/String-to-datetime-timestamp-conversion-generates-some/m-p/368334#M240135</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/103151"&gt;@cotopaul&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Your issue is caused by trying to use the ":toDate" and ":format" functions here.&amp;nbsp; These two functions utilize the Java &lt;A href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html" target="_self"&gt;SimpleDateFormat&amp;nbsp;&lt;/A&gt;&amp;nbsp;which can not handle your date which uses nanoseconds.&lt;BR /&gt;&lt;BR /&gt;You'll want to use a NiFi Expression Language function that supports the Java &lt;A href="https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html" target="_self"&gt;DateTimeFormatter&lt;/A&gt;&amp;nbsp;like ":formatInstant", "toInstant", ":toNanos", or ":toMicros".&lt;BR /&gt;&lt;BR /&gt;Using your example:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;sql.args.2.value = 2023-04-10T10:43:15.794241429+03:00&lt;/LI-CODE&gt;&lt;P&gt;and an updateAttribute processor configured with this new dynamic property:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;newformat = ${sql.args.2.value:formatInstant('yyyy-MM-dd HH:mm:ss.SSS','GMT')}&lt;/LI-CODE&gt;&lt;P&gt;Will result in the newformat FlowFile attribute with a value of:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;2023-04-10 07:43:15.794&lt;/LI-CODE&gt;&lt;P&gt;You'll notice the output was designated as being GMT so 3 hours were removed since you had a +03:00 GMT/UTC offset in your original time format.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="batang,apple gothic"&gt;If you found that the provided solution(s) assisted you with your query, please take a moment to login and click&lt;/FONT&gt;&amp;nbsp;&lt;FONT face="arial black,avant garde" color="#FF0000"&gt;Accept as Solution&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="batang,apple gothic" color="#000000"&gt;below each response that helped.&lt;BR /&gt;&lt;BR /&gt;Thank you,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="batang,apple gothic" color="#000000"&gt;Matt&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Apr 2023 14:09:16 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/String-to-datetime-timestamp-conversion-generates-some/m-p/368334#M240135</guid>
      <dc:creator>MattWho</dc:creator>
      <dc:date>2023-04-12T14:09:16Z</dc:date>
    </item>
    <item>
      <title>Re: String to datetime/timestamp conversion generates some faulty values</title>
      <link>https://community.cloudera.com/t5/Support-Questions/String-to-datetime-timestamp-conversion-generates-some/m-p/368342#M240141</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/35454"&gt;@MattWho&lt;/a&gt;, it worked like a charm. You are a life saver &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P data-unlink="true"&gt;I did not even consider the nanoseconds and I did not really knew about EL functions for the Java DateTimeFormatter. Neverthless, if somebody else encounters a similar issue, here is the link to the documentation --&amp;gt; &lt;A href="https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html#formatInstant" target="_self"&gt;here.&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;One more question though, if possible. When saving the data into the postgresql database, using &lt;STRONG&gt;PutDatabaseRecord&lt;/STRONG&gt; (JSON as Reader) , the value "2023-04-10 07:43:15.794" gets immediately truncated to "2023-04-10 07:43:15" --&amp;gt; basically it removed everything after the point. In postgresql, the column is defined as "timestamp without time zone" with an precision of 6.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Apr 2023 14:58:25 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/String-to-datetime-timestamp-conversion-generates-some/m-p/368342#M240141</guid>
      <dc:creator>cotopaul</dc:creator>
      <dc:date>2023-04-12T14:58:25Z</dc:date>
    </item>
  </channel>
</rss>

