Skip to main content
Acbx
Silver ⭐️
May 17, 2021
Question

NiFI: how can i convert string to date

Hello

 

I trying to put some data from my CSV to my database but i got some error.

 

Acbx_0-1621281201378.png

 

Settings UpdateRecord

 

Acbx_1-1621281273565.png

Settings about CSVRecordSetWriter

Acbx_2-1621281343928.png

 

My csv file:

RegionCountryItemTypeSalesChannelOrderPriorityOrderDateOrderIDShipDateUnitsSoldUnitPriceUnitCostTotalRevenueTotalCostTotalProfit
Sub-Saharan AfricaChadOffice SuppliesOnlineL1/27/201129249452302/12/20114484651.21524.962920025.642353920.64566105.00
EuropeLatviaBeveragesOnlineC12/28/20153618255491/23/2016107547.4531.7951008.7534174.2516834.50
Middle East and North AfricaPakistanVegetablesOfflineC1/13/201114151576702/01/20116515154.0690.931003700.90592408.95411291.95
Sub-Saharan AfricaDemocratic Republic of the CongoHouseholdOnlineC09/11/201250036400510/06/20127683668.27502.545134318.413861014.821273303.59
EuropeCzech RepublicBeveragesOnlineC10/27/201512748159112/05/2015349147.4531.79165647.95110978.8954669.06

2 replies

_mark_
Silver ⭐️⭐️
May 21, 2021

Hi,

 

Check your "Date Format" setting in the CSVReader service where you can specify a format string to interpreting text as a date.  At a guess either "M/dd/yyyy" or M/dd/yyyy" would work based on the data sample provided. 

 

More information on date format strings here: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

 

As an aside, I don't think the CSVRecordSetWriter settings are much use here as you're terminating all outbound PutDatabaseRecord relationships.

 

Hope that helps.

 

Translate
_mark_
Silver ⭐️⭐️
May 21, 2021

Hi,

 

I've just noticed your date strings in the sample aren't consistent which will make things difficult I think.

 

For example in line 1 your OrderDate format is "M/dd/yyyy" (single digit month) and in ship date it seems to be "MM/dd/yyyy" (double digit month).  For the CSV to work correctly all of your date fields would need to adhere to the same format I believe.

 

You can do a couple of things to resolve this:

 

  • Fix it in the source.  The best option in my opinion, if possible.
  • Clean the data in NiFi before it arrives at the PutDatabaseRecord processor.

Unfortunately the second is a touch beyond my current level of expertise, but if I was you I would explore either ReplaceText processor with the appropriately regex expression, or a an UpdateRecord processor with a SQL like statement to update this field in the flowfile - note though this would would require a separate CSVReader service that reads your data as strings.

 

Good luck.

Translate