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);
};
}
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));
}
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);
}
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;
});
}
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) };
}
Aggregations