How do I parse a date field and generate a date in string format in NiFi

情到浓时终转凉″ 提交于 2021-02-09 07:15:10

问题


Each of my flow file contains 2000 records. I would like to parse 01/01/2000 into a column year = 2000, column month = Jan and column day = 01

i.e. the input column 01/01/2000 into 3 values separated by commas 01,Jan,2000


回答1:


Lets say you have a schema like this for a person with a birthday and you want to split out the birthday:

{
  "name": "person",
  "namespace": "nifi",
  "type": "record",
  "fields": [
    { "name": "first_name", "type": "string" },
    { "name": "last_name", "type": "string" },
    { "name": "birthday", "type": "string" }
  ]
}

You would need to modify the schema so it had the fields you want to add:

{
  "name": "person",
  "namespace": "nifi",
  "type": "record",
  "fields": [
    { "name": "first_name", "type": "string" },
    { "name": "last_name", "type": "string" },
    { "name": "birthday", "type": "string" },
    { "name": "birthday_year", "type": ["null", "string"] },
    { "name": "birthday_month", "type": ["null", "string"] },
    { "name": "birthday_day", "type": ["null", "string"] }
  ]
}

Lets say the input record has the following text:

bryan,bende,1980-01-01

We can use UpdateRecord with a CsvReader and CsvWriter, and UpdateRecord can populate the three fields we want by parsing the original birthday field.

If we send the output to LogAttribute we should see the following now:

first_name,last_name,birthday,birthday_year,birthday_month,birthday_day
bryan,bende,1980-01-01,1980,01,01

Here is the link to the record path guide for details on the toDate and format functions:

https://nifi.apache.org/docs/nifi-docs/html/record-path-guide.html




回答2:


You can use UpdateRecord for this, assuming your input record has the date column called "myDate", you'd set the Replacement Value Strategy to Record Path Value, and your user-defined properties might look something like:

/day format(/myDate, "dd") /month format(/myDate, "MMM") /year format(/myDate, "YYYY")

Your output schema would look like this:

{
 "namespace": "nifi",
 "name": "myRecord",
 "type": "record",
 "fields": [
  {"name": "day","type": "int"},
  {"name": "month","type": "string"},
  {"name": "year","type": "int"}
 ]
}


来源:https://stackoverflow.com/questions/51675050/how-do-i-parse-a-date-field-and-generate-a-date-in-string-format-in-nifi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!