Since you're script shows that "filename" is an attribute of your flowfile, you
could use the UpdateAttribute processor.
If you right click on UpdateAttribute and choose ShowUsage, then choose
Expression Language Guide, it shows you the things you can handle.
Something along the lines of ${filename:getDelimitedField(6,'_')}, if I
understand the Groovy code correctly. I did a GenerateFlowFIle to an
UpdateAttribute processor setting filename to "1_2_3_4_5_6.2_abc", then sent
that to another UpdateAttribute with the getDelimitedField() I listed and I
received 6.2. Then another UpdateAttribute could parse the 6.2 for the second
substring, or you might be able to chain them in the existing UpdateProcessor.
--------------------------------------------
On Tue, 1/29/19, Tomislav Novosel <[email protected]> wrote:
Subject: Modify Flowfile attributes
To: [email protected]
Date: Tuesday, January 29, 2019, 9:04 AM
Hi all,
I'm trying to calculate week number and date
from filename using ExecuteScript processor and Jython. Here
is python script.How can I add calculated
attributes week and year to flowfile?
Please help, thank you.Tom
P.S. Maybe I completely missed with this script.
Feel free to correct me.
import
jsonimport java.iofrom org.apache.commons.io import
IOUtilsfrom java.nio.charset import
StandardCharsetsfrom org.apache.nifi.processor.io import
StreamCallbackfrom datetime import datetime, timedelta, date
class PyStreamCallback(StreamCallback):
def __init__(self, flowfile):
self.ff = flowfile
pass
def process(self, inputStream, outputStream):
file_name =
self.ff.getAttribute("filename")
date_file =
file_name.split("_")[6]
date_final =
date_file.split(".")[0]
date_obj =
datetime.strptime(date_final,'%y%m%d')
date_year =
date_obj.year
date_day =
date_obj.day
date_month =
date_obj.month
week = date(year=date_year, month=date_month,
day=date_day).isocalendar()[1]
year =
date(year=date_year, month=date_month, day=date_day).isocalendar()[0]
flowFile =
session.get()if (flowFile != None):
session.transfer(flowFile, REL_SUCCESS)
session.commit()