Search in sources :

Example 21 with ListSerializer

use of org.apache.flink.api.common.typeutils.base.ListSerializer in project flink by splunk.

the class StateDescriptorPassingTest method validateListStateDescriptorConfigured.

private void validateListStateDescriptorConfigured(SingleOutputStreamOperator<?> result) {
    OneInputTransformation<?, ?> transform = (OneInputTransformation<?, ?>) result.getTransformation();
    WindowOperator<?, ?, ?, ?, ?> op = (WindowOperator<?, ?, ?, ?, ?>) transform.getOperator();
    StateDescriptor<?, ?> descr = op.getStateDescriptor();
    assertTrue(descr instanceof ListStateDescriptor);
    ListStateDescriptor<?> listDescr = (ListStateDescriptor<?>) descr;
    // this would be the first statement to fail if state descriptors were not properly
    // initialized
    TypeSerializer<?> serializer = listDescr.getSerializer();
    assertTrue(serializer instanceof ListSerializer);
    TypeSerializer<?> elementSerializer = listDescr.getElementSerializer();
    assertTrue(elementSerializer instanceof KryoSerializer);
    Kryo kryo = ((KryoSerializer<?>) elementSerializer).getKryo();
    assertTrue("serializer registration was not properly passed on", kryo.getSerializer(File.class) instanceof JavaSerializer);
}
Also used : WindowOperator(org.apache.flink.streaming.runtime.operators.windowing.WindowOperator) ListSerializer(org.apache.flink.api.common.typeutils.base.ListSerializer) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) JavaSerializer(com.esotericsoftware.kryo.serializers.JavaSerializer) Kryo(com.esotericsoftware.kryo.Kryo) KryoSerializer(org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer)

Example 22 with ListSerializer

use of org.apache.flink.api.common.typeutils.base.ListSerializer in project flink-mirror by flink-ci.

the class ListStateDescriptorTest method testListStateDescriptor.

@Test
public void testListStateDescriptor() throws Exception {
    TypeSerializer<String> serializer = new KryoSerializer<>(String.class, new ExecutionConfig());
    ListStateDescriptor<String> descr = new ListStateDescriptor<>("testName", serializer);
    assertEquals("testName", descr.getName());
    assertNotNull(descr.getSerializer());
    assertTrue(descr.getSerializer() instanceof ListSerializer);
    assertNotNull(descr.getElementSerializer());
    assertEquals(serializer, descr.getElementSerializer());
    ListStateDescriptor<String> copy = CommonTestUtils.createCopySerializable(descr);
    assertEquals("testName", copy.getName());
    assertNotNull(copy.getSerializer());
    assertTrue(copy.getSerializer() instanceof ListSerializer);
    assertNotNull(copy.getElementSerializer());
    assertEquals(serializer, copy.getElementSerializer());
}
Also used : ListSerializer(org.apache.flink.api.common.typeutils.base.ListSerializer) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) KryoSerializer(org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer) Test(org.junit.Test)

Example 23 with ListSerializer

use of org.apache.flink.api.common.typeutils.base.ListSerializer in project flink-mirror by flink-ci.

the class HeapListState method getSerializedValue.

@Override
public byte[] getSerializedValue(final byte[] serializedKeyAndNamespace, final TypeSerializer<K> safeKeySerializer, final TypeSerializer<N> safeNamespaceSerializer, final TypeSerializer<List<V>> safeValueSerializer) throws Exception {
    Preconditions.checkNotNull(serializedKeyAndNamespace);
    Preconditions.checkNotNull(safeKeySerializer);
    Preconditions.checkNotNull(safeNamespaceSerializer);
    Preconditions.checkNotNull(safeValueSerializer);
    Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);
    List<V> result = stateTable.get(keyAndNamespace.f0, keyAndNamespace.f1);
    if (result == null) {
        return null;
    }
    final TypeSerializer<V> dupSerializer = ((ListSerializer<V>) safeValueSerializer).getElementSerializer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputViewStreamWrapper view = new DataOutputViewStreamWrapper(baos);
    // write the same as RocksDB writes lists, with one ',' separator
    for (int i = 0; i < result.size(); i++) {
        dupSerializer.serialize(result.get(i), view);
        if (i < result.size() - 1) {
            view.writeByte(',');
        }
    }
    view.flush();
    return baos.toByteArray();
}
Also used : ListSerializer(org.apache.flink.api.common.typeutils.base.ListSerializer) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ListSerializer (org.apache.flink.api.common.typeutils.base.ListSerializer)23 KryoSerializer (org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer)11 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)9 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)8 Test (org.junit.Test)8 MapStateDescriptor (org.apache.flink.api.common.state.MapStateDescriptor)6 TaskInfo (org.apache.flink.api.common.TaskInfo)4 Path (org.apache.flink.core.fs.Path)4 Kryo (com.esotericsoftware.kryo.Kryo)3 JavaSerializer (com.esotericsoftware.kryo.serializers.JavaSerializer)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 StateTtlConfig (org.apache.flink.api.common.state.StateTtlConfig)3 LongSerializer (org.apache.flink.api.common.typeutils.base.LongSerializer)3 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)3 InternalMapState (org.apache.flink.runtime.state.internal.InternalMapState)3 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)3