How to obtain first date of each month based on given year range in Nifi flow

青春壹個敷衍的年華 提交于 2020-05-17 07:18:06

问题


I would like to know what could be the best way to obtain the starting date values for each month based on the date range.

For example: If I am given a year range of 2015-11-10 and 2018-01-15(format YYYY-mm-dd). Then I would like to extract following dates:

2015-12-01
2016-01-01
.
.
2018-01-01

回答1:


You can try to use this flow for generating the first day of each month in the provided date range.

Overall flow

Step 1 Configuration: Start

Step 2 Configuration: Configure Date Range

Provide the start and end dates as configuration parameters via this step.

Step 3 Configuration: Generate First Dates For Months

This uses a Groovy script, which is provided below

Groovy script

flowFile = session.get();
if(!flowFile)
    return;

DATE_FORMAT = 'yyyy-MM-dd';
startDate = Date.parse(DATE_FORMAT, flowFile.getAttribute("start_date"));
endDate = Date.parse(DATE_FORMAT, flowFile.getAttribute("end_date"));
allFirstDates = "";

Calendar calendar = Calendar.getInstance();
Set firstDaysOfMonths = new LinkedHashSet();

for (int i = 0; i <= endDate-startDate; i++) {
    calendar.setTime(startDate.plus(i));
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    firstDayOfMonth = calendar.getTime();
    if (firstDayOfMonth.compareTo(startDate) >= 0) {
        firstDaysOfMonths.add(calendar.getTime().format(DATE_FORMAT));
    }
}

firstDaysOfMonths.each {
    firstDayOfMonth -> allFirstDates = allFirstDates + firstDayOfMonth + "\n";
}

flowFile = session.putAttribute(flowFile,"all_first_dates", allFirstDates );
session.transfer(flowFile,REL_SUCCESS)

Step 4 Configuration: View Result

Output of run:

When the flow is run, the attribute all_first_dates will be populated with the first dates of each month in the date range.



来源:https://stackoverflow.com/questions/53087834/how-to-obtain-first-date-of-each-month-based-on-given-year-range-in-nifi-flow

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