Search in sources :

Example 6 with KsqlFunctionException

use of io.confluent.ksql.function.KsqlFunctionException 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]));
}
Also used : KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Udf(io.confluent.ksql.function.udf.Udf)

Example 7 with KsqlFunctionException

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

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

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

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

the class StringToTimestamp method evaluate.

@Override
public Object evaluate(final Object... args) {
    if (args.length != 2) {
        throw new KsqlFunctionException("StringToTimestamp udf should have two input argument:" + " date value and format.");
    }
    try {
        ensureInitialized(args);
        TemporalAccessor parsed = threadSafeFormatter.parseBest(args[0].toString(), ZonedDateTime::from, LocalDateTime::from);
        if (parsed == null) {
            throw new KsqlFunctionException("Value could not be parsed");
        }
        if (parsed instanceof ZonedDateTime) {
            parsed = ((ZonedDateTime) parsed).withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
        }
        final LocalDateTime dateTime = (LocalDateTime) parsed;
        return Timestamp.valueOf(dateTime).getTime();
    } catch (final Exception e) {
        throw new KsqlFunctionException("Exception running StringToTimestamp(" + args[0] + ", " + args[1] + ") : " + e.getMessage(), e);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) TemporalAccessor(java.time.temporal.TemporalAccessor) ZonedDateTime(java.time.ZonedDateTime) KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException)

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