如何将上个月的数据插入数据库Nifi? [英] How to insert previous month of data into database Nifi?

查看:115
本文介绍了如何将上个月的数据插入数据库Nifi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据需要比较上个月的数据(如果是上个月的话),否则应该插入.

I have data in which i need to compare month of data if it is previous month then it should be insert otherwise not.

示例:

23.12.2016 12:02:23,Koji,24
22.01.2016 01:21:22,Mahi,24

现在我需要获取第一列数据(23.12.2016 12:02:23),然后再获取月份(12).

Now i need to get first column of data (23.12.2016 12:02:23) and then get month (12) on it.

与当月之前的类似,

If current month is 'JAN_2017',then get before of 'JAN_2017' it should be 'Dec_2016'

对于第一行,

将此[Dec_2016] [前一个月]与月份的数据'Dec_2016'[23.12.2016]进行比较.

匹配,然后插入数据库.

It matched then insert into database.

我已经尝试过您的建议.

i have already tried with your suggestions.

"UpdateAttribute to add a new attribute with the previous month value, and then RouteOnAttribute to determine if the flowfile should be inserted "

我在RouteOnAttribute中使用了以下表达语言,

i have used below expression language in RouteOnAttribute,

${literal('Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'):getDelimitedField(${csv.1:toDate('dd.MM.yyyy hh:mm:ss'):format('MM')}):equals(${literal('Dec,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov'):getDelimitedField(${now():toDate(' Z MM dd HH:mm:ss.SSS yyyy'):format('MM'):toNumber()})})}

在以下数据中可能会失败.

it could be failed in below data.,

23.12.2015,Andy,21
23.12.2017,Present,32

我的数据可能包含过去的几年和将来的几年

My data may contains some past years and future years

它与我插入的表达式匹配.

It matches with my expression it also inserted.

我需要检查数据中的月份和年份.

I need to check month with year in data.

我如何检查?

推荐答案

最简单的答案是使用具有简单日期逻辑的 ExecuteScript 处理器(这将允许您使用Groovy/Java日期框架正确处理leap年,时区等问题).

The easiest answer is to use the ExecuteScript processor with simple date logic (this will allow you to use the Groovy/Java date framework to correctly handle things like leap years, time zones, etc.).

如果您真的不想这样做,则可以在 UpdateAttribute 中使用正则表达式和表达式语言来添加具有上个月值的新属性,然后再添加 RouteOnAttribute 确定是否应将流文件插入数据库中.

If you really don't want to do that, you could probably use a regex and Expression Language in UpdateAttribute to add a new attribute with the previous month value, and then RouteOnAttribute to determine if the flowfile should be inserted into the database.

这是一个简单的Groovy测试,演示了逻辑.您需要添加代码来处理会话,流文件等.

Here's a simple Groovy test demonstrating the logic. You'll need to add the code to process the session, flowfile, etc.

@Test
public void textScriptShouldFindPreviousMonth() throws Exception {
    // Arrange
    def input = ["23.12.2016 12:02:23,Koji,24", "22.01.2016 01:21:22,Mahi,24"]
    def EXPECTED = ["NOV_2016", "DEC_2015"]

    // Act
    input.eachWithIndex { String data, int i ->
        Calendar calendar = Date.parse("dd.MM.yyyy", data.tokenize(" ")[0]).toCalendar()
        calendar.add(Calendar.MONTH, -1)
        String result = calendar.format("MMM_yyyy").toUpperCase()

        // Assert
        assert result == EXPECTED[i]
    }
}

这篇关于如何将上个月的数据插入数据库Nifi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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