0
votes

How do we convert TimeZone from say UTC to America/New_York in NiFi using JavaScript in ExecuteScript processor.

More information:

I know we can do it in UpdateRecord processor which I've achieved already. But the file is too big (20GB+) and I am doing some pre-processing in ExecuteScript processor, I don't want to re-iterate the entire file all over again. Currently, it is taking 18+ hours to process a single file. Implementing the timezone conversion in ExecuteScript, I think will significantly reduce the processing time.

Exact point where I am currently in:

I'm currently unable to import and create Java objects like SimpleDateFormat

The Java code that I am trying to implement in NiFi (by converting it to JavaScript) is as below:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class HelloWorld {
    public void convertDateTimeZone() throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        TimeZone utcZone = TimeZone.getTimeZone("UTC");
        String dateInString = "1982/01/10 10:20:56";

        sdf.setTimeZone(utcZone);
        Date utcDate = sdf.parse(dateInString); // Create a new Date object

        System.out.println(sdf.format(utcDate));

        SimpleDateFormat  nysdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        TimeZone newyorkZone = TimeZone.getTimeZone("America/New_York");
        nysdf.setTimeZone(newyorkZone);

        System.out.println(nysdf.format(utcDate));
    }
}
1
Do you have Javascript code for ExecuteScript that is not working? What error are you getting? - James
If you have java example and you know java then use groovy that is actually scripted java... Or check following examples how to call java classes from JavaScript in nifi: funnifi.blogspot.com/2016/03/… - daggett
@James, I've started to implement the above code in JavaScript. The JavaScript's Date class doesn't support TimeZone to be set explicitly. And I'm not able to create the Java object for SimpleDateFormat class in JavaScript. When I try to do that, I get the ParseException. - Mallik

1 Answers

0
votes

The sample code below shows how you can reference the Java SimpleDateFormat class from within ExecuteScript Javascript:

var flowFile = session.get();

if (flowFile !== null) {

    var StreamCallback = Java.type("org.apache.nifi.processor.io.StreamCallback");
    var IOUtils = Java.type("org.apache.commons.io.IOUtils");
    var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
    var SimpleDateFormat = Java.type("java.text.SimpleDateFormat")
    var TimeZone = Java.type("java.util.TimeZone")

    flowFile = session.write(flowFile, new StreamCallback(function(inputStream, outputStream) {

            var inputDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
            var utcZone = TimeZone.getTimeZone("UTC")
            inputDateFormat.setTimeZone(utcZone)

            var outputDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
            var outputZone = TimeZone.getTimeZone("America/New_York")
            outputDateFormat.setTimeZone(outputZone)

            var inputDateTime = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
            var utcDate = inputDateFormat.parse(inputDateTime)
            var outputDateTime = outputDateFormat.format(utcDate)
            IOUtils.write(outputDateTime, outputStream, StandardCharsets.UTF_8)
    }));

    session.transfer(flowFile, REL_SUCCESS);
}