Search in sources :

Example 6 with ArrayData

use of org.apache.flink.table.data.ArrayData 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 7 with ArrayData

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

the class MapDataUtil method convertToJavaMap.

/**
 * Converts a {@link MapData} into Java {@link Map}, the keys and values of the Java map still
 * holds objects of internal data structures.
 */
public static Map<Object, Object> convertToJavaMap(MapData map, LogicalType keyType, LogicalType valueType) {
    ArrayData keyArray = map.keyArray();
    ArrayData valueArray = map.valueArray();
    Map<Object, Object> javaMap = new HashMap<>();
    ArrayData.ElementGetter keyGetter = ArrayData.createElementGetter(keyType);
    ArrayData.ElementGetter valueGetter = ArrayData.createElementGetter(valueType);
    for (int i = 0; i < map.size(); i++) {
        Object key = keyGetter.getElementOrNull(keyArray, i);
        Object value = valueGetter.getElementOrNull(valueArray, i);
        javaMap.put(key, value);
    }
    return javaMap;
}
Also used : HashMap(java.util.HashMap) ArrayData(org.apache.flink.table.data.ArrayData)

Example 8 with ArrayData

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

the class MapMapConverter method toExternal.

@Override
public Map<K, V> toExternal(MapData internal) {
    final ArrayData keyArray = internal.keyArray();
    final ArrayData valueArray = internal.valueArray();
    final int length = internal.size();
    final Map<K, V> map = new HashMap<>();
    for (int pos = 0; pos < length; pos++) {
        final Object keyValue = keyConverter.elementGetter.getElementOrNull(keyArray, pos);
        final Object valueValue = valueConverter.elementGetter.getElementOrNull(valueArray, pos);
        map.put(keyConverter.elementConverter.toExternalOrNull(keyValue), valueConverter.elementConverter.toExternalOrNull(valueValue));
    }
    return map;
}
Also used : HashMap(java.util.HashMap) ArrayData(org.apache.flink.table.data.ArrayData)

Example 9 with ArrayData

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

the class MapDataSerializer method toBinaryMap.

public BinaryMapData toBinaryMap(MapData from) {
    if (from instanceof BinaryMapData) {
        return (BinaryMapData) from;
    }
    int numElements = from.size();
    if (reuseKeyArray == null) {
        reuseKeyArray = new BinaryArrayData();
    }
    if (reuseValueArray == null) {
        reuseValueArray = new BinaryArrayData();
    }
    if (reuseKeyWriter == null || reuseKeyWriter.getNumElements() != numElements) {
        reuseKeyWriter = new BinaryArrayWriter(reuseKeyArray, numElements, BinaryArrayData.calculateFixLengthPartSize(keyType));
    } else {
        reuseKeyWriter.reset();
    }
    if (reuseValueWriter == null || reuseValueWriter.getNumElements() != numElements) {
        reuseValueWriter = new BinaryArrayWriter(reuseValueArray, numElements, BinaryArrayData.calculateFixLengthPartSize(valueType));
    } else {
        reuseValueWriter.reset();
    }
    ArrayData keyArray = from.keyArray();
    ArrayData valueArray = from.valueArray();
    for (int i = 0; i < from.size(); i++) {
        Object key = keyGetter.getElementOrNull(keyArray, i);
        Object value = valueGetter.getElementOrNull(valueArray, i);
        if (key == null) {
            reuseKeyWriter.setNullAt(i, keyType);
        } else {
            BinaryWriter.write(reuseKeyWriter, i, key, keyType, keySerializer);
        }
        if (value == null) {
            reuseValueWriter.setNullAt(i, valueType);
        } else {
            BinaryWriter.write(reuseValueWriter, i, value, valueType, valueSerializer);
        }
    }
    reuseKeyWriter.complete();
    reuseValueWriter.complete();
    return BinaryMapData.valueOf(reuseKeyArray, reuseValueArray);
}
Also used : BinaryMapData(org.apache.flink.table.data.binary.BinaryMapData) BinaryArrayWriter(org.apache.flink.table.data.writer.BinaryArrayWriter) BinaryArrayData(org.apache.flink.table.data.binary.BinaryArrayData) BinaryArrayData(org.apache.flink.table.data.binary.BinaryArrayData) ArrayData(org.apache.flink.table.data.ArrayData)

Example 10 with ArrayData

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

the class RowDataToAvroConverters method createArrayConverter.

private static RowDataToAvroConverter createArrayConverter(ArrayType arrayType) {
    LogicalType elementType = arrayType.getElementType();
    final ArrayData.ElementGetter elementGetter = ArrayData.createElementGetter(elementType);
    final RowDataToAvroConverter elementConverter = createConverter(arrayType.getElementType());
    return new RowDataToAvroConverter() {

        private static final long serialVersionUID = 1L;

        @Override
        public Object convert(Schema schema, Object object) {
            final Schema elementSchema = schema.getElementType();
            ArrayData arrayData = (ArrayData) object;
            List<Object> list = new ArrayList<>();
            for (int i = 0; i < arrayData.size(); ++i) {
                list.add(elementConverter.convert(elementSchema, elementGetter.getElementOrNull(arrayData, i)));
            }
            return list;
        }
    };
}
Also used : Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) LogicalType(org.apache.flink.table.types.logical.LogicalType) ArrayData(org.apache.flink.table.data.ArrayData)

Aggregations

ArrayData (org.apache.flink.table.data.ArrayData)16 LogicalType (org.apache.flink.table.types.logical.LogicalType)6 GenericRowData (org.apache.flink.table.data.GenericRowData)5 RowData (org.apache.flink.table.data.RowData)5 ArrayType (org.apache.flink.table.types.logical.ArrayType)4 HashMap (java.util.HashMap)3 JsonNode (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode)3 DecimalData (org.apache.flink.table.data.DecimalData)3 GenericArrayData (org.apache.flink.table.data.GenericArrayData)3 MapData (org.apache.flink.table.data.MapData)3 TimestampData (org.apache.flink.table.data.TimestampData)3 Serializable (java.io.Serializable)2 LocalDate (java.time.LocalDate)2 LocalTime (java.time.LocalTime)2 ISO_LOCAL_DATE (java.time.format.DateTimeFormatter.ISO_LOCAL_DATE)2 Arrays (java.util.Arrays)2 Schema (org.apache.avro.Schema)2 Internal (org.apache.flink.annotation.Internal)2 SQL_TIMESTAMP_FORMAT (org.apache.flink.formats.common.TimeFormats.SQL_TIMESTAMP_FORMAT)2 SQL_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT (org.apache.flink.formats.common.TimeFormats.SQL_TIMESTAMP_WITH_LOCAL_TIMEZONE_FORMAT)2