Search in sources :

Example 1 with KeyedStateReaderFunction

use of org.apache.flink.state.api.functions.KeyedStateReaderFunction in project flink by apache.

the class ExistingSavepoint method readKeyedState.

/**
 * Read keyed state from an operator in a {@code Savepoint}.
 *
 * @param uid The uid of the operator.
 * @param function The {@link KeyedStateReaderFunction} that is called for each key in state.
 * @param <K> The type of the key in state.
 * @param <OUT> The output type of the transform function.
 * @return A {@code DataSet} of objects read from keyed state.
 * @throws IOException If the savepoint does not contain operator state with the given uid.
 */
public <K, OUT> DataSource<OUT> readKeyedState(String uid, KeyedStateReaderFunction<K, OUT> function) throws IOException {
    TypeInformation<K> keyTypeInfo;
    TypeInformation<OUT> outType;
    try {
        keyTypeInfo = TypeExtractor.createTypeInfo(KeyedStateReaderFunction.class, function.getClass(), 0, null, null);
    } catch (InvalidTypesException e) {
        throw new InvalidProgramException("The key type of the KeyedStateReaderFunction could not be automatically determined. Please use " + "Savepoint#readKeyedState(String, KeyedStateReaderFunction, TypeInformation, TypeInformation) instead.", e);
    }
    try {
        outType = TypeExtractor.getUnaryOperatorReturnType(function, KeyedStateReaderFunction.class, 0, 1, TypeExtractor.NO_INDEX, keyTypeInfo, Utils.getCallLocationName(), false);
    } catch (InvalidTypesException e) {
        throw new InvalidProgramException("The output type of the KeyedStateReaderFunction could not be automatically determined. Please use " + "Savepoint#readKeyedState(String, KeyedStateReaderFunction, TypeInformation, TypeInformation) instead.", e);
    }
    return readKeyedState(uid, function, keyTypeInfo, outType);
}
Also used : InvalidProgramException(org.apache.flink.api.common.InvalidProgramException) KeyedStateReaderFunction(org.apache.flink.state.api.functions.KeyedStateReaderFunction) InvalidTypesException(org.apache.flink.api.common.functions.InvalidTypesException)

Example 2 with KeyedStateReaderFunction

use of org.apache.flink.state.api.functions.KeyedStateReaderFunction in project flink by apache.

the class SavepointReader method readKeyedState.

/**
 * Read keyed state from an operator in a {@code Savepoint}.
 *
 * @param uid The uid of the operator.
 * @param function The {@link KeyedStateReaderFunction} that is called for each key in state.
 * @param <K> The type of the key in state.
 * @param <OUT> The output type of the transform function.
 * @return A {@code DataStream} of objects read from keyed state.
 * @throws IOException If the savepoint does not contain operator state with the given uid.
 */
public <K, OUT> DataStream<OUT> readKeyedState(String uid, KeyedStateReaderFunction<K, OUT> function) throws IOException {
    TypeInformation<K> keyTypeInfo;
    TypeInformation<OUT> outType;
    try {
        keyTypeInfo = TypeExtractor.createTypeInfo(KeyedStateReaderFunction.class, function.getClass(), 0, null, null);
    } catch (InvalidTypesException e) {
        throw new InvalidProgramException("The key type of the KeyedStateReaderFunction could not be automatically determined. Please use " + "Savepoint#readKeyedState(String, KeyedStateReaderFunction, TypeInformation, TypeInformation) instead.", e);
    }
    try {
        outType = TypeExtractor.getUnaryOperatorReturnType(function, KeyedStateReaderFunction.class, 0, 1, TypeExtractor.NO_INDEX, keyTypeInfo, Utils.getCallLocationName(), false);
    } catch (InvalidTypesException e) {
        throw new InvalidProgramException("The output type of the KeyedStateReaderFunction could not be automatically determined. Please use " + "Savepoint#readKeyedState(String, KeyedStateReaderFunction, TypeInformation, TypeInformation) instead.", e);
    }
    return readKeyedState(uid, function, keyTypeInfo, outType);
}
Also used : InvalidProgramException(org.apache.flink.api.common.InvalidProgramException) KeyedStateReaderFunction(org.apache.flink.state.api.functions.KeyedStateReaderFunction) InvalidTypesException(org.apache.flink.api.common.functions.InvalidTypesException)

Example 3 with KeyedStateReaderFunction

use of org.apache.flink.state.api.functions.KeyedStateReaderFunction in project flink by apache.

the class KeyedStateInputFormatTest method testReadState.

@Test
public void testReadState() throws Exception {
    OperatorID operatorID = OperatorIDGenerator.fromUid("uid");
    OperatorSubtaskState state = createOperatorSubtaskState(new StreamFlatMap<>(new StatefulFunction()));
    OperatorState operatorState = new OperatorState(operatorID, 1, 128);
    operatorState.putState(0, state);
    KeyedStateInputFormat<?, ?, ?> format = new KeyedStateInputFormat<>(operatorState, new MemoryStateBackend(), new Configuration(), new KeyedStateReaderOperator<>(new ReaderFunction(), Types.INT));
    KeyGroupRangeInputSplit split = format.createInputSplits(1)[0];
    KeyedStateReaderFunction<Integer, Integer> userFunction = new ReaderFunction();
    List<Integer> data = readInputSplit(split, userFunction);
    Assert.assertEquals("Incorrect data read from input split", Arrays.asList(1, 2, 3), data);
}
Also used : KeyGroupRangeInputSplit(org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit) Configuration(org.apache.flink.configuration.Configuration) KeyedStateReaderFunction(org.apache.flink.state.api.functions.KeyedStateReaderFunction) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState) Test(org.junit.Test)

Example 4 with KeyedStateReaderFunction

use of org.apache.flink.state.api.functions.KeyedStateReaderFunction in project flink by apache.

the class KeyedStateInputFormatTest method testInvalidProcessReaderFunctionFails.

@Test(expected = IOException.class)
public void testInvalidProcessReaderFunctionFails() throws Exception {
    OperatorID operatorID = OperatorIDGenerator.fromUid("uid");
    OperatorSubtaskState state = createOperatorSubtaskState(new StreamFlatMap<>(new StatefulFunction()));
    OperatorState operatorState = new OperatorState(operatorID, 1, 128);
    operatorState.putState(0, state);
    KeyedStateInputFormat<?, ?, ?> format = new KeyedStateInputFormat<>(operatorState, new MemoryStateBackend(), new Configuration(), new KeyedStateReaderOperator<>(new ReaderFunction(), Types.INT));
    KeyGroupRangeInputSplit split = format.createInputSplits(1)[0];
    KeyedStateReaderFunction<Integer, Integer> userFunction = new InvalidReaderFunction();
    readInputSplit(split, userFunction);
    Assert.fail("KeyedStateReaderFunction did not fail on invalid RuntimeContext use");
}
Also used : KeyGroupRangeInputSplit(org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit) Configuration(org.apache.flink.configuration.Configuration) KeyedStateReaderFunction(org.apache.flink.state.api.functions.KeyedStateReaderFunction) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState) Test(org.junit.Test)

Example 5 with KeyedStateReaderFunction

use of org.apache.flink.state.api.functions.KeyedStateReaderFunction in project flink by apache.

the class KeyedStateInputFormatTest method readInputSplit.

@Nonnull
private List<Integer> readInputSplit(KeyGroupRangeInputSplit split, KeyedStateReaderFunction<Integer, Integer> userFunction) throws IOException {
    KeyedStateInputFormat<Integer, VoidNamespace, Integer> format = new KeyedStateInputFormat<>(new OperatorState(OperatorIDGenerator.fromUid("uid"), 1, 4), new MemoryStateBackend(), new Configuration(), new KeyedStateReaderOperator<>(userFunction, Types.INT));
    List<Integer> data = new ArrayList<>();
    format.setRuntimeContext(new MockStreamingRuntimeContext(false, 1, 0));
    format.openInputFormat();
    format.open(split);
    while (!format.reachedEnd()) {
        data.add(format.nextRecord(0));
    }
    format.close();
    format.closeInputFormat();
    data.sort(Comparator.comparingInt(id -> id));
    return data;
}
Also used : RichFlatMapFunction(org.apache.flink.api.common.functions.RichFlatMapFunction) VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) Arrays(java.util.Arrays) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState) KeyedOneInputStreamOperatorTestHarness(org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness) KeyedStateReaderOperator(org.apache.flink.state.api.input.operator.KeyedStateReaderOperator) KeyedProcessFunction(org.apache.flink.streaming.api.functions.KeyedProcessFunction) KeyedStateReaderFunction(org.apache.flink.state.api.functions.KeyedStateReaderFunction) ArrayList(java.util.ArrayList) MockStreamingRuntimeContext(org.apache.flink.streaming.util.MockStreamingRuntimeContext) KeyGroupRangeInputSplit(org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit) KeyedProcessOperator(org.apache.flink.streaming.api.operators.KeyedProcessOperator) Collector(org.apache.flink.util.Collector) Nonnull(javax.annotation.Nonnull) Types(org.apache.flink.api.common.typeinfo.Types) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) StreamFlatMap(org.apache.flink.streaming.api.operators.StreamFlatMap) Configuration(org.apache.flink.configuration.Configuration) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) OperatorSubtaskState(org.apache.flink.runtime.checkpoint.OperatorSubtaskState) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) List(java.util.List) ValueState(org.apache.flink.api.common.state.ValueState) VoidSerializer(org.apache.flink.api.common.typeutils.base.VoidSerializer) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) Assert(org.junit.Assert) Comparator(java.util.Comparator) OperatorIDGenerator(org.apache.flink.state.api.runtime.OperatorIDGenerator) MockStreamingRuntimeContext(org.apache.flink.streaming.util.MockStreamingRuntimeContext) Configuration(org.apache.flink.configuration.Configuration) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) ArrayList(java.util.ArrayList) VoidNamespace(org.apache.flink.runtime.state.VoidNamespace) OperatorState(org.apache.flink.runtime.checkpoint.OperatorState) Nonnull(javax.annotation.Nonnull)

Aggregations

KeyedStateReaderFunction (org.apache.flink.state.api.functions.KeyedStateReaderFunction)6 Configuration (org.apache.flink.configuration.Configuration)4 OperatorState (org.apache.flink.runtime.checkpoint.OperatorState)4 OperatorSubtaskState (org.apache.flink.runtime.checkpoint.OperatorSubtaskState)4 OperatorID (org.apache.flink.runtime.jobgraph.OperatorID)4 MemoryStateBackend (org.apache.flink.runtime.state.memory.MemoryStateBackend)4 KeyGroupRangeInputSplit (org.apache.flink.state.api.input.splits.KeyGroupRangeInputSplit)4 Test (org.junit.Test)4 InvalidProgramException (org.apache.flink.api.common.InvalidProgramException)2 InvalidTypesException (org.apache.flink.api.common.functions.InvalidTypesException)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Set (java.util.Set)1 Nonnull (javax.annotation.Nonnull)1 RichFlatMapFunction (org.apache.flink.api.common.functions.RichFlatMapFunction)1 ValueState (org.apache.flink.api.common.state.ValueState)1 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)1