Skip to main content
Solved

Convert String to Datetime Error

  • November 12, 2021
  • 6 replies
  • 1525 views

mtaka
Fan

Hello,

I am using a Field Type Converter to convert string to datetime with date format (yyyy-MM-dd'T'HH:mm:ss.SSS'Z' -  2021-08-18T08:20:24.136000Z).  However, the same field has dates with format 2021-08-18T08:20:24.00Z, which it gives me error CONVERTER_00 - Failed to convert field '/createdate' of type 'STRING' with value '2021-08-19T14:01:32Z' to type 'DATETIME'

 

Any recommendations on how to handle two different date formats for the same field?

 

Best answer by Dimas CabrĆ© i ChacĆ³n

@mtaka Sorry. You are right. My suggestion would work if we used DatetTimeFormatter to make the datetime conversion, but currently we use SimpleDateFormat, which does not support optional formatting. Currently there is no direct method to accomplish this. A reasonable approach is stagging the conversion in two separate phases: normalization and conversion. You first normalize fields using processor ā€œField Replacerā€. Then, having assured all fields have the same format, you convert them to datetime using ā€œField Type Converterā€ processor. This is one replacement expression that would do the trick:

${str:length(record:value('/datetime')) > 20 ? record:value('/datetime') : str:concat(str:substring(record:value('/datetime'), 0, 19), '.000000Z')}

You will need to work the finer details if more input formats can appear.

I can send you a sample pipeline if you face some trouble configuring this.

View original
Did this topic help you find an answer to your question?

6 replies

Dimas CabrĆ© i ChacĆ³n
StreamSets Employee
Forum|alt.badge.img

Since Java 8, you can use optional formatter options to parse and output dates. Use the following format instead of the one you show: 

[yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'][yyyy-MM-dd'T'HH:mm:ss.SSSSS'Z'][yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'][yyyy-MM-dd'T'HH:mm:ss.SSS'Z'][yyyy-MM-dd'T'HH:mm:ss.SS'Z'][yyyy-MM-dd'T'HH:mm:ss.S'Z'][yyyy-MM-dd'T'HH:mm:ss'Z']

mtaka
Fan
  • Author
  • Fan
  • 2 replies
  • November 12, 2021

Sorry for the as I am a newbie in streamset.

So, under he Conversions tab in the Field Type Converter, I would create 2 ā€œsectionā€ to convert /createdate.  One section would have date format yyyy-MM-dd'T'HH:mm:ss.SSS'Z', and another that would have yyyy-MM-dd'T'HH:mm:ss'Z'

 


Dimas CabrĆ© i ChacĆ³n
StreamSets Employee
Forum|alt.badge.img

No. Just use ā€œOther Date Formatā€ and the specify as format (single long line):

[yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'][yyyy-MM-dd'T'HH:mm:ss.SSSSS'Z'][yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'][yyyy-MM-dd'T'HH:mm:ss.SSS'Z'][yyyy-MM-dd'T'HH:mm:ss.SS'Z'][yyyy-MM-dd'T'HH:mm:ss.S'Z'][yyyy-MM-dd'T'HH:mm:ss'Z']

This should work with one sinlgle section.


mtaka
Fan
  • Author
  • Fan
  • 2 replies
  • November 15, 2021

@Dimas CabrĆ© i ChacĆ³n it is still failing.

 

 


Dimas CabrĆ© i ChacĆ³n
StreamSets Employee
Forum|alt.badge.img

@mtaka Sorry. You are right. My suggestion would work if we used DatetTimeFormatter to make the datetime conversion, but currently we use SimpleDateFormat, which does not support optional formatting. Currently there is no direct method to accomplish this. A reasonable approach is stagging the conversion in two separate phases: normalization and conversion. You first normalize fields using processor ā€œField Replacerā€. Then, having assured all fields have the same format, you convert them to datetime using ā€œField Type Converterā€ processor. This is one replacement expression that would do the trick:

${str:length(record:value('/datetime')) > 20 ? record:value('/datetime') : str:concat(str:substring(record:value('/datetime'), 0, 19), '.000000Z')}

You will need to work the finer details if more input formats can appear.

I can send you a sample pipeline if you face some trouble configuring this.


swayam
Discovered Fame
Forum|alt.badge.img
  • Discovered Fame
  • 20 replies
  • November 18, 2021

@mtaka you may refer to 

This may help. If you like, don't forget to give a thumbs up.


Reply