Search in sources :

Example 1 with UdfDescription

use of io.confluent.ksql.function.udf.UdfDescription in project ksql by confluentinc.

the class UdfLoader method loadUdfFromClass.

@VisibleForTesting
public void loadUdfFromClass(final Class<?> theClass, final String path) {
    final UdfDescription udfDescriptionAnnotation = theClass.getAnnotation(UdfDescription.class);
    if (udfDescriptionAnnotation == null) {
        throw new KsqlException(String.format("Cannot load class %s. Classes containing UDFs must" + "be annotated with @UdfDescription.", theClass.getName()));
    }
    final String functionName = udfDescriptionAnnotation.name();
    final String sensorName = "ksql-udf-" + functionName;
    @SuppressWarnings("unchecked") final Class<? extends Kudf> udfClass = metrics.map(m -> (Class) UdfMetricProducer.class).orElse(PluggableUdf.class);
    FunctionMetrics.initInvocationSensor(metrics, sensorName, "ksql-udf", functionName + " udf");
    final UdfFactory factory = new UdfFactory(udfClass, new UdfMetadata(udfDescriptionAnnotation.name(), udfDescriptionAnnotation.description(), udfDescriptionAnnotation.author(), udfDescriptionAnnotation.version(), udfDescriptionAnnotation.category(), path));
    functionRegistry.ensureFunctionFactory(factory);
    for (final Method method : theClass.getMethods()) {
        final Udf udfAnnotation = method.getAnnotation(Udf.class);
        if (udfAnnotation != null) {
            final KsqlScalarFunction function;
            try {
                function = createFunction(theClass, udfDescriptionAnnotation, udfAnnotation, method, path, sensorName, udfClass);
            } catch (final KsqlException e) {
                if (throwExceptionOnLoadFailure) {
                    throw e;
                } else {
                    LOGGER.warn("Failed to add UDF to the MetaStore. name={} method={}", udfDescriptionAnnotation.name(), method, e);
                    continue;
                }
            }
            factory.addFunction(function);
        }
    }
}
Also used : UdfDescription(io.confluent.ksql.function.udf.UdfDescription) Logger(org.slf4j.Logger) Kudf(io.confluent.ksql.function.udf.Kudf) Udf(io.confluent.ksql.function.udf.Udf) Configurable(org.apache.kafka.common.Configurable) Time(org.apache.kafka.common.utils.Time) FunctionName(io.confluent.ksql.name.FunctionName) ParamType(io.confluent.ksql.function.types.ParamType) LoggerFactory(org.slf4j.LoggerFactory) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Function(java.util.function.Function) Objects(java.util.Objects) UdfMetadata(io.confluent.ksql.function.udf.UdfMetadata) List(java.util.List) PluggableUdf(io.confluent.ksql.function.udf.PluggableUdf) Metrics(org.apache.kafka.common.metrics.Metrics) SqlTypeParser(io.confluent.ksql.schema.ksql.SqlTypeParser) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Method(java.lang.reflect.Method) Udf(io.confluent.ksql.function.udf.Udf) PluggableUdf(io.confluent.ksql.function.udf.PluggableUdf) Method(java.lang.reflect.Method) UdfDescription(io.confluent.ksql.function.udf.UdfDescription) KsqlException(io.confluent.ksql.util.KsqlException) UdfMetadata(io.confluent.ksql.function.udf.UdfMetadata) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ParamType (io.confluent.ksql.function.types.ParamType)1 Kudf (io.confluent.ksql.function.udf.Kudf)1 PluggableUdf (io.confluent.ksql.function.udf.PluggableUdf)1 Udf (io.confluent.ksql.function.udf.Udf)1 UdfDescription (io.confluent.ksql.function.udf.UdfDescription)1 UdfMetadata (io.confluent.ksql.function.udf.UdfMetadata)1 FunctionName (io.confluent.ksql.name.FunctionName)1 SqlTypeParser (io.confluent.ksql.schema.ksql.SqlTypeParser)1 KsqlConfig (io.confluent.ksql.util.KsqlConfig)1 KsqlException (io.confluent.ksql.util.KsqlException)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Configurable (org.apache.kafka.common.Configurable)1 Metrics (org.apache.kafka.common.metrics.Metrics)1 Time (org.apache.kafka.common.utils.Time)1 Logger (org.slf4j.Logger)1