简体   繁体   中英

Can not return a Struct from a KSQL UDF

I am trying to make a UDF function that accepts a STRUCT as a param and return a struct after doing some modifications on the input one (update existing field + adding new fields) like in the below code:

@UdfDescription(name = "myCustomUdf")
public class MyCustomUdf {

    @Udf(description = "do stuff")
    public Struct MyCustomUdf(@UdfParameter(schema = "struct <NAME VARCHAR, EMAIL VARCHAR>", value = "user") final Struct struct) {
        String processedEmail = struct.getString("EMAIL").toUpperCase();
        struct.put("EMAIL", processedEmail);
        return struct;
    }
}

However when deploying my custom jar, I see in the KSQL logs the below exception, can't find in the documentation anything referring to prohibiting this functionality, or am I missing something here:

Is it doable or not, to make UDF return STRUCT?

io.confluent.ksql.util.KsqlException: Could not load UDF method with signature: public org.apache.kafka.connect.data.Struct com.myudf.MyCustomUdf.MyCustomUdf(org.apache.kafka.connect.data.Struct)
    at io.confluent.ksql.function.UdfLoader.getReturnType(UdfLoader.java:373)
    at io.confluent.ksql.function.UdfLoader.addFunction(UdfLoader.java:280)
    at io.confluent.ksql.function.UdfLoader.lambda$handleUdfAnnotation$8(UdfLoader.java:217)
    at io.github.lukehutch.fastclasspathscanner.scanner.ScanSpec$9.lookForMatches(ScanSpec.java:1390)
    at io.github.lukehutch.fastclasspathscanner.scanner.ScanSpec.callMatchProcessors(ScanSpec.java:696)
    at io.github.lukehutch.fastclasspathscanner.FastClasspathScanner.scan(FastClasspathScanner.java:1606)
    at io.github.lukehutch.fastclasspathscanner.FastClasspathScanner.scan(FastClasspathScanner.java:1678)
    at io.github.lukehutch.fastclasspathscanner.FastClasspathScanner.scan(FastClasspathScanner.java:1704)
    at io.confluent.ksql.function.UdfLoader.loadUdfs(UdfLoader.java:145)
    at io.confluent.ksql.function.UdfLoader.lambda$load$2(UdfLoader.java:115)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
    at java.util.Iterator.forEachRemaining(Iterator.java:116)
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    at io.confluent.ksql.function.UdfLoader.load(UdfLoader.java:115)
    at io.confluent.ksql.rest.server.KsqlRestApplication.buildApplication(KsqlRestApplication.java:473)
    at io.confluent.ksql.rest.server.KsqlRestApplication.buildApplication(KsqlRestApplication.java:441)
    at io.confluent.ksql.rest.server.KsqlServerMain.createExecutable(KsqlServerMain.java:94)
    at io.confluent.ksql.rest.server.KsqlServerMain.main(KsqlServerMain.java:59)
Caused by: io.confluent.ksql.util.KsqlException: Type inference is not supported for: class org.apache.kafka.connect.data.Struct
    at io.confluent.ksql.util.SchemaUtil.handleParametrizedType(SchemaUtil.java:378)
    at io.confluent.ksql.util.SchemaUtil.lambda$getSchemaFromType$5(SchemaUtil.java:158)
    at io.confluent.ksql.util.SchemaUtil.getSchemaFromType(SchemaUtil.java:158)
    at io.confluent.ksql.util.SchemaUtil.getSchemaFromType(SchemaUtil.java:153)
    at io.confluent.ksql.function.UdfLoader.getReturnType(UdfLoader.java:366)
    ... 26 more

KSQL requires the schema not just for the input argument but also for the output. In your case, make sure to specify the following in your @Udf annotation:

    @Udf(description = "do stuff", schema="STRUCT<name VARCHAR, email VARCHAR>")
    public Struct MyCustomUdf(@UdfParameter(schema = "struct <NAME VARCHAR, EMAIL VARCHAR>", value = "user") final Struct struct) {
       ...
    }

It looks like we're indeed missing this from our documentation! I've added an issue to make sure that it's addressed ( https://github.com/confluentinc/ksql/issues/3699 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM