Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Read a flowfile with groovy error

avatar
Contributor

I'm using groovy in Execute processor to read an avro schema to create a sql table. My code is :

import groovy.json.JsonSlurper
def ff = session.get()
if(!ff)return
def schema = ff.read().withReader("UTF-8"){ new JsonSlurper().parse(it) }
def typeMap = [
    "string"            : "varchar(255)",
    "long"              : "numeric(10)",
    [ "null", "string" ]: "varchar(255)",
    [ "null", "long" ]  : "numeric(10)",
]
def createTable = "create table ${schema.name} (" +
    schema.fields.collect{ "\n  ${it.name.padRight(39)} ${typeMap[it.type]}" }.join(',') +
    "\n)"
SQL.mydb.execute(createTable)
REL_SUCCESS << ff

And I got this error can someone help me plz

ERROR [Timer-Driven Process Thread-3] o.a.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) at org.apache.nifi.processors.script.ExecuteScript.onTrigger(ExecuteScript.java:230) at org.apache.nifi.controller.StandardProcessorNode.onTrigger(StandardProcessorNode.java:1119) at org.apache.nifi.controller.tasks.ContinuallyRunProcessorTask.call(ContinuallyRunProcessorTask.java:147) at org.apache.nifi.controller.tasks.ContinuallyRunProcessorTask.call(ContinuallyRunProcessorTask.java:47) at org.apache.nifi.controller.scheduling.TimerDrivenSchedulingAgent$1.run(TimerDrivenSchedulingAgent.java:128) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.apache.nifi.controller.repositor$ Possible solutions: getId(), find(), grep(), each(groovy.lang.Closure), find(groovy.lang.Closure), grep(java.lang.Object)

6 REPLIES 6

avatar
Contributor
@Matt Burgess

Help plz

avatar
Master Guru

Looks like you are referring to the script provided in this SO post. The variable createTable is a GString, not a Java String. This causes invocation of Sql.execute(GString), which converts the embedded expressions into parameters, and you can't use a parameter for a table name. Use the following instead:

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

This will cause the invocation of Sql.execute(String), which does not try to parameterize the statement.

avatar
Contributor

Thank you for your response. Actually it's my post.

I used SQL.mydb.execute(createTable.toString()) but I got the same error which due to this instruction

def schema = ff.read().withReader("UTF-8"){newJsonSlurper().parse(it). 

So I changed the code

import groovy.json.JsonSlurper
import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
def ff = session.get()
if(!ff)return
def schema = ''
session.read(ff, {inputStream ->
  schema= IOUtils.toString(inputStream, StandardCharsets.UTF_8)
} as InputStreamCallback)
//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.toString())
//transfer flow file to success
REL_SUCCESS << ff
error : groovy.lang.missingpropertyexception no such property for class java.lang.string

avatar
Master Guru

What line gives that error? I tried the original script and just added the toString(), and it worked fine.

avatar
Contributor

Not for me 😞

def createTable = "create table ${schema.name} (" +
    schema.fields.collect{ "\n  ${it.name.padRight(39)} ${typeMap[it.type]}" }.join(',') +
    "\n)"

No such a no such property : name for class : java.lang.string

And i Used this to escape the first error : def schema = ''

session.read(ff, {inputStream ->
  schema= IOUtils.toString(inputStream, StandardCharsets.UTF_8)
} as InputStreamCallback)

avatar
Contributor

My file extension is avro i can't use def schema = ff.read().withReader("UTF-8"){newJsonSlurper().parse(it)}

I have to use each and then parse schema. Do you an example plz @Matt Burgess