Nifi Groovy 脚本修改 csv 行给出 MissingMethodException 错误
Nifi Groovy script to modify csv rows giving MissingMethodException error
我正在编写一个 groovy 脚本,它将以下 csv 文件作为输入
Id, Name, Class
1, Kevin,[Eight, Nine]
2, Mark,Four
并按如下方式创建或更改 csv
Id, Name, Class
1, Kevin,Eight
1, Kevin,Nine
2, Mark,Four
基本上,如果列 Class 具有字符串数组,则将其放在多行中以复制所有其他列值。
def flowFile = session.get()
if(!flowFile) return
try {
flowFile = session.write(flowFile, {inputStream, outputStream ->
outputStream.withWriter("UTF-8"){ w ->
inputStream.eachLine("UTF-8"){ line ->
def subString = line.takeBetween('[', ']')
def splitArray = subString.split(',')
if(splitArray.length > 1) {
def lineBefore = line.takeBefore('[');
def lineAfter = line.takeAfter(']');
for(int i=0;i<splitArray.length;i++) {
w << lineBefore << row.getAt(i) << lineAfter << '\n'
}
}else {
w << line << '\n'
}
}
}
} as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)
}catch(e) {
log.error('Error capturing nextLink', e)
session.transfer(flowFile, REL_FAILURE)
}
我收到以下错误:
Error capturing nextLink: groovy.lang.MissingMethodException: No signature of method: java.lang.String.takeBetween() is applicable for argument types: (String, String) values: [[, ]]
但是 def subString = line.takeBetween("[", "]")
中的 takeBetween 方法在 eclipse
中工作得很好
它在 Eclipse 中运行但不在服务器上运行的事实可能表明服务器上的 groovy 版本早于 3.0(groovy 中引入的 takeBetween
方法3.0).
/**
* A String variant of the equivalent CharSequence method {@link #takeBetween(CharSequence,CharSequence,CharSequence)}.
*
* @param self the original CharSequence
* @param from beginning of search
* @param to end of search
* @return String that is in between the given two CharSequences and empty if the unavailable inputs are given
*
* @since 3.0.0
*/
public static String takeBetween(final String self, final CharSequence from, final CharSequence to) {
return (String) takeBetween((CharSequence) self, from, to);
}
以下使用 'substring' 方法而不是 'takeBetween' 方法的 groovy 脚本帮助我创建了所需格式的 csv
def flowFile = session.get()
if(!flowFile) return
try {
flowFile = session.write(flowFile, {inputStream, outputStream ->
outputStream.withWriter("UTF-8"){ w ->
inputStream.eachLine("UTF-8"){ line ->
def splitArray = new String[0];
def subString = "";
def x = line.indexOf("[")+1;
def y = line.indexOf("]");
if(x > 0 && y >0)
subString = line.substring(x,y);
if(subString != null && subString.length() >0)
splitArray = subString.split(',')
if(splitArray.length > 1) {
def lineBefore = line.substring(0,x);
def lineAfter = line.substring(y,line.length());
for(int i=0;i<splitArray.length;i++) {
w << lineBefore << splitArray.getAt(i) << lineAfter << '\n'
}
}else {
w << line << '\n'
}
}
}
} as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)
}catch(e) {
log.error('Error capturing nextLink', e)
session.transfer(flowFile, REL_FAILURE)
}
我正在编写一个 groovy 脚本,它将以下 csv 文件作为输入
Id, Name, Class
1, Kevin,[Eight, Nine]
2, Mark,Four
并按如下方式创建或更改 csv
Id, Name, Class
1, Kevin,Eight
1, Kevin,Nine
2, Mark,Four
基本上,如果列 Class 具有字符串数组,则将其放在多行中以复制所有其他列值。
def flowFile = session.get()
if(!flowFile) return
try {
flowFile = session.write(flowFile, {inputStream, outputStream ->
outputStream.withWriter("UTF-8"){ w ->
inputStream.eachLine("UTF-8"){ line ->
def subString = line.takeBetween('[', ']')
def splitArray = subString.split(',')
if(splitArray.length > 1) {
def lineBefore = line.takeBefore('[');
def lineAfter = line.takeAfter(']');
for(int i=0;i<splitArray.length;i++) {
w << lineBefore << row.getAt(i) << lineAfter << '\n'
}
}else {
w << line << '\n'
}
}
}
} as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)
}catch(e) {
log.error('Error capturing nextLink', e)
session.transfer(flowFile, REL_FAILURE)
}
我收到以下错误:
Error capturing nextLink: groovy.lang.MissingMethodException: No signature of method: java.lang.String.takeBetween() is applicable for argument types: (String, String) values: [[, ]]
但是 def subString = line.takeBetween("[", "]")
中的 takeBetween 方法在 eclipse
它在 Eclipse 中运行但不在服务器上运行的事实可能表明服务器上的 groovy 版本早于 3.0(groovy 中引入的 takeBetween
方法3.0).
/**
* A String variant of the equivalent CharSequence method {@link #takeBetween(CharSequence,CharSequence,CharSequence)}.
*
* @param self the original CharSequence
* @param from beginning of search
* @param to end of search
* @return String that is in between the given two CharSequences and empty if the unavailable inputs are given
*
* @since 3.0.0
*/
public static String takeBetween(final String self, final CharSequence from, final CharSequence to) {
return (String) takeBetween((CharSequence) self, from, to);
}
以下使用 'substring' 方法而不是 'takeBetween' 方法的 groovy 脚本帮助我创建了所需格式的 csv
def flowFile = session.get()
if(!flowFile) return
try {
flowFile = session.write(flowFile, {inputStream, outputStream ->
outputStream.withWriter("UTF-8"){ w ->
inputStream.eachLine("UTF-8"){ line ->
def splitArray = new String[0];
def subString = "";
def x = line.indexOf("[")+1;
def y = line.indexOf("]");
if(x > 0 && y >0)
subString = line.substring(x,y);
if(subString != null && subString.length() >0)
splitArray = subString.split(',')
if(splitArray.length > 1) {
def lineBefore = line.substring(0,x);
def lineAfter = line.substring(y,line.length());
for(int i=0;i<splitArray.length;i++) {
w << lineBefore << splitArray.getAt(i) << lineAfter << '\n'
}
}else {
w << line << '\n'
}
}
}
} as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)
}catch(e) {
log.error('Error capturing nextLink', e)
session.transfer(flowFile, REL_FAILURE)
}