如何根据Nifi流量中的给定年份范围获取每个月的第一天 [英] How to obtain first date of each month based on given year range in Nifi flow

查看:135
本文介绍了如何根据Nifi流量中的给定年份范围获取每个月的第一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是根据日期范围获取每个月的起始日期值的最佳方法.

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.

例如:如果给定的年份范围为2015-11-10 and 2018-01-15(格式YYYY-mm-dd).然后,我想提取以下日期:

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

推荐答案

您可以尝试使用此流程在提供的日期范围内生成每月的第一天.

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

总体流量

第1步配置:开始

第2步配置:配置日期范围

通过此步骤提供开始日期和结束日期作为配置参数.

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

第3步配置:生成月份的第一个日期

这使用了Groovy脚本,下面提供了该脚本

This uses a Groovy script, which is provided below

Groovy脚本

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)

第4步配置:查看结果

运行输出:

运行流时,属性all_first_dates将填充日期范围内每个月的第一个日期.

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

这篇关于如何根据Nifi流量中的给定年份范围获取每个月的第一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆