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]));
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations