You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@nifi.apache.org by Tomislav Novosel <to...@gmail.com> on 2019/01/29 14:04:13 UTC

Modify Flowfile attributes

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 json
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
from 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()

Re: Modify Flowfile attributes

Posted by Jerry Vinokurov <gr...@gmail.com>.
You can do this using the putAttribute function of the flowfile. You're
using getAttribute to get the filename and you can use putAttribute to set
other attributes of the flowfile before transferring it.

On Tue, Jan 29, 2019 at 9:04 AM Tomislav Novosel <to...@gmail.com>
wrote:

> 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 json
> import java.io
> from org.apache.commons.io import IOUtils
> from java.nio.charset import StandardCharsets
> from org.apache.nifi.processor.io import StreamCallback
> from 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()
>


-- 
http://www.google.com/profiles/grapesmoker