Created 05-28-2020 05:35 PM
I have a csv flowfile with single record. I need to create its file name based on couple of column values in the csv file. Can you please let me know how we can do it by using the column name only not the position of the column as column position may change. Example
CSV File
Name , City, State, Country, Gender
John, Dallas, Texas, USA, M
File name should be John_USA.csv
Created on 05-29-2020 12:54 AM - edited 05-29-2020 12:58 AM
Hey,
This is the one option using Python script -
Just use ExecuteScript processor and use below script as script body -
(I am not Python expert, there should be better way to write below code, but this works)
import csv
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
global filename
class PyStreamCallback(StreamCallback):
def __init__(self):
pass
def process(self, inputStream, outputStream):
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
lines = text.splitlines()
reader = csv.reader(lines)
headerList = list(next(reader))
dataList = list(next(reader))
global filename
filename = dataList[headerList.index('Name')] + "_" + dataList[headerList.index('Country')] + ".csv"
outputStream.write(bytearray(text.encode('utf-8')))
flowFile = session.get()
if (flowFile != None):
flowFile = session.write(flowFile, PyStreamCallback())
flowFile = session.putAttribute(flowFile, "filename", filename)
session.transfer(flowFile, REL_SUCCESS)
Thanks
Mahendra
Created 05-29-2020 01:03 AM
Hi,
This is one option using simple Python script -
(I am not python expert, there should be better way to write below code, but this works)
Just use ExecuteScript processor and add below code as script body -
import csv
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
global filename
class PyStreamCallback(StreamCallback):
def __init__(self):
pass
def process(self, inputStream, outputStream):
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
lines = text.splitlines()
reader = csv.reader(lines)
headerList = list(next(reader))
dataList = list(next(reader))
global filename
filename = dataList[headerList.index('Name')] + "_" + dataList[headerList.index('Country')] + ".csv"
outputStream.write(bytearray(text.encode('utf-8')))
flowFile = session.get()
if (flowFile != None):
flowFile = session.write(flowFile, PyStreamCallback())
flowFile = session.putAttribute(flowFile, "filename", filename)
session.transfer(flowFile, REL_SUCCESS)
Thanks
Mahendra
Created 06-05-2020 08:23 AM
@vikrant_kumar24 - Have you found a better way to solve this ? If so would be curious to hear that!