我通过传递avro模式,使用ConvertRecord处理器将csv转换为xml。由于NiFi 1.5中没有XMLRecordSetWriter,因此我正在使用ScriptedRecordSetWriter。我从Nifi github获得了示例xml writer groovy脚本
我想自定义xml,例如自定义记录标签,父标签和删除空标签。
输入CSV
MovieID,Year,Genre
1,2000,Action
2,1997,Action|Adventure
使用示例代码的当前输出
<record>
<MovieID>1</MovieID>
<Year>2000</Year>
<Genre>Action</Genre>
</record>
<record>
<MovieID>2</MovieID>
<Year>1997</Year>
<Genre>Action|Adventure</Genre>
</record>
预期产量
<MovieList>
<Movie>
<MovieID>1</MovieID>
<Year>2000</Year>
<Genre>Action</Genre>
</Movie>
<Movie>
<MovieID>2</MovieID>
<Year>1997</Year>
<Genre>Action|Adventure</Genre>
</Movie>
</MovieList>
这是来自nifi github的示例代码
import groovy.xml.MarkupBuilder
import org.apache.nifi.controller.AbstractControllerService
import org.apache.nifi.flowfile.FlowFile
import org.apache.nifi.logging.ComponentLog
import org.apache.nifi.schema.access.SchemaNotFoundException
import org.apache.nifi.serialization.RecordSetWriter
import org.apache.nifi.serialization.RecordSetWriterFactory
import org.apache.nifi.serialization.WriteResult
import org.apache.nifi.serialization.record.Record
import org.apache.nifi.serialization.record.RecordSchema
import org.apache.nifi.serialization.record.RecordSet
import org.apache.nifi.stream.io.NonCloseableOutputStream
class GroovyRecordSetWriter implements RecordSetWriter {
private int recordCount = 0;
private final OutputStream out;
public GroovyRecordSetWriter(final OutputStream out) {
this.out = out;
}
@Override
WriteResult write(Record r) throws IOException {
new OutputStreamWriter(new NonCloseableOutputStream(out)).with {osw ->
new MarkupBuilder(osw).record {
r.schema.fieldNames.each {fieldName ->
"$fieldName" r.getValue(fieldName)
}
}
}
recordCount++;
WriteResult.of(1, [:])
}
@Override
String getMimeType() {
return 'application/xml'
}
@Override
WriteResult write(final RecordSet rs) throws IOException {
int count = 0
new OutputStreamWriter(new NonCloseableOutputStream(out)).with {osw ->
new MarkupBuilder(osw).recordSet {
Record r
while (r = rs.next()) {
count++
record {
rs.schema.fieldNames.each {fieldName ->
"$fieldName" r.getValue(fieldName)
}
}
}
}
}
WriteResult.of(count, [:])
}
public void beginRecordSet() throws IOException {
}
@Override
public WriteResult finishRecordSet() throws IOException {
return WriteResult.of(recordCount, [:]);
}
@Override
public void close() throws IOException {
}
@Override
public void flush() throws IOException {
}
}
class GroovyRecordSetWriterFactory extends AbstractControllerService implements RecordSetWriterFactory {
@Override
RecordSchema getSchema(Map<String, String> variables, RecordSchema readSchema) throws SchemaNotFoundException, IOException {
return null
}
@Override
RecordSetWriter createWriter(ComponentLog logger, RecordSchema schema, OutputStream out) throws SchemaNotFoundException, IOException {
return new GroovyRecordSetWriter(out)
}
}
writer = new GroovyRecordSetWriterFactory()
我对groovy和nifi不熟悉。我搜索了实现相同但找不到任何示例的任何示例。任何帮助或指导表示赞赏。谢谢!