Search in sources :

Example 6 with Udf

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

the class ParseTime method parseTime.

@Udf(description = "Converts a string representation of a time in the given format" + " into the TIME value.")
public Time parseTime(@UdfParameter(description = "The string representation of a time.") final String formattedTime, @UdfParameter(description = "The format pattern should be in the format expected by" + " java.time.format.DateTimeFormatter.") final String formatPattern) {
    if (formattedTime == null | formatPattern == null) {
        return null;
    }
    try {
        final TemporalAccessor ta = formatters.get(formatPattern).parse(formattedTime);
        final Optional<ChronoField> dateField = Arrays.stream(ChronoField.values()).filter(ChronoField::isDateBased).filter(ta::isSupported).findFirst();
        if (dateField.isPresent()) {
            throw new KsqlFunctionException("Time format contains date field.");
        }
        return new Time(TimeUnit.NANOSECONDS.toMillis(LocalTime.from(ta).toNanoOfDay()));
    } catch (ExecutionException | RuntimeException e) {
        throw new KsqlFunctionException("Failed to parse time '" + formattedTime + "' with formatter '" + formatPattern + "': " + e.getMessage(), e);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) ChronoField(java.time.temporal.ChronoField) Time(java.sql.Time) LocalTime(java.time.LocalTime) ExecutionException(java.util.concurrent.ExecutionException) Udf(io.confluent.ksql.function.udf.Udf)

Example 7 with Udf

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

the class ParseTimestamp method parseTimestamp.

@Udf(description = "Converts a string representation of a date at the given time zone" + " in the given format into the TIMESTAMP value." + " Single quotes in the timestamp format can be escaped with ''," + " for example: 'yyyy-MM-dd''T''HH:mm:ssX'.")
public Timestamp parseTimestamp(@UdfParameter(description = "The string representation of a date.") final String formattedTimestamp, @UdfParameter(description = "The format pattern should be in the format expected by" + " java.time.format.DateTimeFormatter.") final String formatPattern, @UdfParameter(description = " timeZone is a java.util.TimeZone ID format, for example: \"UTC\"," + " \"America/Los_Angeles\", \"PST\", \"Europe/London\"") final String timeZone) {
    if (formattedTimestamp == null || formatPattern == null || timeZone == null) {
        return null;
    }
    try {
        final StringToTimestampParser timestampParser = parsers.get(formatPattern);
        final ZoneId zoneId = ZoneId.of(timeZone);
        return timestampParser.parseToTimestamp(formattedTimestamp, zoneId);
    } catch (final ExecutionException | RuntimeException e) {
        throw new KsqlFunctionException("Failed to parse timestamp '" + formattedTimestamp + "' at timezone '" + timeZone + "' with formatter '" + formatPattern + "': " + e.getMessage(), e);
    }
}
Also used : ZoneId(java.time.ZoneId) KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) StringToTimestampParser(io.confluent.ksql.util.timestamp.StringToTimestampParser) ExecutionException(java.util.concurrent.ExecutionException) Udf(io.confluent.ksql.function.udf.Udf)

Example 8 with Udf

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

the class StringToTimestamp method stringToTimestamp.

@Udf(description = "Converts a string representation of a date at the given time zone" + " into the number of milliseconds since 1970-01-01 00:00:00 UTC/GMT." + " Single quotes in the timestamp format can be escaped with ''," + " for example: 'yyyy-MM-dd''T''HH:mm:ssX'.")
public long stringToTimestamp(@UdfParameter(description = "The string representation of a date.") final String formattedTimestamp, @UdfParameter(description = "The format pattern should be in the format expected by" + " java.time.format.DateTimeFormatter.") final String formatPattern, @UdfParameter(description = " timeZone is a java.util.TimeZone ID format, for example: \"UTC\"," + " \"America/Los_Angeles\", \"PST\", \"Europe/London\"") final String timeZone) {
    // there is no sentinel value for a "null" Date.
    try {
        final StringToTimestampParser timestampParser = parsers.get(formatPattern);
        final ZoneId zoneId = ZoneId.of(timeZone);
        return timestampParser.parse(formattedTimestamp, zoneId);
    } catch (final ExecutionException | RuntimeException e) {
        throw new KsqlFunctionException("Failed to parse timestamp '" + formattedTimestamp + "' at timezone '" + timeZone + "' with formatter '" + formatPattern + "': " + e.getMessage(), e);
    }
}
Also used : ZoneId(java.time.ZoneId) KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) StringToTimestampParser(io.confluent.ksql.util.timestamp.StringToTimestampParser) ExecutionException(java.util.concurrent.ExecutionException) Udf(io.confluent.ksql.function.udf.Udf)

Example 9 with Udf

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

the class JsonRecords method records.

@Udf
public Map<String, String> records(@UdfParameter final String jsonObj) {
    if (jsonObj == null) {
        return null;
    }
    final JsonNode node = UdfJsonMapper.parseJson(jsonObj);
    if (node.isMissingNode() || !node.isObject()) {
        return null;
    }
    final Map<String, String> ret = new HashMap<>(node.size());
    node.fieldNames().forEachRemaining(k -> ret.put(k, node.get(k).toString()));
    return ret;
}
Also used : HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) Udf(io.confluent.ksql.function.udf.Udf)

Example 10 with Udf

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

the class ArrayJoin method join.

@Udf
public <T> String join(@UdfParameter(description = "the array to join using the specified delimiter") final List<T> array, @UdfParameter(description = "the string to be used as element delimiter") final String delimiter) {
    if (array == null) {
        return null;
    }
    final StringJoiner sj = new StringJoiner(delimiter == null ? "" : delimiter);
    array.forEach(e -> processElement(e, sj));
    return sj.toString();
}
Also used : StringJoiner(java.util.StringJoiner) 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