Json 使用Groovy Nifi读取流文件

Json 使用Groovy Nifi读取流文件,json,groovy,apache-nifi,Json,Groovy,Apache Nifi,我尝试将Avro文件转换为SQL请求。我的文件如下: { "type" : "record", "name" : "warranty", "doc" : "Schema generated by Kite", "fields" : [ { "name" : "id", "type" : "long", "doc" : "Type inferred from '1'" }, { "name" : "train_id", "type" :

我尝试将Avro文件转换为SQL请求。我的文件如下:

{
  "type" : "record",
  "name" : "warranty",
  "doc" : "Schema generated by Kite",
  "fields" : [ {
    "name" : "id",
    "type" : "long",
    "doc" : "Type inferred from '1'"
  }, {
    "name" : "train_id",
    "type" : "long",
    "doc" : "Type inferred from '21691'"
  }, {
    "name" : "siemens_nr",
    "type" : "string",
    "doc" : "Type inferred from 'Loco-001'"
  }, {
    "name" : "uic_nr",
    "type" : "long",
    "doc" : "Type inferred from '193901'"
  }, {
    "name" : "Configuration",
    "type" : "string",
    "doc" : "Type inferred from 'ZP28'"
  }, {
    "name" : "Warranty_Status",
    "type" : "string",
    "doc" : "Type inferred from 'Out_of_Warranty'"
  }, {
    "name" : "Warranty_Data_Type",
    "type" : "string",
    "doc" : "Type inferred from 'Real_based_on_preliminary_acceptance_date'"
  }
我的代码是:

import groovy.json.JsonSlurper

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

//parse afro 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)",
]
//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)

//transfer flow file to success
REL_SUCCESS << ff

有人能帮我一下吗?plz

这引用了一个脚本,我在那里发表了评论,并提供了一个关于的答案,为了完整起见,我将复制到这里:

变量createTable是GString,而不是Java字符串。这会导致调用Sql.execute(GString),将嵌入的表达式转换为参数,并且不能将参数用作表名。请改用以下方法:

SQL.mydb.execute(createTable.toString())
这将导致调用Sql.execute(String),它不会尝试参数化语句

SQL.mydb.execute(createTable.toString())