标签:
.
import org.apache.commons.io.IOUtils
import java.nio.charset.*
import java.text.SimpleDateFormat
import groovy.json.*
def flowFile = session.get()
flowFile = session.write(flowFile, {inputStream, outputStream ->
def js = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
def data = new JsonSlurper().parseText( js )
def columns = data.data*.keySet().flatten().unique()
// Wrap strings in double quotes, and remove nulls
def encode = { e -> e == null ? ‘‘ : e instanceof String ? /"$e"/ : "$e" }
// Print all the column names
def columnName = columns.collect { c -> encode( c ) }.join( ‘,‘ )
// Then create all the rows
def columnData = data.data.collect { row ->
// A row at a time
columns.collect { colName -> encode( row[ colName ] ) }.join( ‘,‘ )
}.join( ‘\n‘ )
StringBuilder cd = new StringBuilder()
cd.append(columnName + "\n")
cd.append(columnData)
outputStream.write(cd.toString().getBytes(StandardCharsets.UTF_8))
}as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)
标签:
原文地址:http://www.cnblogs.com/fengwenit/p/5598606.html