首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Groovy阅读FlowFile

使用Groovy阅读FlowFile
EN

Stack Overflow用户
提问于 2018-03-05 23:39:35
回答 1查看 2.6K关注 0票数 0

我尝试将Avro文件转换为SQL请求。我的文件是这样的:

代码语言:javascript
复制
{
  "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'"
  }

我的代码是:

代码语言:javascript
复制
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

我得到了这个错误:

代码语言:javascript
复制
ERROR nifi.processors.script.ExecuteScript ExecuteScript[id=e65b733e-0161-1000-45f0-3264d6fb51dd] ExecuteSc$ Possible solutions: getId(), find(), grep(), each(groovy.lang.Closure), find(groovy.lang.Closure), grep(java.lang.Object); rolling back session: {} org.apache.nifi.processor.exception.ProcessException: javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of m$ Possible solutions: getId(), find(), grep(), each(groovy.lang.Closure), find(groovy.lang.Closure), grep(java.lang.Object) 

有人能帮帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2018-03-06 00:06:51

这引用了一个来自another SO post的脚本,我在那里进行了注释,并在different forum上提供了答案,为了完整起见,我将其复制到这里:

变量createTable是一个GString,而不是一个Java String。这会导致调用Sql.execute(GString),它会将嵌入的表达式转换为参数,并且您不能将参数用于表名。请改用以下内容:

代码语言:javascript
复制
SQL.mydb.execute(createTable.toString())

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49114088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档