Search in sources :

Example 11 with KsqlFunctionException

use of io.confluent.ksql.function.KsqlFunctionException in project ksql by confluentinc.

the class ParseTimestampTest method shouldThrowOnEmptyString.

@Test
public void shouldThrowOnEmptyString() {
    // When:
    final KsqlFunctionException e = assertThrows(KsqlFunctionException.class, () -> udf.parseTimestamp("", "yyyy-MM-dd'T'HH:mm:ss.SSS"));
    // Then:
    assertThat(e.getMessage(), containsString("Text '' could not be parsed at index 0"));
}
Also used : KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) Test(org.junit.Test)

Example 12 with KsqlFunctionException

use of io.confluent.ksql.function.KsqlFunctionException in project ksql by confluentinc.

the class ParseTimestampTest method shouldThrowIfFormatInvalid.

@Test
public void shouldThrowIfFormatInvalid() {
    // When:
    final KsqlFunctionException e = assertThrows(KsqlFunctionException.class, () -> udf.parseTimestamp("2021-12-01 12:10:11.123", "invalid"));
    // Then:
    assertThat(e.getMessage(), containsString("Unknown pattern letter: i"));
}
Also used : KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) Test(org.junit.Test)

Example 13 with KsqlFunctionException

use of io.confluent.ksql.function.KsqlFunctionException in project ksql by confluentinc.

the class ParseTimestampTest method shouldThrowIfParseFails.

@Test
public void shouldThrowIfParseFails() {
    // When:
    final KsqlFunctionException e = assertThrows(KsqlFunctionException.class, () -> udf.parseTimestamp("invalid", "yyyy-MM-dd'T'HH:mm:ss.SSS"));
    // Then:
    assertThat(e.getMessage(), containsString("Text 'invalid' could not be parsed at index 0"));
}
Also used : KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) Test(org.junit.Test)

Example 14 with KsqlFunctionException

use of io.confluent.ksql.function.KsqlFunctionException in project ksql by confluentinc.

the class StringToTimestampTest method shouldThrowIfFormatInvalid.

@Test
public void shouldThrowIfFormatInvalid() {
    // When:
    final KsqlFunctionException e = assertThrows(KsqlFunctionException.class, () -> udf.stringToTimestamp("2021-12-01 12:10:11.123", "invalid"));
    // Then:
    assertThat(e.getMessage(), containsString("Unknown pattern letter: i"));
}
Also used : KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) Test(org.junit.Test)

Example 15 with KsqlFunctionException

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

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