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