Search in sources :

Example 1 with Udf

use of io.confluent.ksql.function.udf.Udf 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)

Example 2 with Udf

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

the class InitCap method initcap.

@Udf(description = "Returns the string with the the first letter" + " of each word capitalized and the rest lowercased")
public String initcap(@UdfParameter(description = "The source string." + " If null, then function returns null.") final String str) {
    if (str == null) {
        return null;
    }
    final Pattern pattern = Pattern.compile("[^\\s]+\\s*");
    final Matcher matcher = pattern.matcher(str.toLowerCase());
    String initCapped = "";
    while (matcher.find()) {
        final String part = matcher.group();
        initCapped = initCapped.concat(part.substring(0, 1).toUpperCase() + part.substring(1));
    }
    return initCapped;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Udf(io.confluent.ksql.function.udf.Udf)

Example 3 with Udf

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

the class Concat method concat.

@Udf
public ByteBuffer concat(@UdfParameter(description = "The bytes fields to concatenate") final ByteBuffer... inputs) {
    if (inputs == null) {
        return null;
    }
    int capacity = 0;
    for (final ByteBuffer bytes : inputs) {
        if (Objects.nonNull(bytes)) {
            capacity += bytes.capacity();
        }
    }
    final ByteBuffer concatenated = ByteBuffer.allocate(capacity);
    Arrays.stream(inputs).filter(Objects::nonNull).forEachOrdered(concatenated::put);
    concatenated.rewind();
    return concatenated;
}
Also used : ByteBuffer(java.nio.ByteBuffer) Udf(io.confluent.ksql.function.udf.Udf)

Example 4 with Udf

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

the class RegexpExtract method regexpExtract.

@Udf(description = "Returns the first substring of the " + "input that matches the regex pattern and the capturing group number specified")
public String regexpExtract(@UdfParameter(description = "The regex pattern") final String pattern, @UdfParameter(description = "The input string to apply regex on") final String input, @UdfParameter(description = "The capturing group number") final Integer group) {
    if (pattern == null || input == null || group == null) {
        return null;
    }
    final Pattern p = Pattern.compile(pattern);
    final Matcher m = p.matcher(input);
    if (group > m.groupCount()) {
        return null;
    }
    return m.find() ? m.group(group) : null;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Udf(io.confluent.ksql.function.udf.Udf)

Example 5 with Udf

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

the class ConcatWS method concatWS.

@Udf
public ByteBuffer concatWS(@UdfParameter(description = "Separator and bytes values to join") final ByteBuffer... inputs) {
    if (inputs == null || inputs.length < 2) {
        throw new KsqlFunctionException("Function Concat_WS expects at least two input arguments.");
    }
    final ByteBuffer separator = inputs[0];
    if (separator == null) {
        return null;
    }
    final List<ByteBuffer> concatInputs = new ArrayList<>();
    for (int i = 1; i < inputs.length; i++) {
        if (Objects.nonNull(inputs[i])) {
            if (concatInputs.size() != 0) {
                concatInputs.add(separator.duplicate());
            }
            concatInputs.add(inputs[i]);
        }
    }
    return CONCAT.concat(concatInputs.toArray(new ByteBuffer[0]));
}
Also used : KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Udf(io.confluent.ksql.function.udf.Udf)

Aggregations

Udf (io.confluent.ksql.function.udf.Udf)18 KsqlFunctionException (io.confluent.ksql.function.KsqlFunctionException)6 ExecutionException (java.util.concurrent.ExecutionException)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayList (java.util.ArrayList)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ParamType (io.confluent.ksql.function.types.ParamType)2 UdfDescription (io.confluent.ksql.function.udf.UdfDescription)2 UdfParameter (io.confluent.ksql.function.udf.UdfParameter)2 SqlTypeParser (io.confluent.ksql.schema.ksql.SqlTypeParser)2 KsqlException (io.confluent.ksql.util.KsqlException)2 StringToTimestampParser (io.confluent.ksql.util.timestamp.StringToTimestampParser)2 ByteBuffer (java.nio.ByteBuffer)2 ZoneId (java.time.ZoneId)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 CANONICALIZE_FIELD_NAMES (com.fasterxml.jackson.core.JsonFactory.Feature.CANONICALIZE_FIELD_NAMES)1 JsonFactoryBuilder (com.fasterxml.jackson.core.JsonFactoryBuilder)1