Запись в CSV в Apache NIFi
У меня есть процессор ExecuteScript, который обрабатывает входящий флоуфайл. Необходимо записать данные из него в формат 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
from org.python.core.util import StringUtil
from org.apache.nifi.distributed.cache.client import DistributedMapCacheClient, Serializer, Deserializer
import json
import os
import time, calendar
import re
from datetime import datetime
from datetime import timedelta
from io import StringIO
import csv
cacheContent = ""
tagid_value = ""
cachePointsKey_value = ""
tag = ""
class ModJSON(StreamCallback):
def __init__(self):
pass
def process(self, inputStream, outputStream):
inputText = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
pointsCache = json.loads(cacheContent)
tagName = tagid_value
if tagid_value in pointsCache:
tagName = pointsCache[tagid_value]['name']
global tag
tag = tagName
outList=["Date;Tagname;Value"]
f = StringIO(inputText)
reader = csv.reader(f, delimiter=';',)
next(reader, None) # skip the headers
for row in reader:
ts = datetime.strptime(row[0], "%d.%m.%Y %H:%M:%S.%f")
value = row[1]
outList.append("%s;%s;%s" % (ts.strftime("%d.%m.%Y %H:%M:%S.%f")[:-3],tagName,value))
f.close()
text = '\n'.join(outList)
outputStream.write(text)
# Define a subclass of Serializer for use in the client's get() method
class StringSerializer(Serializer):
def __init__(self):
pass
def serialize(self, value, out):
out.write(value)
# Define a subclass of Deserializer for use in the client's get() method
class StringDeserializer(Deserializer):
def __init__(self):
pass
def deserialize(self, bytes):
return StringUtil.fromBytes(bytes)
flowFile = session.get()
if (flowFile is not None):
cachePointsKey_value = cachePointsKey.evaluateAttributeExpressions().getValue()
tagid_value = tagid.evaluateAttributeExpressions(flowFile).getValue()
cacheClient = cacheServiceId.asControllerService(DistributedMapCacheClient)
cacheContent = cacheClient.get(cachePointsKey_value, StringSerializer(), StringDeserializer())
flowFile = session.write(flowFile, ModJSON())
if (flowFile is not None):
flowFile = session.putAttribute(flowFile,'tagname',tag)
session.transfer(flowFile, REL_SUCCESS)
session.commit()
На выходе ExecuteScript выдает следующее:
Date;Tagname;Value
В чем может быть дело?