Search in sources :

Example 21 with DataOutputSerializer

use of org.apache.flink.core.memory.DataOutputSerializer in project flink by apache.

the class TestManagedFileSourceSplitSerializer method serialize.

@Override
public byte[] serialize(TestManagedIterableSourceSplit split) throws IOException {
    final DataOutputSerializer out = new DataOutputSerializer(64);
    out.writeUTF(split.splitId());
    split.getFilePath().write(out);
    final byte[] result = out.getCopyOfBuffer();
    out.clear();
    return result;
}
Also used : DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer)

Example 22 with DataOutputSerializer

use of org.apache.flink.core.memory.DataOutputSerializer in project flink by apache.

the class RawType method getSerializerString.

/**
 * Returns the serialized {@link TypeSerializerSnapshot} in Base64 encoding of this raw type.
 */
public String getSerializerString() {
    if (serializerString == null) {
        final DataOutputSerializer outputSerializer = new DataOutputSerializer(128);
        try {
            TypeSerializerSnapshot.writeVersionedSnapshot(outputSerializer, serializer.snapshotConfiguration());
            serializerString = EncodingUtils.encodeBytesToBase64(outputSerializer.getCopyOfBuffer());
            return serializerString;
        } catch (Exception e) {
            throw new TableException(String.format("Unable to generate a string representation of the serializer snapshot of '%s' " + "describing the class '%s' for the RAW type.", serializer.getClass().getName(), clazz.toString()), e);
        }
    }
    return serializerString;
}
Also used : DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) TableException(org.apache.flink.table.api.TableException) TableException(org.apache.flink.table.api.TableException) ValidationException(org.apache.flink.table.api.ValidationException)

Example 23 with DataOutputSerializer

use of org.apache.flink.core.memory.DataOutputSerializer in project flink by apache.

the class SourceCoordinatorTest method createCheckpointDataWithSerdeV0.

// ------------------------------------------------------------------------
// test helpers
// ------------------------------------------------------------------------
private byte[] createCheckpointDataWithSerdeV0(Set<MockSourceSplit> splits) throws Exception {
    final MockSplitEnumeratorCheckpointSerializer enumChkptSerializer = new MockSplitEnumeratorCheckpointSerializer();
    final DataOutputSerializer serializer = new DataOutputSerializer(32);
    serializer.writeInt(SourceCoordinatorSerdeUtils.VERSION_0);
    serializer.writeInt(enumChkptSerializer.getVersion());
    final byte[] serializedEnumChkpt = enumChkptSerializer.serialize(splits);
    serializer.writeInt(serializedEnumChkpt.length);
    serializer.write(serializedEnumChkpt);
    // Version 0 wrote number of reader, see FLINK-21452
    serializer.writeInt(0);
    // Version 0 wrote split assignment tracker
    // SplitSerializer version used in assignment tracker
    serializer.writeInt(0);
    // Number of checkpoint in assignment tracker
    serializer.writeInt(0);
    return serializer.getCopyOfBuffer();
}
Also used : MockSplitEnumeratorCheckpointSerializer(org.apache.flink.api.connector.source.mocks.MockSplitEnumeratorCheckpointSerializer) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer)

Example 24 with DataOutputSerializer

use of org.apache.flink.core.memory.DataOutputSerializer in project flink by apache.

the class RocksDBRocksStateKeysAndNamespacesIteratorTest method testIteratorHelper.

@SuppressWarnings("unchecked")
<K> void testIteratorHelper(TypeSerializer<K> keySerializer, int maxKeyGroupNumber, Function<Integer, K> getKeyFunc) throws Exception {
    String testStateName = "aha";
    String namespace = "ns";
    try (RocksDBKeyedStateBackendTestFactory factory = new RocksDBKeyedStateBackendTestFactory()) {
        RocksDBKeyedStateBackend<K> keyedStateBackend = factory.create(tmp, keySerializer, maxKeyGroupNumber);
        ValueState<String> testState = keyedStateBackend.getPartitionedState(namespace, StringSerializer.INSTANCE, new ValueStateDescriptor<>(testStateName, String.class));
        // insert record
        for (int i = 0; i < 1000; ++i) {
            keyedStateBackend.setCurrentKey(getKeyFunc.apply(i));
            testState.update(String.valueOf(i));
        }
        DataOutputSerializer outputStream = new DataOutputSerializer(8);
        boolean ambiguousKeyPossible = CompositeKeySerializationUtils.isAmbiguousKeyPossible(keySerializer, StringSerializer.INSTANCE);
        CompositeKeySerializationUtils.writeNameSpace(namespace, StringSerializer.INSTANCE, outputStream, ambiguousKeyPossible);
        // already created with the state, should be closed with the backend
        ColumnFamilyHandle handle = keyedStateBackend.getColumnFamilyHandle(testStateName);
        try (RocksIteratorWrapper iterator = RocksDBOperationUtils.getRocksIterator(keyedStateBackend.db, handle, keyedStateBackend.getReadOptions());
            RocksStateKeysAndNamespaceIterator<K, String> iteratorWrapper = new RocksStateKeysAndNamespaceIterator<>(iterator, testStateName, keySerializer, StringSerializer.INSTANCE, keyedStateBackend.getKeyGroupPrefixBytes(), ambiguousKeyPossible)) {
            iterator.seekToFirst();
            // valid record
            List<Tuple2<Integer, String>> fetchedKeys = new ArrayList<>(1000);
            while (iteratorWrapper.hasNext()) {
                Tuple2 entry = iteratorWrapper.next();
                entry.f0 = Integer.parseInt(entry.f0.toString());
                fetchedKeys.add((Tuple2<Integer, String>) entry);
            }
            fetchedKeys.sort(Comparator.comparingInt(a -> a.f0));
            Assert.assertEquals(1000, fetchedKeys.size());
            for (int i = 0; i < 1000; ++i) {
                Assert.assertEquals(i, fetchedKeys.get(i).f0.intValue());
                Assert.assertEquals(namespace, fetchedKeys.get(i).f1);
            }
        }
    }
}
Also used : TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) Tuple2(org.apache.flink.api.java.tuple.Tuple2) CompositeKeySerializationUtils(org.apache.flink.runtime.state.CompositeKeySerializationUtils) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) Test(org.junit.Test) StringSerializer(org.apache.flink.api.common.typeutils.base.StringSerializer) Function(java.util.function.Function) RocksStateKeysAndNamespaceIterator(org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysAndNamespaceIterator) ArrayList(java.util.ArrayList) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) List(java.util.List) Rule(org.junit.Rule) ValueState(org.apache.flink.api.common.state.ValueState) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) Assert(org.junit.Assert) Comparator(java.util.Comparator) TemporaryFolder(org.junit.rules.TemporaryFolder) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) ArrayList(java.util.ArrayList) RocksStateKeysAndNamespaceIterator(org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysAndNamespaceIterator) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) Tuple2(org.apache.flink.api.java.tuple.Tuple2)

Example 25 with DataOutputSerializer

use of org.apache.flink.core.memory.DataOutputSerializer in project flink by apache.

the class RocksDBRocksStateKeysIteratorTest method testIteratorHelper.

<K> void testIteratorHelper(TypeSerializer<K> keySerializer, int maxKeyGroupNumber, Function<Integer, K> getKeyFunc) throws Exception {
    String testStateName = "aha";
    String namespace = "ns";
    try (RocksDBKeyedStateBackendTestFactory factory = new RocksDBKeyedStateBackendTestFactory()) {
        RocksDBKeyedStateBackend<K> keyedStateBackend = factory.create(tmp, keySerializer, maxKeyGroupNumber);
        ValueState<String> testState = keyedStateBackend.getPartitionedState(namespace, StringSerializer.INSTANCE, new ValueStateDescriptor<>(testStateName, String.class));
        // insert record
        for (int i = 0; i < 1000; ++i) {
            keyedStateBackend.setCurrentKey(getKeyFunc.apply(i));
            testState.update(String.valueOf(i));
        }
        DataOutputSerializer outputStream = new DataOutputSerializer(8);
        boolean ambiguousKeyPossible = CompositeKeySerializationUtils.isAmbiguousKeyPossible(keySerializer, StringSerializer.INSTANCE);
        CompositeKeySerializationUtils.writeNameSpace(namespace, StringSerializer.INSTANCE, outputStream, ambiguousKeyPossible);
        byte[] nameSpaceBytes = outputStream.getCopyOfBuffer();
        // already created with the state, should be closed with the backend
        ColumnFamilyHandle handle = keyedStateBackend.getColumnFamilyHandle(testStateName);
        try (RocksIteratorWrapper iterator = RocksDBOperationUtils.getRocksIterator(keyedStateBackend.db, handle, keyedStateBackend.getReadOptions());
            RocksStateKeysIterator<K> iteratorWrapper = new RocksStateKeysIterator<>(iterator, testStateName, keySerializer, keyedStateBackend.getKeyGroupPrefixBytes(), ambiguousKeyPossible, nameSpaceBytes)) {
            iterator.seekToFirst();
            // valid record
            List<Integer> fetchedKeys = new ArrayList<>(1000);
            while (iteratorWrapper.hasNext()) {
                fetchedKeys.add(Integer.parseInt(iteratorWrapper.next().toString()));
            }
            fetchedKeys.sort(Comparator.comparingInt(a -> a));
            Assert.assertEquals(1000, fetchedKeys.size());
            for (int i = 0; i < 1000; ++i) {
                Assert.assertEquals(i, fetchedKeys.get(i).intValue());
            }
        }
    }
}
Also used : TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) CompositeKeySerializationUtils(org.apache.flink.runtime.state.CompositeKeySerializationUtils) RocksStateKeysIterator(org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysIterator) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) Test(org.junit.Test) StringSerializer(org.apache.flink.api.common.typeutils.base.StringSerializer) Function(java.util.function.Function) ArrayList(java.util.ArrayList) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) List(java.util.List) Rule(org.junit.Rule) ValueState(org.apache.flink.api.common.state.ValueState) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) Assert(org.junit.Assert) Comparator(java.util.Comparator) TemporaryFolder(org.junit.rules.TemporaryFolder) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) RocksStateKeysIterator(org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysIterator) ArrayList(java.util.ArrayList) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle)

Aggregations

DataOutputSerializer (org.apache.flink.core.memory.DataOutputSerializer)63 DataInputDeserializer (org.apache.flink.core.memory.DataInputDeserializer)15 Test (org.junit.Test)15 IOException (java.io.IOException)10 ByteBuffer (java.nio.ByteBuffer)6 List (java.util.List)4 IntSerializer (org.apache.flink.api.common.typeutils.base.IntSerializer)4 StringSerializer (org.apache.flink.api.common.typeutils.base.StringSerializer)4 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)4 ArrayList (java.util.ArrayList)3 IntStream (java.util.stream.IntStream)3 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)3 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)3 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Stream (java.util.stream.Stream)2 ValueState (org.apache.flink.api.common.state.ValueState)2 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)2