简体   繁体   English

在 apache-nifi 中使用 executescript 处理器更新 csv 值失败

[英]Update csv value using executescript processor fails in apache-nifi

I try to read from a flowfile and update a record value using default value in csv.我尝试从流文件中读取并使用 csv 中的默认值更新记录值。 To that I have used ExecuteScript processor with following python code in it.为此,我使用了带有以下 python 代码的ExecuteScript处理器。

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()

My csv looks like我的 csv 看起来像

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

Result should be:结果应该是:

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

I get following error:我收到以下错误:

AttributeError: 'NoneType' object has no attribute 'getAttribute' in script at line number 13 AttributeError:“NoneType”object 在第 13 行的脚本中没有属性“getAttribute”

It says record = flowfile.getAttribute('record_type') this is wrong..它说record = flowfile.getAttribute('record_type')这是错误的..

I have no idea how to solve this as I am not good with python .我不知道如何解决这个问题,因为我不擅长python

that,s not python, but according to comment from author could be groovy.那不是 python,但根据作者的评论可能是 groovy。

use ExecuteGroovyScript processor with following code:使用带有以下代码的 ExecuteGroovyScript 处理器:

def ff=session.get()
if(!ff)return

def map = [
    '0': 'NEW_USER',
    '1': 'OLD_USER',
]

ff.write{rawIn, rawOut->
    rawOut.withWriter("UTF-8"){w->
        rawIn.withReader("UTF-8"){r->
            int rowNum = 0
            //iterate lines from input stream and split each with coma
            r.splitEachLine( ',' ){row->
                if(rowNum>0){
                    //if not a header line then substitute value using map
                    row[1] = map[ row[1] ] ?: 'IGNORE'
                }
                //join and write row to output writer
                w << row.join(',') << '\n'
                rowNum++
            }
        }
    }
}

REL_SUCCESS << ff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM