use of io.confluent.ksql.function.udf.Udf 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);
}
}
use of io.confluent.ksql.function.udf.Udf 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);
}
}
use of io.confluent.ksql.function.udf.Udf in project ksql by confluentinc.
the class JsonExtractString method extract.
@Udf
public String extract(@UdfParameter(description = "The input JSON string") final String input, @UdfParameter(description = "The JSONPath to extract") final String path) {
if (input == null || path == null) {
return null;
}
if (latestPath == null || !latestPath.equals(path)) {
final JsonPathTokenizer tokenizer = new JsonPathTokenizer(path);
latestTokens = ImmutableList.copyOf(tokenizer);
latestPath = path;
}
JsonNode currentNode = UdfJsonMapper.parseJson(input);
for (final String token : latestTokens) {
if (currentNode instanceof ArrayNode) {
try {
final int index = Integer.parseInt(token);
currentNode = currentNode.get(index);
} catch (final NumberFormatException e) {
return null;
}
} else {
currentNode = currentNode.get(token);
}
if (currentNode == null) {
return null;
}
}
if (currentNode.isTextual()) {
return currentNode.asText();
} else {
return currentNode.toString();
}
}
Aggregations