Search in sources :

Example 16 with Udf

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

the class FormatTimestamp method formatTimestamp.

@Udf(description = "Converts a TIMESTAMP value into the" + " string representation of the timestamp in the given format. Single quotes in the" + " timestamp format can be escaped with '', for example: 'yyyy-MM-dd''T''HH:mm:ssX'")
public String formatTimestamp(@UdfParameter(description = "TIMESTAMP value.") final Timestamp timestamp, @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 (timestamp == null || formatPattern == null || timeZone == null) {
        return null;
    }
    try {
        final DateTimeFormatter formatter = formatters.get(formatPattern);
        final ZoneId zoneId = ZoneId.of(timeZone);
        return timestamp.toInstant().atZone(zoneId).format(formatter);
    } catch (final ExecutionException | RuntimeException e) {
        throw new KsqlFunctionException("Failed to format timestamp " + timestamp + " at timeZone '" + timeZone + "' with formatter '" + formatPattern + "': " + e.getMessage(), e);
    }
}
Also used : ZoneId(java.time.ZoneId) KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) ExecutionException(java.util.concurrent.ExecutionException) DateTimeFormatter(java.time.format.DateTimeFormatter) Udf(io.confluent.ksql.function.udf.Udf)

Example 17 with Udf

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

the class TimestampToString method timestampToString.

@Udf(description = "Converts a number of milliseconds since 1970-01-01 00:00:00 UTC/GMT into the" + " string representation of the timestamp in the given format. Single quotes in the" + " timestamp format can be escaped with '', for example: 'yyyy-MM-dd''T''HH:mm:ssX'." + " The system default time zone is used when no time zone is explicitly provided." + " The format pattern should be in the format expected" + " by java.time.format.DateTimeFormatter")
public String timestampToString(@UdfParameter(description = "Milliseconds since" + " January 1, 1970, 00:00:00 UTC/GMT.") final long epochMilli, @UdfParameter(description = "The format pattern should be in the format expected by" + " java.time.format.DateTimeFormatter.") final String formatPattern) {
    if (formatPattern == null) {
        return null;
    }
    try {
        final Timestamp timestamp = new Timestamp(epochMilli);
        final DateTimeFormatter formatter = formatters.get(formatPattern);
        return timestamp.toInstant().atZone(ZoneId.systemDefault()).format(formatter);
    } catch (final ExecutionException | RuntimeException e) {
        throw new KsqlFunctionException("Failed to format timestamp " + epochMilli + " with formatter '" + formatPattern + "': " + e.getMessage(), e);
    }
}
Also used : KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) ExecutionException(java.util.concurrent.ExecutionException) Timestamp(java.sql.Timestamp) DateTimeFormatter(java.time.format.DateTimeFormatter) Udf(io.confluent.ksql.function.udf.Udf)

Example 18 with Udf

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

the class JsonExtractString method extract.

@Udf
public String extract(@UdfParameter(description = "The input JSON string") final String input, @UdfParameter(description = "The JSONPath to extract") final String path) {
    if (input == null || path == null) {
        return null;
    }
    if (latestPath == null || !latestPath.equals(path)) {
        final JsonPathTokenizer tokenizer = new JsonPathTokenizer(path);
        latestTokens = ImmutableList.copyOf(tokenizer);
        latestPath = path;
    }
    JsonNode currentNode = UdfJsonMapper.parseJson(input);
    for (final String token : latestTokens) {
        if (currentNode instanceof ArrayNode) {
            try {
                final int index = Integer.parseInt(token);
                currentNode = currentNode.get(index);
            } catch (final NumberFormatException e) {
                return null;
            }
        } else {
            currentNode = currentNode.get(token);
        }
        if (currentNode == null) {
            return null;
        }
    }
    if (currentNode.isTextual()) {
        return currentNode.asText();
    } else {
        return currentNode.toString();
    }
}
Also used : JsonPathTokenizer(io.confluent.ksql.util.json.JsonPathTokenizer) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) 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