Created on 03-05-2018 03:09 PM - edited 09-16-2022 05:56 AM
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)
Created 03-05-2018 03:28 PM
Help plz
Created 03-05-2018 04:03 PM
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.
Created 03-05-2018 04:26 PM
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
Created 03-05-2018 04:44 PM
What line gives that error? I tried the original script and just added the toString(), and it worked fine.
Created 03-05-2018 05:00 PM
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)
Created 03-05-2018 06:15 PM
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