Search in sources :

Example 1 with TimestampData

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

the class DataGenTableSourceFactoryTest method testSource.

@Test
public void testSource() throws Exception {
    DescriptorProperties descriptor = new DescriptorProperties();
    descriptor.putString(FactoryUtil.CONNECTOR.key(), "datagen");
    descriptor.putLong(DataGenConnectorOptions.ROWS_PER_SECOND.key(), 100);
    descriptor.putString(DataGenConnectorOptionsUtil.FIELDS + ".f0." + DataGenConnectorOptionsUtil.KIND, DataGenConnectorOptionsUtil.RANDOM);
    descriptor.putLong(DataGenConnectorOptionsUtil.FIELDS + ".f0." + DataGenConnectorOptionsUtil.LENGTH, 20);
    descriptor.putString(DataGenConnectorOptionsUtil.FIELDS + ".f1." + DataGenConnectorOptionsUtil.KIND, DataGenConnectorOptionsUtil.RANDOM);
    descriptor.putLong(DataGenConnectorOptionsUtil.FIELDS + ".f1." + DataGenConnectorOptionsUtil.MIN, 10);
    descriptor.putLong(DataGenConnectorOptionsUtil.FIELDS + ".f1." + DataGenConnectorOptionsUtil.MAX, 100);
    descriptor.putString(DataGenConnectorOptionsUtil.FIELDS + ".f2." + DataGenConnectorOptionsUtil.KIND, DataGenConnectorOptionsUtil.SEQUENCE);
    descriptor.putLong(DataGenConnectorOptionsUtil.FIELDS + ".f2." + DataGenConnectorOptionsUtil.START, 50);
    descriptor.putLong(DataGenConnectorOptionsUtil.FIELDS + ".f2." + DataGenConnectorOptionsUtil.END, 60);
    descriptor.putString(DataGenConnectorOptionsUtil.FIELDS + ".f3." + DataGenConnectorOptionsUtil.KIND, DataGenConnectorOptionsUtil.RANDOM);
    descriptor.putString(DataGenConnectorOptionsUtil.FIELDS + ".f3." + DataGenConnectorOptionsUtil.MAX_PAST, "5s");
    final long begin = System.currentTimeMillis();
    List<RowData> results = runGenerator(SCHEMA, descriptor);
    final long end = System.currentTimeMillis();
    Assert.assertEquals(11, results.size());
    for (int i = 0; i < results.size(); i++) {
        RowData row = results.get(i);
        Assert.assertEquals(20, row.getString(0).toString().length());
        long f1 = row.getLong(1);
        Assert.assertTrue(f1 >= 10 && f1 <= 100);
        Assert.assertEquals(i + 50, row.getLong(2));
        final TimestampData f3 = row.getTimestamp(3, 3);
        Assert.assertTrue(f3.getMillisecond() >= begin - 5000 && f3.getMillisecond() <= end);
    }
}
Also used : GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) TimestampData(org.apache.flink.table.data.TimestampData) DescriptorProperties(org.apache.flink.table.descriptors.DescriptorProperties) DataGeneratorSourceTest(org.apache.flink.streaming.api.functions.source.datagen.DataGeneratorSourceTest) Test(org.junit.Test)

Example 2 with TimestampData

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

the class RowDataToAvroConverters method createConverter.

// --------------------------------------------------------------------------------
// IMPORTANT! We use anonymous classes instead of lambdas for a reason here. It is
// necessary because the maven shade plugin cannot relocate classes in
// SerializedLambdas (MSHADE-260). On the other hand we want to relocate Avro for
// sql-client uber jars.
// --------------------------------------------------------------------------------
/**
 * Creates a runtime converter according to the given logical type that converts objects of
 * Flink Table & SQL internal data structures to corresponding Avro data structures.
 */
public static RowDataToAvroConverter createConverter(LogicalType type) {
    final RowDataToAvroConverter converter;
    switch(type.getTypeRoot()) {
        case NULL:
            converter = new RowDataToAvroConverter() {

                private static final long serialVersionUID = 1L;

                @Override
                public Object convert(Schema schema, Object object) {
                    return null;
                }
            };
            break;
        case TINYINT:
            converter = new RowDataToAvroConverter() {

                private static final long serialVersionUID = 1L;

                @Override
                public Object convert(Schema schema, Object object) {
                    return ((Byte) object).intValue();
                }
            };
            break;
        case SMALLINT:
            converter = new RowDataToAvroConverter() {

                private static final long serialVersionUID = 1L;

                @Override
                public Object convert(Schema schema, Object object) {
                    return ((Short) object).intValue();
                }
            };
            break;
        // boolean
        case BOOLEAN:
        // int
        case INTEGER:
        // long
        case INTERVAL_YEAR_MONTH:
        // long
        case BIGINT:
        // long
        case INTERVAL_DAY_TIME:
        // float
        case FLOAT:
        // double
        case DOUBLE:
        // int
        case TIME_WITHOUT_TIME_ZONE:
        case // int
        DATE:
            converter = new RowDataToAvroConverter() {

                private static final long serialVersionUID = 1L;

                @Override
                public Object convert(Schema schema, Object object) {
                    return object;
                }
            };
            break;
        case CHAR:
        case VARCHAR:
            converter = new RowDataToAvroConverter() {

                private static final long serialVersionUID = 1L;

                @Override
                public Object convert(Schema schema, Object object) {
                    return new Utf8(object.toString());
                }
            };
            break;
        case BINARY:
        case VARBINARY:
            converter = new RowDataToAvroConverter() {

                private static final long serialVersionUID = 1L;

                @Override
                public Object convert(Schema schema, Object object) {
                    return ByteBuffer.wrap((byte[]) object);
                }
            };
            break;
        case TIMESTAMP_WITHOUT_TIME_ZONE:
            converter = new RowDataToAvroConverter() {

                private static final long serialVersionUID = 1L;

                @Override
                public Object convert(Schema schema, Object object) {
                    return ((TimestampData) object).toInstant().toEpochMilli();
                }
            };
            break;
        case DECIMAL:
            converter = new RowDataToAvroConverter() {

                private static final long serialVersionUID = 1L;

                @Override
                public Object convert(Schema schema, Object object) {
                    return ByteBuffer.wrap(((DecimalData) object).toUnscaledBytes());
                }
            };
            break;
        case ARRAY:
            converter = createArrayConverter((ArrayType) type);
            break;
        case ROW:
            converter = createRowConverter((RowType) type);
            break;
        case MAP:
        case MULTISET:
            converter = createMapConverter(type);
            break;
        case RAW:
        default:
            throw new UnsupportedOperationException("Unsupported type: " + type);
    }
    // wrap into nullable converter
    return new RowDataToAvroConverter() {

        private static final long serialVersionUID = 1L;

        @Override
        public Object convert(Schema schema, Object object) {
            if (object == null) {
                return null;
            }
            // get actual schema if it is a nullable schema
            Schema actualSchema;
            if (schema.getType() == Schema.Type.UNION) {
                List<Schema> types = schema.getTypes();
                int size = types.size();
                if (size == 2 && types.get(1).getType() == Schema.Type.NULL) {
                    actualSchema = types.get(0);
                } else if (size == 2 && types.get(0).getType() == Schema.Type.NULL) {
                    actualSchema = types.get(1);
                } else {
                    throw new IllegalArgumentException("The Avro schema is not a nullable type: " + schema.toString());
                }
            } else {
                actualSchema = schema;
            }
            return converter.convert(actualSchema, object);
        }
    };
}
Also used : TimestampData(org.apache.flink.table.data.TimestampData) Schema(org.apache.avro.Schema) RowType(org.apache.flink.table.types.logical.RowType) DecimalData(org.apache.flink.table.data.DecimalData) ArrayType(org.apache.flink.table.types.logical.ArrayType) Utf8(org.apache.avro.util.Utf8)

Example 3 with TimestampData

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

the class OrcBulkRowDataWriterTest method readMap.

/**
 * Read MapColumnVector with specify schema {@literal
 * map<string,struct<_col3_col0:string,_col3_col1:timestamp>>}.
 */
private static MapData readMap(MapColumnVector mapVector, int row) {
    int offset = (int) mapVector.offsets[row];
    StringData keyData = readStringData((BytesColumnVector) mapVector.keys, offset);
    GenericRowData valueData = new GenericRowData(2);
    StructColumnVector structVector = (StructColumnVector) mapVector.values;
    BytesColumnVector bytesVector = (BytesColumnVector) structVector.fields[0];
    TimestampColumnVector timestampVector = (TimestampColumnVector) structVector.fields[1];
    StringData strValueData = readStringData(bytesVector, offset);
    TimestampData timestampData = readTimestamp(timestampVector, offset);
    valueData.setField(0, strValueData);
    valueData.setField(1, timestampData);
    Map<StringData, RowData> mapDataMap = new HashMap<>();
    mapDataMap.put(keyData, valueData);
    return new GenericMapData(mapDataMap);
}
Also used : TimestampColumnVector(org.apache.hadoop.hive.ql.exec.vector.TimestampColumnVector) GenericMapData(org.apache.flink.table.data.GenericMapData) TimestampData(org.apache.flink.table.data.TimestampData) GenericRowData(org.apache.flink.table.data.GenericRowData) RowData(org.apache.flink.table.data.RowData) StructColumnVector(org.apache.hadoop.hive.ql.exec.vector.StructColumnVector) HashMap(java.util.HashMap) BytesColumnVector(org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector) GenericRowData(org.apache.flink.table.data.GenericRowData) BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData) StringData(org.apache.flink.table.data.StringData)

Example 4 with TimestampData

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

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

the class EqualiserCodeGeneratorTest method testTimestamp.

@Test
public void testTimestamp() {
    RecordEqualiser equaliser = new EqualiserCodeGenerator(new LogicalType[] { new TimestampType() }).generateRecordEqualiser("TIMESTAMP").newInstance(Thread.currentThread().getContextClassLoader());
    Function<TimestampData, BinaryRowData> func = o -> {
        BinaryRowData row = new BinaryRowData(1);
        BinaryRowWriter writer = new BinaryRowWriter(row);
        writer.writeTimestamp(0, o, 9);
        writer.complete();
        return row;
    };
    assertBoolean(equaliser, func, fromEpochMillis(1024), fromEpochMillis(1024), true);
    assertBoolean(equaliser, func, fromEpochMillis(1024), fromEpochMillis(1025), false);
}
Also used : Types(org.apache.flink.api.common.typeinfo.Types) TypeInformationRawType(org.apache.flink.table.types.logical.TypeInformationRawType) IntStream(java.util.stream.IntStream) RecordEqualiser(org.apache.flink.table.runtime.generated.RecordEqualiser) TimestampData(org.apache.flink.table.data.TimestampData) Assert.assertTrue(org.junit.Assert.assertTrue) VarCharType(org.apache.flink.table.types.logical.VarCharType) Test(org.junit.Test) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) Function(java.util.function.Function) TimestampData.fromEpochMillis(org.apache.flink.table.data.TimestampData.fromEpochMillis) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) StringData(org.apache.flink.table.data.StringData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) TimestampType(org.apache.flink.table.types.logical.TimestampType) RawValueDataSerializer(org.apache.flink.table.runtime.typeutils.RawValueDataSerializer) GenericRowData(org.apache.flink.table.data.GenericRowData) LogicalType(org.apache.flink.table.types.logical.LogicalType) RawValueData(org.apache.flink.table.data.RawValueData) Assert(org.junit.Assert) RecordEqualiser(org.apache.flink.table.runtime.generated.RecordEqualiser) TimestampData(org.apache.flink.table.data.TimestampData) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) TimestampType(org.apache.flink.table.types.logical.TimestampType) Test(org.junit.Test)

Aggregations

TimestampData (org.apache.flink.table.data.TimestampData)5 GenericRowData (org.apache.flink.table.data.GenericRowData)4 RowData (org.apache.flink.table.data.RowData)3 DecimalData (org.apache.flink.table.data.DecimalData)2 StringData (org.apache.flink.table.data.StringData)2 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)2 BinaryRowWriter (org.apache.flink.table.data.writer.BinaryRowWriter)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Function (java.util.function.Function)1 IntStream (java.util.stream.IntStream)1 Schema (org.apache.avro.Schema)1 Utf8 (org.apache.avro.util.Utf8)1 Types (org.apache.flink.api.common.typeinfo.Types)1 IntSerializer (org.apache.flink.api.common.typeutils.base.IntSerializer)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 DataGeneratorSourceTest (org.apache.flink.streaming.api.functions.source.datagen.DataGeneratorSourceTest)1 ArrayData (org.apache.flink.table.data.ArrayData)1 GenericMapData (org.apache.flink.table.data.GenericMapData)1 RawValueData (org.apache.flink.table.data.RawValueData)1