Search in sources :

Example 51 with GenericRowData

use of org.apache.flink.table.data.GenericRowData in project flink by apache.

the class MaxwellJsonDeserializationSchema method deserialize.

@Override
public void deserialize(byte[] message, Collector<RowData> out) throws IOException {
    if (message == null || message.length == 0) {
        return;
    }
    try {
        final JsonNode root = jsonDeserializer.deserializeToJsonNode(message);
        final GenericRowData row = (GenericRowData) jsonDeserializer.convertToRowData(root);
        // "type" field
        String type = row.getString(2).toString();
        if (OP_INSERT.equals(type)) {
            // "data" field is a row, contains inserted rows
            GenericRowData insert = (GenericRowData) row.getRow(0, fieldCount);
            insert.setRowKind(RowKind.INSERT);
            emitRow(row, insert, out);
        } else if (OP_UPDATE.equals(type)) {
            // "data" field is a row, contains new rows
            // "old" field is a row, contains old values
            // the underlying JSON deserialization schema always produce GenericRowData.
            // "data" field
            GenericRowData after = (GenericRowData) row.getRow(0, fieldCount);
            // "old" field
            GenericRowData before = (GenericRowData) row.getRow(1, fieldCount);
            final JsonNode oldField = root.get(FIELD_OLD);
            for (int f = 0; f < fieldCount; f++) {
                if (before.isNullAt(f) && oldField.findValue(fieldNames.get(f)) == null) {
                    // not null fields in "old" (before) means the fields are changed
                    // null/empty fields in "old" (before) means the fields are not changed
                    // so we just copy the not changed fields into before
                    before.setField(f, after.getField(f));
                }
            }
            before.setRowKind(RowKind.UPDATE_BEFORE);
            after.setRowKind(RowKind.UPDATE_AFTER);
            emitRow(row, before, out);
            emitRow(row, after, out);
        } else if (OP_DELETE.equals(type)) {
            // "data" field is a row, contains deleted rows
            GenericRowData delete = (GenericRowData) row.getRow(0, fieldCount);
            delete.setRowKind(RowKind.DELETE);
            emitRow(row, delete, out);
        } else {
            if (!ignoreParseErrors) {
                throw new IOException(format("Unknown \"type\" value \"%s\". The Maxwell JSON message is '%s'", type, new String(message)));
            }
        }
    } catch (Throwable t) {
        // a big try catch to protect the processing.
        if (!ignoreParseErrors) {
            throw new IOException(format("Corrupt Maxwell JSON message '%s'.", new String(message)), t);
        }
    }
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 52 with GenericRowData

use of org.apache.flink.table.data.GenericRowData in project flink by apache.

the class HiveTableInputFormat method reopen.

@Override
public void reopen(HiveTableInputSplit split, Long state) throws IOException {
    this.open(split);
    this.currentReadCount = state;
    this.reader.seekToRow(state, new GenericRowData(selectedFields.length));
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData)

Example 53 with GenericRowData

use of org.apache.flink.table.data.GenericRowData in project flink by apache.

the class DynamicKafkaRecordSerializationSchema method createProjectedRow.

static RowData createProjectedRow(RowData consumedRow, RowKind kind, RowData.FieldGetter[] fieldGetters) {
    final int arity = fieldGetters.length;
    final GenericRowData genericRowData = new GenericRowData(kind, arity);
    for (int fieldPos = 0; fieldPos < arity; fieldPos++) {
        genericRowData.setField(fieldPos, fieldGetters[fieldPos].getFieldOrNull(consumedRow));
    }
    return genericRowData;
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData)

Example 54 with GenericRowData

use of org.apache.flink.table.data.GenericRowData in project flink by apache.

the class JsonToRowDataConverters method createRowConverter.

public JsonToRowDataConverter createRowConverter(RowType rowType) {
    final JsonToRowDataConverter[] fieldConverters = rowType.getFields().stream().map(RowType.RowField::getType).map(this::createConverter).toArray(JsonToRowDataConverter[]::new);
    final String[] fieldNames = rowType.getFieldNames().toArray(new String[0]);
    return jsonNode -> {
        ObjectNode node = (ObjectNode) jsonNode;
        int arity = fieldNames.length;
        GenericRowData row = new GenericRowData(arity);
        for (int i = 0; i < arity; i++) {
            String fieldName = fieldNames[i];
            JsonNode field = node.get(fieldName);
            try {
                Object convertedField = convertField(fieldConverters[i], fieldName, field);
                row.setField(i, convertedField);
            } catch (Throwable t) {
                throw new JsonParseException(String.format("Fail to deserialize at field: %s.", fieldName), t);
            }
        }
        return row;
    };
}
Also used : ISO8601_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT(org.apache.flink.formats.common.TimeFormats.ISO8601_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT) Array(java.lang.reflect.Array) GenericArrayData(org.apache.flink.table.data.GenericArrayData) IntType(org.apache.flink.table.types.logical.IntType) LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) SQL_TIMESTAMP_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIMESTAMP_FORMAT) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode) MapType(org.apache.flink.table.types.logical.MapType) RowType(org.apache.flink.table.types.logical.RowType) TemporalQueries(java.time.temporal.TemporalQueries) ArrayNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ArrayNode) TemporalAccessor(java.time.temporal.TemporalAccessor) BigDecimal(java.math.BigDecimal) SQL_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT) TextNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.TextNode) GenericRowData(org.apache.flink.table.data.GenericRowData) DecimalType(org.apache.flink.table.types.logical.DecimalType) LogicalTypeFamily(org.apache.flink.table.types.logical.LogicalTypeFamily) GenericMapData(org.apache.flink.table.data.GenericMapData) Map(java.util.Map) LocalTime(java.time.LocalTime) ZoneOffset(java.time.ZoneOffset) ISO_LOCAL_DATE(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE) LogicalTypeUtils(org.apache.flink.table.types.logical.utils.LogicalTypeUtils) MultisetType(org.apache.flink.table.types.logical.MultisetType) ISO8601_TIMESTAMP_FORMAT(org.apache.flink.formats.common.TimeFormats.ISO8601_TIMESTAMP_FORMAT) RowData(org.apache.flink.table.data.RowData) Iterator(java.util.Iterator) TimestampData(org.apache.flink.table.data.TimestampData) ObjectNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode) TableException(org.apache.flink.table.api.TableException) DecimalData(org.apache.flink.table.data.DecimalData) IOException(java.io.IOException) ArrayType(org.apache.flink.table.types.logical.ArrayType) TimestampFormat(org.apache.flink.formats.common.TimestampFormat) Serializable(java.io.Serializable) SQL_TIME_FORMAT(org.apache.flink.formats.common.TimeFormats.SQL_TIME_FORMAT) StringData(org.apache.flink.table.data.StringData) LogicalType(org.apache.flink.table.types.logical.LogicalType) LocalDate(java.time.LocalDate) Internal(org.apache.flink.annotation.Internal) ObjectNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode) GenericRowData(org.apache.flink.table.data.GenericRowData) JsonNode(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)

Example 55 with GenericRowData

use of org.apache.flink.table.data.GenericRowData in project flink by apache.

the class DebeziumJsonDeserializationSchema method deserialize.

@Override
public void deserialize(byte[] message, Collector<RowData> out) throws IOException {
    if (message == null || message.length == 0) {
        // skip tombstone messages
        return;
    }
    try {
        GenericRowData row = (GenericRowData) jsonDeserializer.deserialize(message);
        GenericRowData payload;
        if (schemaInclude) {
            payload = (GenericRowData) row.getField(0);
        } else {
            payload = row;
        }
        GenericRowData before = (GenericRowData) payload.getField(0);
        GenericRowData after = (GenericRowData) payload.getField(1);
        String op = payload.getField(2).toString();
        if (OP_CREATE.equals(op) || OP_READ.equals(op)) {
            after.setRowKind(RowKind.INSERT);
            emitRow(row, after, out);
        } else if (OP_UPDATE.equals(op)) {
            if (before == null) {
                throw new IllegalStateException(String.format(REPLICA_IDENTITY_EXCEPTION, "UPDATE"));
            }
            before.setRowKind(RowKind.UPDATE_BEFORE);
            after.setRowKind(RowKind.UPDATE_AFTER);
            emitRow(row, before, out);
            emitRow(row, after, out);
        } else if (OP_DELETE.equals(op)) {
            if (before == null) {
                throw new IllegalStateException(String.format(REPLICA_IDENTITY_EXCEPTION, "DELETE"));
            }
            before.setRowKind(RowKind.DELETE);
            emitRow(row, before, out);
        } else {
            if (!ignoreParseErrors) {
                throw new IOException(format("Unknown \"op\" value \"%s\". The Debezium JSON message is '%s'", op, new String(message)));
            }
        }
    } catch (Throwable t) {
        // a big try catch to protect the processing.
        if (!ignoreParseErrors) {
            throw new IOException(format("Corrupt Debezium JSON message '%s'.", new String(message)), t);
        }
    }
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData) IOException(java.io.IOException)

Aggregations

GenericRowData (org.apache.flink.table.data.GenericRowData)94 RowData (org.apache.flink.table.data.RowData)32 JoinedRowData (org.apache.flink.table.data.utils.JoinedRowData)16 Test (org.junit.Test)14 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)13 RowType (org.apache.flink.table.types.logical.RowType)13 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 IntType (org.apache.flink.table.types.logical.IntType)11 List (java.util.List)9 LogicalType (org.apache.flink.table.types.logical.LogicalType)9 GenericArrayData (org.apache.flink.table.data.GenericArrayData)6 StringData (org.apache.flink.table.data.StringData)6 Arrays (java.util.Arrays)5 HashMap (java.util.HashMap)5 OutputStream (java.io.OutputStream)4 PrintStream (java.io.PrintStream)4 Collections (java.util.Collections)4 Random (java.util.Random)4 Consumer (java.util.function.Consumer)4