1
votes

I am currently trying to get list of all dates in a flowfile between the two dates specified using ExecuteScript. But I am somehow getting empty attribute.

Following is my Groovy code of ExecuteScript for the specified startdate and enddate variable specified:

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

DATE_FORMAT = 'dd-MM-yyyy';
startDate = Date.parse(DATE_FORMAT, flowFile.getAttribute("startdate"));
endDate = Date.parse(DATE_FORMAT, flowFile.getAttribute("enddate"));
allDates = "";

Calendar calendar = Calendar.getInstance();
Set allDates = new LinkedHashSet();
numbers = TimeUnit.MILLISECONDS.toDays(Math.abs(endDate - startDate))


for (int i = 1; i <= numbers; i++) {
calendar.setTime( startDate );
calendar.add( Calendar.DATE, i );
}

days.each {
    day -> allDates = allDates + day + "\n";
}

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

On my outgoing queue I find the attribute allDates is empty String

What is going wrong with my code?

1

1 Answers

2
votes

you have some problems in your code

for example the variable allDates declared twice i two different scopes:

global (without type or def)

allDates = "";

and local (with type)

Set allDates = new LinkedHashSet();

after that it's hard to predict which one is used

and actually code could be easier in groovy:

def DATE_FORMAT = 'dd-MM-yyyy';
def startDate = Date.parse(DATE_FORMAT, '01-11-1970');
def endDate = Date.parse(DATE_FORMAT, '09-11-1970');
def allDates = ""

for (def d = startDate; d<=endDate; d++){
    allDates+=d.format(DATE_FORMAT)+"\n"
}
println allDates

note that this is runable code so you can use groovyconsole or any IDE to debug it before integrating into nifi

of cause you have to wrap it with flow file handling before using in nifi