use of io.confluent.ksql.function.udf.Udf 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.udf.Udf 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.udf.Udf 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.udf.Udf in project ksql by confluentinc.
the class JsonRecords method records.
@Udf
public Map<String, String> records(@UdfParameter final String jsonObj) {
if (jsonObj == null) {
return null;
}
final JsonNode node = UdfJsonMapper.parseJson(jsonObj);
if (node.isMissingNode() || !node.isObject()) {
return null;
}
final Map<String, String> ret = new HashMap<>(node.size());
node.fieldNames().forEachRemaining(k -> ret.put(k, node.get(k).toString()));
return ret;
}
use of io.confluent.ksql.function.udf.Udf in project ksql by confluentinc.
the class ArrayJoin method join.
@Udf
public <T> String join(@UdfParameter(description = "the array to join using the specified delimiter") final List<T> array, @UdfParameter(description = "the string to be used as element delimiter") final String delimiter) {
if (array == null) {
return null;
}
final StringJoiner sj = new StringJoiner(delimiter == null ? "" : delimiter);
array.forEach(e -> processElement(e, sj));
return sj.toString();
}
Aggregations