在Apache-Nifi中使用executescript处理器更新csv值失败

时间:2019-11-14 05:53:15

标签: python csv apache-nifi

我尝试从流文件读取并使用csv中的默认值更新记录值。为此,我使用了ExecuteScript处理器和以下python代码。

import sys
import re
import traceback
from org.apache.commons.io import IOUtils
from org.apache.nifi.processor.io import StreamCallback
from org.python.core.util import StringUtil
from java.lang import Class
from java.io import BufferedReader
from java.io import InputStreamReader
from java.io import OutputStreamWriter

flowfile = session.get()
record = flowfile.getAttribute('record_type')

if record == '0':
    flowfile = session.putAttribute(flowfile,'record_type', 'NEW_USER')
    session.transfer(flowFile, REL_SUCCESS)
    session.commit()
elif record == '1':
    flowfile = session.putAttribute(flowfile,'record_type', 'OLD_USER')
    session.transfer(flowFile, REL_SUCCESS)
    session.commit()
else:
    flowfile = session.putAttribute(flowfile,'record_type', 'IGNORE')
    session.transfer(flowFile, REL_SUCCESS)
    session.commit()

writer.flush()
writer.close()
reader.close()

我的csv看起来像

id,record_type
1,0
2,1
3,2
4,0

结果应为:

id,record_type
1,NEW_USER
2,OLD_USER
3,IGNORE
4,NEW_USER

我收到以下错误消息:

  

AttributeError:“ NoneType”对象在其中没有属性“ getAttribute”   第13行的脚本

它说record = flowfile.getAttribute('record_type')这是错误的。.

我不知道如何解决这个问题,因为我对python不满意。

1 个答案:

答案 0 :(得分:2)

那不是python,但是根据作者的评论可能很古怪。

通过以下代码使用ExecuteGroovyScript处理器:

d <- data.table(a = rep(1:2, 2), b = 1:4)

d[, b := rep(Sys.Date(), .N), by = a][]
#    a     b
# 1: 1 18214
# 2: 2 18214
# 3: 1 18214
# 4: 2 18214
d[, b := Sys.Date()][]
#    a     b
# 1: 1 18214
# 2: 2 18214
# 3: 1 18214
# 4: 2 18214
d[, b := rep(Sys.Date(), .N)][]
#    a          b
# 1: 1 2019-11-14
# 2: 2 2019-11-14
# 3: 1 2019-11-14
# 4: 2 2019-11-14