Search in sources :

Example 11 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project flink by apache.

the class LogicalWindowJsonDeserializer method deserialize.

@Override
public LogicalWindow deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    JsonNode jsonNode = jsonParser.readValueAsTree();
    String kind = jsonNode.get(FIELD_NAME_KIND).asText().toUpperCase();
    WindowReference alias = deserializationContext.readValue(jsonNode.get(FIELD_NAME_ALIAS).traverse(jsonParser.getCodec()), WindowReference.class);
    FieldReferenceExpression timeField = deserializeFieldReferenceExpression(jsonNode.get(FIELD_NAME_TIME_FIELD), jsonParser, deserializationContext);
    switch(kind) {
        case KIND_TUMBLING:
            boolean isTimeTumblingWindow = jsonNode.get(FIELD_NAME_IS_TIME_WINDOW).asBoolean();
            if (isTimeTumblingWindow) {
                Duration size = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_SIZE), jsonParser.getCodec()), Duration.class);
                return new TumblingGroupWindow(alias, timeField, new ValueLiteralExpression(size));
            } else {
                long size = jsonNode.get(FIELD_NAME_SIZE).asLong();
                return new TumblingGroupWindow(alias, timeField, new ValueLiteralExpression(size));
            }
        case KIND_SLIDING:
            boolean isTimeSlidingWindow = jsonNode.get(FIELD_NAME_IS_TIME_WINDOW).asBoolean();
            if (isTimeSlidingWindow) {
                Duration size = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_SIZE), jsonParser.getCodec()), Duration.class);
                Duration slide = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_SLIDE), jsonParser.getCodec()), Duration.class);
                return new SlidingGroupWindow(alias, timeField, new ValueLiteralExpression(size), new ValueLiteralExpression(slide));
            } else {
                long size = jsonNode.get(FIELD_NAME_SIZE).asLong();
                long slide = jsonNode.get(FIELD_NAME_SLIDE).asLong();
                return new SlidingGroupWindow(alias, timeField, new ValueLiteralExpression(size), new ValueLiteralExpression(slide));
            }
        case KIND_SESSION:
            Duration gap = deserializationContext.readValue(traverse(jsonNode.get(FIELD_NAME_GAP), jsonParser.getCodec()), Duration.class);
            return new SessionGroupWindow(alias, timeField, new ValueLiteralExpression(gap));
        default:
            throw new TableException("Unknown Logical Window:" + jsonNode);
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) TumblingGroupWindow(org.apache.flink.table.planner.plan.logical.TumblingGroupWindow) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) Duration(java.time.Duration) SlidingGroupWindow(org.apache.flink.table.planner.plan.logical.SlidingGroupWindow) SessionGroupWindow(org.apache.flink.table.planner.plan.logical.SessionGroupWindow) WindowReference(org.apache.flink.table.runtime.groupwindow.WindowReference)

Example 12 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project flink by apache.

the class ChangelogModeJsonDeserializer method deserialize.

@Override
public ChangelogMode deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    ChangelogMode.Builder builder = ChangelogMode.newBuilder();
    JsonNode rowKindsNode = jsonParser.readValueAsTree();
    for (JsonNode rowKindNode : rowKindsNode) {
        RowKind rowKind = RowKind.valueOf(rowKindNode.asText().toUpperCase());
        builder.addContainedKind(rowKind);
    }
    return builder.build();
}
Also used : ChangelogMode(org.apache.flink.table.connector.ChangelogMode) RowKind(org.apache.flink.types.RowKind) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)

Example 13 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project flink by apache.

the class ColumnJsonDeserializer method deserialize.

@Override
public Column deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException {
    ObjectNode jsonNode = jsonParser.readValueAsTree();
    String columnName = jsonNode.required(NAME).asText();
    String columnKind = Optional.ofNullable(jsonNode.get(KIND)).map(JsonNode::asText).orElse(KIND_PHYSICAL);
    Column column;
    switch(columnKind) {
        case KIND_PHYSICAL:
            column = deserializePhysicalColumn(columnName, jsonNode, jsonParser.getCodec(), ctx);
            break;
        case KIND_COMPUTED:
            column = deserializeComputedColumn(columnName, jsonNode, jsonParser.getCodec(), ctx);
            break;
        case KIND_METADATA:
            column = deserializeMetadataColumn(columnName, jsonNode, jsonParser.getCodec(), ctx);
            break;
        default:
            throw new ValidationException(String.format("Cannot recognize column type '%s'. Allowed types: %s.", columnKind, SUPPORTED_KINDS));
    }
    return column.withComment(deserializeOptionalField(jsonNode, COMMENT, String.class, jsonParser.getCodec(), ctx).orElse(null));
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) ObjectNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode) Column(org.apache.flink.table.catalog.Column)

Example 14 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project flink by apache.

the class ContextResolvedTableJsonDeserializer method deserialize.

@Override
public ContextResolvedTable deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException {
    final CatalogPlanRestore planRestoreOption = SerdeContext.get(ctx).getConfiguration().get(PLAN_RESTORE_CATALOG_OBJECTS);
    final CatalogManager catalogManager = SerdeContext.get(ctx).getFlinkContext().getCatalogManager();
    final ObjectNode objectNode = jsonParser.readValueAsTree();
    // Deserialize the two fields, if available
    final ObjectIdentifier identifier = JsonSerdeUtil.deserializeOptionalField(objectNode, FIELD_NAME_IDENTIFIER, ObjectIdentifier.class, jsonParser.getCodec(), ctx).orElse(null);
    ResolvedCatalogTable resolvedCatalogTable = JsonSerdeUtil.deserializeOptionalField(objectNode, FIELD_NAME_CATALOG_TABLE, ResolvedCatalogTable.class, jsonParser.getCodec(), ctx).orElse(null);
    if (identifier == null && resolvedCatalogTable == null) {
        throw new ValidationException(String.format("The input JSON is invalid because it doesn't contain '%s', nor the '%s'.", FIELD_NAME_IDENTIFIER, FIELD_NAME_CATALOG_TABLE));
    }
    if (identifier == null) {
        if (isLookupForced(planRestoreOption)) {
            throw missingIdentifier();
        }
        return ContextResolvedTable.anonymous(resolvedCatalogTable);
    }
    Optional<ContextResolvedTable> contextResolvedTableFromCatalog = isLookupEnabled(planRestoreOption) ? catalogManager.getTable(identifier) : Optional.empty();
    // If we have a schema from the plan and from the catalog, we need to check they match.
    if (contextResolvedTableFromCatalog.isPresent() && resolvedCatalogTable != null) {
        ResolvedSchema schemaFromPlan = resolvedCatalogTable.getResolvedSchema();
        ResolvedSchema schemaFromCatalog = contextResolvedTableFromCatalog.get().getResolvedSchema();
        if (!areResolvedSchemasEqual(schemaFromPlan, schemaFromCatalog)) {
            throw schemaNotMatching(identifier, schemaFromPlan, schemaFromCatalog);
        }
    }
    if (resolvedCatalogTable == null || isLookupForced(planRestoreOption)) {
        if (!isLookupEnabled(planRestoreOption)) {
            throw lookupDisabled(identifier);
        }
        // We use what is stored inside the catalog
        return contextResolvedTableFromCatalog.orElseThrow(() -> missingTableFromCatalog(identifier, isLookupForced(planRestoreOption)));
    }
    if (contextResolvedTableFromCatalog.isPresent()) {
        // SCHEMA, so we just need to return the catalog query result
        if (objectNode.at("/" + FIELD_NAME_CATALOG_TABLE + "/" + OPTIONS).isMissingNode()) {
            return contextResolvedTableFromCatalog.get();
        }
        return contextResolvedTableFromCatalog.flatMap(ContextResolvedTable::getCatalog).map(c -> ContextResolvedTable.permanent(identifier, c, resolvedCatalogTable)).orElseGet(() -> ContextResolvedTable.temporary(identifier, resolvedCatalogTable));
    }
    return ContextResolvedTable.temporary(identifier, resolvedCatalogTable);
}
Also used : CatalogManager(org.apache.flink.table.catalog.CatalogManager) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier) FIELD_NAME_IDENTIFIER(org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedTableJsonSerializer.FIELD_NAME_IDENTIFIER) Column(org.apache.flink.table.catalog.Column) ObjectNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode) IDENTIFIER(org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore.IDENTIFIER) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) JsonParser(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser) IOException(java.io.IOException) PLAN_COMPILE_CATALOG_OBJECTS(org.apache.flink.table.api.config.TableConfigOptions.PLAN_COMPILE_CATALOG_OBJECTS) CatalogPlanRestore(org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore) CatalogPlanCompilation(org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanCompilation) Objects(java.util.Objects) OPTIONS(org.apache.flink.table.planner.plan.nodes.exec.serde.ResolvedCatalogTableJsonSerializer.OPTIONS) DeserializationContext(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext) List(java.util.List) ValidationException(org.apache.flink.table.api.ValidationException) Optional(java.util.Optional) Internal(org.apache.flink.annotation.Internal) StdDeserializer(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer) PLAN_RESTORE_CATALOG_OBJECTS(org.apache.flink.table.api.config.TableConfigOptions.PLAN_RESTORE_CATALOG_OBJECTS) ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) FIELD_NAME_CATALOG_TABLE(org.apache.flink.table.planner.plan.nodes.exec.serde.ContextResolvedTableJsonSerializer.FIELD_NAME_CATALOG_TABLE) ContextResolvedTable(org.apache.flink.table.catalog.ContextResolvedTable) ValidationException(org.apache.flink.table.api.ValidationException) ObjectNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode) ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) CatalogPlanRestore(org.apache.flink.table.api.config.TableConfigOptions.CatalogPlanRestore) ContextResolvedTable(org.apache.flink.table.catalog.ContextResolvedTable) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) CatalogManager(org.apache.flink.table.catalog.CatalogManager) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier)

Example 15 with DeserializationContext

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext in project flink by apache.

the class ShuffleJsonDeserializer method deserialize.

@Override
public Shuffle deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException {
    JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
    Shuffle.Type type = Shuffle.Type.valueOf(jsonNode.get("type").asText().toUpperCase());
    switch(type) {
        case ANY:
            return ExecEdge.ANY_SHUFFLE;
        case SINGLETON:
            return ExecEdge.SINGLETON_SHUFFLE;
        case BROADCAST:
            return ExecEdge.BROADCAST_SHUFFLE;
        case FORWARD:
            return ExecEdge.FORWARD_SHUFFLE;
        case HASH:
            JsonNode keysNode = jsonNode.get("keys");
            if (keysNode == null || keysNode.size() == 0) {
                throw new TableException("Hash shuffle requires non-empty hash keys.");
            }
            int[] keys = new int[keysNode.size()];
            for (int i = 0; i < keysNode.size(); ++i) {
                keys[i] = keysNode.get(i).asInt();
            }
            return ExecEdge.hashShuffle(keys);
        default:
            throw new TableException("Unsupported shuffle type: " + type);
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) Shuffle(org.apache.flink.table.planner.plan.nodes.exec.ExecEdge.Shuffle) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)

Aggregations

DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)28 JsonParser (com.fasterxml.jackson.core.JsonParser)21 IOException (java.io.IOException)17 JsonNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)11 Test (org.junit.Test)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)7 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)7 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)7 BeanProperty (com.fasterxml.jackson.databind.BeanProperty)6 StdDeserializer (com.fasterxml.jackson.databind.deser.std.StdDeserializer)6 Map (java.util.Map)6 InjectableValues (com.fasterxml.jackson.databind.InjectableValues)5 List (java.util.List)5 ObjectNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 Iterator (java.util.Iterator)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3