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);
}
}
}
Aggregations