Search code examples
apache-nifi

Nifi convertRecord CSV to JSON truncate number values


I have the following CSV file in entry and I convert CSV to JSON using a convertRecord with csvReader and JsonRecordSetWriter

key,x,y,latitude,longitude
123,722052.172555174,6555555.17858555,42.0422004518503,2.21755344237117

but my float values are truncated

{"key":123,"x":722052.2,"y":6555555.0,"latitude":42.042202,"longitude":2.2175534}

How to get them all without truncating them ?


Solution

  • It's possible to achieve that with an explicit schema (CSV Reader service):

    • Schema Access Strategy: Use 'Schema Text' Property
    • Schema Text:
    {
      "type" : "record",
      "name" : "MyClass",
      "fields" : [ {
        "name" : "key",
        "type" : "long"
      }, {
        "name" : "x",
        "type" : "double"
      }, {
        "name" : "y",
        "type" : "double"
      }, {
        "name" : "latitude",
        "type" : "double"
      }, {
        "name" : "longitude",
        "type" : "double"
      } ]
    }
    

    Output JSON with explicit schema:

    {
      "key" : 123,
      "x" : 722052.172555174,
      "y" : 6555555.17858555,
      "latitude" : 42.0422004518503,
      "longitude" : 2.21755344237117
    }