Jeg kan foreslå ExecuteGroovyScript
processor i nifi v1.5+
definere ny egenskab SQL.mydb
- du bliver bedt om at linke dens værdi til en database (DBCPConnectionPool
)
vælg den database, hvor du vil oprette en tabel
og brug dette script (antag at avro-skemaet er i flowfilens indhold)
import groovy.json.JsonSlurper
def ff = session.get()
if(!ff)return
//parse avro schema from flow file content
def schema = ff.read().withReader("UTF-8"){ new JsonSlurper().parse(it) }
//define type mapping
def typeMap = [
"string" : "varchar(255)",
"long" : "numeric(10)",
[ "null", "string" ]: "varchar(255)",
[ "null", "long" ] : "numeric(10)",
]
assert schema.name && schema.name=~/^\w.*/
//build create table statement
def createTable = "create table ${schema.name} (" +
schema.fields.collect{ "\n ${it.name.padRight(39)} ${typeMap[it.type]}" }.join(',') +
"\n)"
//execute statement through the custom defined property
//SQL.mydb references http://docs.groovy-lang.org/2.4.10/html/api/groovy/sql/Sql.html object
SQL.mydb.execute(createTable as String) //important to cast to String
//transfer flow file to success
REL_SUCCESS << ff