use of io.confluent.ksql.function.KsqlFunctionException in project ksql by confluentinc.
the class JsonExtractStringKudf method evaluate.
@Override
public Object evaluate(Object... args) {
if (args.length != 2) {
throw new KsqlFunctionException("getStringFromJson udf should have two input arguments:" + " JSON document and JSON path.");
}
ensureInitialized(args);
JsonNode currentNode = parseJsonDoc(args[0]);
for (String token : tokens) {
if (currentNode instanceof ArrayNode) {
try {
final int index = Integer.parseInt(token);
currentNode = currentNode.get(index);
} catch (NumberFormatException e) {
return null;
}
} else {
currentNode = currentNode.get(token);
}
if (currentNode == null) {
return null;
}
}
if (currentNode.isTextual()) {
return currentNode.asText();
} else {
return currentNode.toString();
}
}
use of io.confluent.ksql.function.KsqlFunctionException in project ksql by confluentinc.
the class TimestampToString method evaluate.
@Override
public Object evaluate(Object... args) {
if (args.length != 2) {
throw new KsqlFunctionException("TimestampToString udf should have two input argument:" + " date value and format.");
}
try {
ensureInitialized(args);
final Timestamp timestamp = new Timestamp((Long) args[0]);
return timestamp.toLocalDateTime().atZone(ZoneId.systemDefault()).format(threadSafeFormatter);
} catch (Exception e) {
throw new KsqlFunctionException("Exception running TimestampToString(" + args[0] + " , " + args[1] + ") : " + e.getMessage(), e);
}
}
use of io.confluent.ksql.function.KsqlFunctionException in project ksql by confluentinc.
the class ConvertTzTest method shouldThrowOnInvalidTimezone.
@Test
public void shouldThrowOnInvalidTimezone() {
// When:
final KsqlFunctionException e = assertThrows(KsqlFunctionException.class, () -> udf.convertTz(Timestamp.valueOf("2000-01-01 00:00:00"), "wow", "amazing"));
// Then:
assertThat(e.getMessage(), containsString("Invalid time zone"));
}
use of io.confluent.ksql.function.KsqlFunctionException in project ksql by confluentinc.
the class StringToTimestampTest method shouldThrowOnEmptyString.
@Test
public void shouldThrowOnEmptyString() {
// When:
final KsqlFunctionException e = assertThrows(KsqlFunctionException.class, () -> udf.stringToTimestamp("", "yyyy-MM-dd'T'HH:mm:ss.SSS"));
// Then:
assertThat(e.getMessage(), containsString("Text '' could not be parsed at index 0"));
}
use of io.confluent.ksql.function.KsqlFunctionException in project ksql by confluentinc.
the class StringToTimestampTest method shouldThrowIfParseFails.
@Test
public void shouldThrowIfParseFails() {
// When:
final KsqlFunctionException e = assertThrows(KsqlFunctionException.class, () -> udf.stringToTimestamp("invalid", "yyyy-MM-dd'T'HH:mm:ss.SSS"));
// Then:
assertThat(e.getMessage(), containsString("Text 'invalid' could not be parsed at index 0"));
}
Aggregations