Search in sources :

Example 1 with UdtfDescription

use of io.confluent.ksql.function.udtf.UdtfDescription in project ksql by confluentinc.

the class UdtfLoader method loadUdtfFromClass.

public void loadUdtfFromClass(final Class<?> theClass, final String path) {
    final UdtfDescription udtfDescriptionAnnotation = theClass.getAnnotation(UdtfDescription.class);
    if (udtfDescriptionAnnotation == null) {
        throw new KsqlException(String.format("Cannot load class %s. Classes containing UDTFs must" + "be annotated with @UdtfDescription.", theClass.getName()));
    }
    final String functionName = udtfDescriptionAnnotation.name();
    final String sensorName = "ksql-udtf-" + functionName;
    FunctionMetrics.initInvocationSensor(metrics, sensorName, "ksql-udtf", functionName + " udtf");
    final UdfMetadata metadata = new UdfMetadata(udtfDescriptionAnnotation.name(), udtfDescriptionAnnotation.description(), udtfDescriptionAnnotation.author(), udtfDescriptionAnnotation.version(), udtfDescriptionAnnotation.category(), path);
    final TableFunctionFactory factory = new TableFunctionFactory(metadata);
    for (final Method method : theClass.getMethods()) {
        if (method.getAnnotation(Udtf.class) != null) {
            final Udtf annotation = method.getAnnotation(Udtf.class);
            try {
                if (method.getReturnType() != List.class) {
                    throw new KsqlException(String.format("UDTF functions must return a List. Class %s Method %s", theClass.getName(), method.getName()));
                }
                final Type ret = method.getGenericReturnType();
                if (!(ret instanceof ParameterizedType)) {
                    throw new KsqlException(String.format("UDTF functions must return a parameterized List. Class %s Method %s", theClass.getName(), method.getName()));
                }
                final Type typeArg = ((ParameterizedType) ret).getActualTypeArguments()[0];
                final ParamType returnType = FunctionLoaderUtils.getReturnType(method, typeArg, annotation.schema(), typeParser);
                final List<ParameterInfo> parameters = FunctionLoaderUtils.createParameters(method, functionName, typeParser);
                final KsqlTableFunction tableFunction = createTableFunction(method, FunctionName.of(functionName), returnType, parameters, annotation.description(), annotation);
                factory.addFunction(tableFunction);
            } catch (final KsqlException e) {
                if (throwExceptionOnLoadFailure) {
                    throw e;
                } else {
                    LOGGER.warn("Failed to add UDTF to the MetaStore. name={} method={}", udtfDescriptionAnnotation.name(), method, e);
                }
            }
        }
    }
    functionRegistry.addTableFunctionFactory(factory);
}
Also used : Udtf(io.confluent.ksql.function.udtf.Udtf) Method(java.lang.reflect.Method) KsqlException(io.confluent.ksql.util.KsqlException) UdfMetadata(io.confluent.ksql.function.udf.UdfMetadata) ParamType(io.confluent.ksql.function.types.ParamType) ParameterizedType(java.lang.reflect.ParameterizedType) ParamType(io.confluent.ksql.function.types.ParamType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) UdtfDescription(io.confluent.ksql.function.udtf.UdtfDescription)

Aggregations

ParamType (io.confluent.ksql.function.types.ParamType)1 UdfMetadata (io.confluent.ksql.function.udf.UdfMetadata)1 Udtf (io.confluent.ksql.function.udtf.Udtf)1 UdtfDescription (io.confluent.ksql.function.udtf.UdtfDescription)1 KsqlException (io.confluent.ksql.util.KsqlException)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1