Search in sources :

Example 1 with KsqlFunctionException

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();
    }
}
Also used : KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 2 with KsqlFunctionException

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);
    }
}
Also used : KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) Timestamp(java.sql.Timestamp) KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException)

Example 3 with KsqlFunctionException

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"));
}
Also used : KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) Test(org.junit.Test)

Example 4 with KsqlFunctionException

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"));
}
Also used : KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) Test(org.junit.Test)

Example 5 with KsqlFunctionException

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"));
}
Also used : KsqlFunctionException(io.confluent.ksql.function.KsqlFunctionException) Test(org.junit.Test)

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