Search in sources :

Example 1 with UdfParameter

use of io.confluent.ksql.function.udf.UdfParameter in project ksql by confluentinc.

the class JsonArrayContains method contains.

@Udf
public <T> Boolean contains(@UdfParameter final String jsonArray, @UdfParameter final T val) {
    try (JsonParser parser = PARSER_FACTORY.createParser(jsonArray)) {
        if (parser.nextToken() != START_ARRAY) {
            return false;
        }
        while (parser.nextToken() != null) {
            final JsonToken token = parser.currentToken();
            if (token == null) {
                return val == null;
            } else if (token == END_ARRAY) {
                return false;
            }
            parser.skipChildren();
            if (TOKEN_COMPAT.getOrDefault(token, foo -> false).test(val)) {
                if (token == VALUE_NULL || (val != null && Objects.equals(parser.readValueAs(val.getClass()), val))) {
                    return true;
                }
            }
        }
        return false;
    } catch (final IOException e) {
        return false;
    }
}
Also used : UdfDescription(io.confluent.ksql.function.udf.UdfDescription) JsonParser(com.fasterxml.jackson.core.JsonParser) Udf(io.confluent.ksql.function.udf.Udf) UdfParameter(io.confluent.ksql.function.udf.UdfParameter) EnumMap(java.util.EnumMap) VALUE_NUMBER_FLOAT(com.fasterxml.jackson.core.JsonToken.VALUE_NUMBER_FLOAT) Predicate(java.util.function.Predicate) VALUE_NUMBER_INT(com.fasterxml.jackson.core.JsonToken.VALUE_NUMBER_INT) JsonFactoryBuilder(com.fasterxml.jackson.core.JsonFactoryBuilder) VALUE_STRING(com.fasterxml.jackson.core.JsonToken.VALUE_STRING) END_ARRAY(com.fasterxml.jackson.core.JsonToken.END_ARRAY) IOException(java.io.IOException) VALUE_NULL(com.fasterxml.jackson.core.JsonToken.VALUE_NULL) CANONICALIZE_FIELD_NAMES(com.fasterxml.jackson.core.JsonFactory.Feature.CANONICALIZE_FIELD_NAMES) VALUE_FALSE(com.fasterxml.jackson.core.JsonToken.VALUE_FALSE) Objects(java.util.Objects) JsonFactory(com.fasterxml.jackson.core.JsonFactory) START_ARRAY(com.fasterxml.jackson.core.JsonToken.START_ARRAY) VALUE_TRUE(com.fasterxml.jackson.core.JsonToken.VALUE_TRUE) JsonToken(com.fasterxml.jackson.core.JsonToken) FunctionCategory(io.confluent.ksql.function.FunctionCategory) KsqlConstants(io.confluent.ksql.util.KsqlConstants) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser) Udf(io.confluent.ksql.function.udf.Udf)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 CANONICALIZE_FIELD_NAMES (com.fasterxml.jackson.core.JsonFactory.Feature.CANONICALIZE_FIELD_NAMES)1 JsonFactoryBuilder (com.fasterxml.jackson.core.JsonFactoryBuilder)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonToken (com.fasterxml.jackson.core.JsonToken)1 END_ARRAY (com.fasterxml.jackson.core.JsonToken.END_ARRAY)1 START_ARRAY (com.fasterxml.jackson.core.JsonToken.START_ARRAY)1 VALUE_FALSE (com.fasterxml.jackson.core.JsonToken.VALUE_FALSE)1 VALUE_NULL (com.fasterxml.jackson.core.JsonToken.VALUE_NULL)1 VALUE_NUMBER_FLOAT (com.fasterxml.jackson.core.JsonToken.VALUE_NUMBER_FLOAT)1 VALUE_NUMBER_INT (com.fasterxml.jackson.core.JsonToken.VALUE_NUMBER_INT)1 VALUE_STRING (com.fasterxml.jackson.core.JsonToken.VALUE_STRING)1 VALUE_TRUE (com.fasterxml.jackson.core.JsonToken.VALUE_TRUE)1 FunctionCategory (io.confluent.ksql.function.FunctionCategory)1 Udf (io.confluent.ksql.function.udf.Udf)1 UdfDescription (io.confluent.ksql.function.udf.UdfDescription)1 UdfParameter (io.confluent.ksql.function.udf.UdfParameter)1 KsqlConstants (io.confluent.ksql.util.KsqlConstants)1 IOException (java.io.IOException)1 EnumMap (java.util.EnumMap)1