Search in sources :

Example 46 with BinaryRowWriter

use of org.apache.flink.table.data.writer.BinaryRowWriter in project flink by apache.

the class RowDataSerializer method toBinaryRow.

/**
 * Convert {@link RowData} into {@link BinaryRowData}. TODO modify it to code gen.
 */
@Override
public BinaryRowData toBinaryRow(RowData row) {
    if (row instanceof BinaryRowData) {
        return (BinaryRowData) row;
    }
    if (reuseRow == null) {
        reuseRow = new BinaryRowData(types.length);
        reuseWriter = new BinaryRowWriter(reuseRow);
    }
    reuseWriter.reset();
    reuseWriter.writeRowKind(row.getRowKind());
    for (int i = 0; i < types.length; i++) {
        if (row.isNullAt(i)) {
            reuseWriter.setNullAt(i);
        } else {
            BinaryWriter.write(reuseWriter, i, fieldGetters[i].getFieldOrNull(row), types[i], fieldSerializers[i]);
        }
    }
    reuseWriter.complete();
    return reuseRow;
}
Also used : BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter)

Example 47 with BinaryRowWriter

use of org.apache.flink.table.data.writer.BinaryRowWriter in project flink by apache.

the class BytesMultiMapTestBase method genValues.

private BinaryRowData[] genValues(int num) {
    BinaryRowData[] values = new BinaryRowData[num];
    final Random rnd = new Random(RANDOM_SEED);
    for (int i = 0; i < num; i++) {
        values[i] = new BinaryRowData(2);
        BinaryRowWriter writer = new BinaryRowWriter(values[i]);
        writer.writeString(0, StringData.fromString("string" + rnd.nextInt()));
        writer.writeInt(1, rnd.nextInt());
        writer.complete();
    }
    return values;
}
Also used : Random(java.util.Random) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter)

Example 48 with BinaryRowWriter

use of org.apache.flink.table.data.writer.BinaryRowWriter 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)

Example 49 with BinaryRowWriter

use of org.apache.flink.table.data.writer.BinaryRowWriter in project flink by apache.

the class SliceAssignerTestBase method row.

protected static RowData row(long timestamp) {
    BinaryRowData binaryRowData = new BinaryRowData(1);
    BinaryRowWriter writer = new BinaryRowWriter(binaryRowData);
    writer.writeTimestamp(0, TimestampData.fromEpochMillis(timestamp), 3);
    writer.complete();
    return binaryRowData;
}
Also used : BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter)

Example 50 with BinaryRowWriter

use of org.apache.flink.table.data.writer.BinaryRowWriter in project flink by apache.

the class BinaryRowSerializerTest method createRow.

private static BinaryRowData createRow(String f0, int f1) {
    BinaryRowData row = new BinaryRowData(2);
    BinaryRowWriter writer = new BinaryRowWriter(row);
    writer.writeString(0, StringData.fromString(f0));
    writer.writeInt(1, f1);
    writer.complete();
    return row;
}
Also used : BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter)

Aggregations

BinaryRowWriter (org.apache.flink.table.data.writer.BinaryRowWriter)54 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)53 Test (org.junit.Test)32 Random (java.util.Random)6 BinaryArrayData (org.apache.flink.table.data.binary.BinaryArrayData)6 BinaryRowDataSerializer (org.apache.flink.table.runtime.typeutils.BinaryRowDataSerializer)6 RowDataSerializer (org.apache.flink.table.runtime.typeutils.RowDataSerializer)6 MemorySegment (org.apache.flink.core.memory.MemorySegment)5 BinaryArrayWriter (org.apache.flink.table.data.writer.BinaryArrayWriter)5 ArrayDataSerializer (org.apache.flink.table.runtime.typeutils.ArrayDataSerializer)5 ArrayList (java.util.ArrayList)4 GenericRowData (org.apache.flink.table.data.GenericRowData)4 MapDataSerializer (org.apache.flink.table.runtime.typeutils.MapDataSerializer)4 LogicalType (org.apache.flink.table.types.logical.LogicalType)4 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)3 StringData.fromString (org.apache.flink.table.data.StringData.fromString)3 TimestampData (org.apache.flink.table.data.TimestampData)3 RawValueDataSerializer (org.apache.flink.table.runtime.typeutils.RawValueDataSerializer)3 BigDecimal (java.math.BigDecimal)2 HashMap (java.util.HashMap)2