Search in sources :

Example 6 with ArrayDataSerializer

use of org.apache.flink.table.runtime.typeutils.ArrayDataSerializer in project flink by apache.

the class BinaryRowDataTest method testGenericArray.

@Test
public void testGenericArray() {
    // 1. array test
    Integer[] javaArray = { 6, null, 666 };
    GenericArrayData array = new GenericArrayData(javaArray);
    assertEquals(array.getInt(0), 6);
    assertTrue(array.isNullAt(1));
    assertEquals(array.getInt(2), 666);
    // 2. test write array to binary row
    BinaryRowData row2 = new BinaryRowData(1);
    BinaryRowWriter writer2 = new BinaryRowWriter(row2);
    ArrayDataSerializer serializer = new ArrayDataSerializer(DataTypes.INT().getLogicalType());
    writer2.writeArray(0, array, serializer);
    writer2.complete();
    ArrayData array2 = row2.getArray(0);
    assertEquals(6, array2.getInt(0));
    assertTrue(array2.isNullAt(1));
    assertEquals(666, array2.getInt(2));
}
Also used : BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) ArrayDataSerializer(org.apache.flink.table.runtime.typeutils.ArrayDataSerializer) BinaryArrayData(org.apache.flink.table.data.binary.BinaryArrayData) Test(org.junit.Test)

Example 7 with ArrayDataSerializer

use of org.apache.flink.table.runtime.typeutils.ArrayDataSerializer in project flink by apache.

the class StreamRecordUtils method binaryrow.

/**
 * Receives a object array, generates a BinaryRowData based on the array.
 *
 * @param fields input object array
 * @return generated BinaryRowData.
 */
public static BinaryRowData binaryrow(Object... fields) {
    BinaryRowData row = new BinaryRowData(fields.length);
    BinaryRowWriter writer = new BinaryRowWriter(row);
    for (int j = 0; j < fields.length; j++) {
        Object value = fields[j];
        if (value == null) {
            writer.setNullAt(j);
        } else if (value instanceof Byte) {
            writer.writeByte(j, (Byte) value);
        } else if (value instanceof Short) {
            writer.writeShort(j, (Short) value);
        } else if (value instanceof Integer) {
            writer.writeInt(j, (Integer) value);
        } else if (value instanceof String) {
            writer.writeString(j, StringData.fromString((String) value));
        } else if (value instanceof Double) {
            writer.writeDouble(j, (Double) value);
        } else if (value instanceof Float) {
            writer.writeFloat(j, (Float) value);
        } else if (value instanceof Long) {
            writer.writeLong(j, (Long) value);
        } else if (value instanceof Boolean) {
            writer.writeBoolean(j, (Boolean) value);
        } else if (value instanceof byte[]) {
            writer.writeBinary(j, (byte[]) value);
        } else if (value instanceof DecimalData) {
            DecimalData decimal = (DecimalData) value;
            writer.writeDecimal(j, decimal, decimal.precision());
        } else if (value instanceof TimestampData) {
            TimestampData timestamp = (TimestampData) value;
            writer.writeTimestamp(j, timestamp, 3);
        } else if (value instanceof Tuple2 && ((Tuple2) value).f0 instanceof TimestampData) {
            TimestampData timestamp = (TimestampData) ((Tuple2) value).f0;
            writer.writeTimestamp(j, timestamp, (int) ((Tuple2) value).f1);
        } else if (value instanceof Tuple2 && ((Tuple2) value).f0 instanceof ArrayData) {
            ArrayData array = (ArrayData) ((Tuple2) value).f0;
            ArrayDataSerializer serializer = (ArrayDataSerializer) ((Tuple2) value).f1;
            writer.writeArray(j, array, serializer);
        } else if (value instanceof Tuple2 && ((Tuple2) value).f0 instanceof RowData) {
            RowData rowData = ((RowData) ((Tuple2) value).f0);
            RowDataSerializer serializer = (RowDataSerializer) ((Tuple2) value).f1;
            writer.writeRow(j, rowData, serializer);
        } else {
            throw new RuntimeException("Not support yet!");
        }
    }
    writer.complete();
    return row;
}
Also used : TimestampData(org.apache.flink.table.data.TimestampData) StringData.fromString(org.apache.flink.table.data.StringData.fromString) ArrayDataSerializer(org.apache.flink.table.runtime.typeutils.ArrayDataSerializer) DecimalData(org.apache.flink.table.data.DecimalData) RowData(org.apache.flink.table.data.RowData) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) GenericRowData(org.apache.flink.table.data.GenericRowData) Tuple2(org.apache.flink.api.java.tuple.Tuple2) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) ArrayData(org.apache.flink.table.data.ArrayData) RowDataSerializer(org.apache.flink.table.runtime.typeutils.RowDataSerializer)

Aggregations

ArrayDataSerializer (org.apache.flink.table.runtime.typeutils.ArrayDataSerializer)7 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)6 BinaryRowWriter (org.apache.flink.table.data.writer.BinaryRowWriter)5 BinaryArrayData (org.apache.flink.table.data.binary.BinaryArrayData)4 Test (org.junit.Test)4 BinaryArrayWriter (org.apache.flink.table.data.writer.BinaryArrayWriter)3 RowDataSerializer (org.apache.flink.table.runtime.typeutils.RowDataSerializer)3 GenericRowData (org.apache.flink.table.data.GenericRowData)2 RowData (org.apache.flink.table.data.RowData)2 MapDataSerializer (org.apache.flink.table.runtime.typeutils.MapDataSerializer)2 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 ArrayData (org.apache.flink.table.data.ArrayData)1 DecimalData (org.apache.flink.table.data.DecimalData)1 GenericArrayData (org.apache.flink.table.data.GenericArrayData)1 StringData (org.apache.flink.table.data.StringData)1 StringData.fromString (org.apache.flink.table.data.StringData.fromString)1 TimestampData (org.apache.flink.table.data.TimestampData)1 IntType (org.apache.flink.table.types.logical.IntType)1 VarCharType (org.apache.flink.table.types.logical.VarCharType)1