Search in sources :

Example 1 with GenericMapData

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

the class AvroToRowDataConverters method createMapConverter.

private static AvroToRowDataConverter createMapConverter(LogicalType type) {
    final AvroToRowDataConverter keyConverter = createConverter(DataTypes.STRING().getLogicalType());
    final AvroToRowDataConverter valueConverter = createNullableConverter(extractValueTypeToAvroMap(type));
    return avroObject -> {
        final Map<?, ?> map = (Map<?, ?>) avroObject;
        Map<Object, Object> result = new HashMap<>();
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            Object key = keyConverter.convert(entry.getKey());
            Object value = valueConverter.convert(entry.getValue());
            result.put(key, value);
        }
        return new GenericMapData(result);
    };
}
Also used : ChronoField(java.time.temporal.ChronoField) Array(java.lang.reflect.Array) GenericArrayData(org.apache.flink.table.data.GenericArrayData) HashMap(java.util.HashMap) RowType(org.apache.flink.table.types.logical.RowType) ByteBuffer(java.nio.ByteBuffer) GenericRowData(org.apache.flink.table.data.GenericRowData) DecimalType(org.apache.flink.table.types.logical.DecimalType) GenericMapData(org.apache.flink.table.data.GenericMapData) Map(java.util.Map) LocalTime(java.time.LocalTime) LogicalTypeUtils(org.apache.flink.table.types.logical.utils.LogicalTypeUtils) IndexedRecord(org.apache.avro.generic.IndexedRecord) GenericRecord(org.apache.avro.generic.GenericRecord) RowData(org.apache.flink.table.data.RowData) GenericFixed(org.apache.avro.generic.GenericFixed) TimestampData(org.apache.flink.table.data.TimestampData) DataTypes(org.apache.flink.table.api.DataTypes) DecimalData(org.apache.flink.table.data.DecimalData) ArrayType(org.apache.flink.table.types.logical.ArrayType) Instant(java.time.Instant) AvroSchemaConverter.extractValueTypeToAvroMap(org.apache.flink.formats.avro.typeutils.AvroSchemaConverter.extractValueTypeToAvroMap) Serializable(java.io.Serializable) StringData(org.apache.flink.table.data.StringData) List(java.util.List) LogicalType(org.apache.flink.table.types.logical.LogicalType) LocalDate(java.time.LocalDate) Internal(org.apache.flink.annotation.Internal) GenericMapData(org.apache.flink.table.data.GenericMapData) HashMap(java.util.HashMap) Map(java.util.Map) AvroSchemaConverter.extractValueTypeToAvroMap(org.apache.flink.formats.avro.typeutils.AvroSchemaConverter.extractValueTypeToAvroMap)

Example 2 with GenericMapData

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

the class JsonRowDataSerDeSchemaTest method testSerializationMapNullKey.

@Test
public void testSerializationMapNullKey() throws Exception {
    RowType rowType = (RowType) ROW(FIELD("nestedMap", MAP(STRING(), MAP(STRING(), INT())))).getLogicalType();
    // test data
    // use LinkedHashMap to make sure entries order
    Map<StringData, Integer> map = new LinkedHashMap<>();
    map.put(StringData.fromString("no-null key"), 1);
    map.put(StringData.fromString(null), 2);
    GenericMapData mapData = new GenericMapData(map);
    Map<StringData, GenericMapData> nestedMap = new LinkedHashMap<>();
    nestedMap.put(StringData.fromString("no-null key"), mapData);
    nestedMap.put(StringData.fromString(null), mapData);
    GenericMapData nestedMapData = new GenericMapData(nestedMap);
    GenericRowData rowData = new GenericRowData(1);
    rowData.setField(0, nestedMapData);
    JsonRowDataSerializationSchema serializationSchema1 = new JsonRowDataSerializationSchema(rowType, TimestampFormat.SQL, JsonFormatOptions.MapNullKeyMode.FAIL, "null", true);
    // expect message for serializationSchema1
    String errorMessage1 = "JSON format doesn't support to serialize map data with null keys." + " You can drop null key entries or encode null in literals by specifying map-null-key.mode option.";
    JsonRowDataSerializationSchema serializationSchema2 = new JsonRowDataSerializationSchema(rowType, TimestampFormat.SQL, JsonFormatOptions.MapNullKeyMode.DROP, "null", true);
    // expect result for serializationSchema2
    String expectResult2 = "{\"nestedMap\":{\"no-null key\":{\"no-null key\":1}}}";
    JsonRowDataSerializationSchema serializationSchema3 = new JsonRowDataSerializationSchema(rowType, TimestampFormat.SQL, JsonFormatOptions.MapNullKeyMode.LITERAL, "nullKey", true);
    // expect result for serializationSchema3
    String expectResult3 = "{\"nestedMap\":{\"no-null key\":{\"no-null key\":1,\"nullKey\":2},\"nullKey\":{\"no-null key\":1,\"nullKey\":2}}}";
    try {
        // throw exception when mapNullKey Mode is fail
        serializationSchema1.serialize(rowData);
        Assert.fail("expecting exception message: " + errorMessage1);
    } catch (Throwable t) {
        assertThat(t, FlinkMatchers.containsMessage(errorMessage1));
    }
    // mapNullKey Mode is drop
    byte[] actual2 = serializationSchema2.serialize(rowData);
    assertEquals(expectResult2, new String(actual2));
    // mapNullKey Mode is literal
    byte[] actual3 = serializationSchema3.serialize(rowData);
    assertEquals(expectResult3, new String(actual3));
}
Also used : GenericMapData(org.apache.flink.table.data.GenericMapData) RowType(org.apache.flink.table.types.logical.RowType) GenericRowData(org.apache.flink.table.data.GenericRowData) StringData(org.apache.flink.table.data.StringData) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 3 with GenericMapData

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

the class InternalDataUtils method toGenericMap.

static GenericMapData toGenericMap(MapData mapData, LogicalType logicalType) {
    final LogicalType keyType = logicalType.is(LogicalTypeRoot.MULTISET) ? ((MultisetType) logicalType).getElementType() : ((MapType) logicalType).getKeyType();
    final LogicalType valueType = logicalType.is(LogicalTypeRoot.MULTISET) ? new IntType(false) : ((MapType) logicalType).getValueType();
    final ArrayData.ElementGetter keyGetter = ArrayData.createElementGetter(keyType);
    final ArrayData.ElementGetter valueGetter = ArrayData.createElementGetter(valueType);
    final ArrayData keys = mapData.keyArray();
    final ArrayData values = mapData.valueArray();
    final LinkedHashMap<Object, Object> newMap = new LinkedHashMap<>();
    for (int i = 0; i < mapData.size(); i++) {
        Object key = null;
        Object value = null;
        if (!keys.isNullAt(i)) {
            key = toGenericInternalData(keyGetter.getElementOrNull(keys, i), keyType);
        }
        if (!values.isNullAt(i)) {
            value = toGenericInternalData(valueGetter.getElementOrNull(values, i), valueType);
        }
        newMap.put(key, value);
    }
    return new GenericMapData(newMap);
}
Also used : GenericMapData(org.apache.flink.table.data.GenericMapData) LogicalType(org.apache.flink.table.types.logical.LogicalType) IntType(org.apache.flink.table.types.logical.IntType) GenericArrayData(org.apache.flink.table.data.GenericArrayData) ArrayData(org.apache.flink.table.data.ArrayData) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with GenericMapData

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

the class MapDataAssert method asGeneric.

public MapDataAssert asGeneric(LogicalType logicalType) {
    GenericMapData actual = InternalDataUtils.toGenericMap(this.actual, logicalType);
    return new MapDataAssert(actual).usingComparator((x, y) -> {
        // Avoid converting actual again
        x = x == actual ? x : InternalDataUtils.toGenericMap(x, logicalType);
        y = y == actual ? y : InternalDataUtils.toGenericMap(y, logicalType);
        if (Objects.equals(x, y)) {
            return 0;
        }
        return Objects.hashCode(x) < Objects.hashCode(y) ? -1 : 1;
    });
}
Also used : GenericMapData(org.apache.flink.table.data.GenericMapData)

Example 5 with GenericMapData

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

the class MapDataSerializerTest method getTestData.

@Override
protected MapData[] getTestData() {
    Map<Object, Object> first = new HashMap<>();
    first.put(1L, -100.1F);
    BinaryArrayData keyBinary = BinaryArrayData.fromPrimitiveArray(new long[] { 10L, 20L });
    BinaryArrayData valueBinary = new BinaryArrayData();
    BinaryArrayWriter writer = new BinaryArrayWriter(valueBinary, 2, 4);
    BinaryArrayWriter.NullSetter nullSetter = BinaryArrayWriter.createNullSetter(FLOAT);
    writer.writeFloat(0, 10.2F);
    nullSetter.setNull(writer, 1);
    writer.complete();
    return new MapData[] { new GenericMapData(first), BinaryMapData.valueOf(keyBinary, valueBinary) };
}
Also used : GenericMapData(org.apache.flink.table.data.GenericMapData) HashMap(java.util.HashMap) MapData(org.apache.flink.table.data.MapData) BinaryMapData(org.apache.flink.table.data.binary.BinaryMapData) GenericMapData(org.apache.flink.table.data.GenericMapData) BinaryArrayWriter(org.apache.flink.table.data.writer.BinaryArrayWriter) BinaryArrayData(org.apache.flink.table.data.binary.BinaryArrayData)

Aggregations

GenericMapData (org.apache.flink.table.data.GenericMapData)8 HashMap (java.util.HashMap)5 GenericRowData (org.apache.flink.table.data.GenericRowData)5 GenericArrayData (org.apache.flink.table.data.GenericArrayData)4 StringData (org.apache.flink.table.data.StringData)4 RowType (org.apache.flink.table.types.logical.RowType)4 Map (java.util.Map)3 RowData (org.apache.flink.table.data.RowData)3 TimestampData (org.apache.flink.table.data.TimestampData)3 ArrayType (org.apache.flink.table.types.logical.ArrayType)3 IntType (org.apache.flink.table.types.logical.IntType)3 Serializable (java.io.Serializable)2 Array (java.lang.reflect.Array)2 LocalDate (java.time.LocalDate)2 LocalTime (java.time.LocalTime)2 LinkedHashMap (java.util.LinkedHashMap)2 Internal (org.apache.flink.annotation.Internal)2 DecimalData (org.apache.flink.table.data.DecimalData)2 BinaryStringData (org.apache.flink.table.data.binary.BinaryStringData)2 LogicalType (org.apache.flink.table.types.logical.LogicalType)2