Search in sources :

Example 16 with KsqlFunctionException

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

Aggregations

KsqlFunctionException (io.confluent.ksql.function.KsqlFunctionException)16 Test (org.junit.Test)7 Udf (io.confluent.ksql.function.udf.Udf)6 ExecutionException (java.util.concurrent.ExecutionException)5 ZoneId (java.time.ZoneId)3 StringToTimestampParser (io.confluent.ksql.util.timestamp.StringToTimestampParser)2 Timestamp (java.sql.Timestamp)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 TemporalAccessor (java.time.temporal.TemporalAccessor)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ByteBuffer (java.nio.ByteBuffer)1 Time (java.sql.Time)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 ZonedDateTime (java.time.ZonedDateTime)1 ChronoField (java.time.temporal.ChronoField)1 ArrayList (java.util.ArrayList)1