Java Code Examples for org.apache.flink.api.common.state.ValueStateDescriptor#enableTimeToLive()

The following examples show how to use org.apache.flink.api.common.state.ValueStateDescriptor#enableTimeToLive() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StateBackendTestBase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testValueStateWorkWithTtl() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);
	try {
		ValueStateDescriptor<MutableLong> kvId = new ValueStateDescriptor<>("id", MutableLong.class);
		kvId.enableTimeToLive(StateTtlConfig.newBuilder(Time.seconds(1)).build());

		ValueState<MutableLong> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
		backend.setCurrentKey(1);
		state.update(new MutableLong());
		state.value();
	} finally {
		backend.close();
		backend.dispose();
	}
}
 
Example 2
Source File: KeyedStateDeduplication.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
@Override
public void open(Configuration parameters) throws Exception {
    super.open(parameters);
    ValueStateDescriptor<Boolean> keyedStateDuplicated =
            new ValueStateDescriptor<>("KeyedStateDeduplication",
                    TypeInformation.of(new TypeHint<Boolean>() {}));
    // 状态 TTL 相关配置,过期时间设定为 36 小时
    StateTtlConfig ttlConfig = StateTtlConfig
            .newBuilder(Time.hours(36))
            .setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite)
            .setStateVisibility(
                    StateTtlConfig.StateVisibility.NeverReturnExpired)
            .cleanupInRocksdbCompactFilter(50000000L)
            .build();
    // 开启 TTL
    keyedStateDuplicated.enableTimeToLive(ttlConfig);
    // 从状态后端恢复状态
    isExist = getRuntimeContext().getState(keyedStateDuplicated);
}
 
Example 3
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testValueStateWorkWithTtl() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);
	try {
		ValueStateDescriptor<MutableLong> kvId = new ValueStateDescriptor<>("id", MutableLong.class);
		kvId.enableTimeToLive(StateTtlConfig.newBuilder(Time.seconds(1)).build());

		ValueState<MutableLong> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
		backend.setCurrentKey(1);
		state.update(new MutableLong());
		state.value();
	} finally {
		backend.close();
		backend.dispose();
	}
}
 
Example 4
Source File: KeyedStateDeduplication.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
@Override
public void open(Configuration parameters) throws Exception {
    super.open(parameters);
    ValueStateDescriptor<Boolean> keyedStateDuplicated =
            new ValueStateDescriptor<>("KeyedStateDeduplication",
                    TypeInformation.of(new TypeHint<Boolean>() {}));
    // 状态 TTL 相关配置,过期时间设定为 36 小时
    StateTtlConfig ttlConfig = StateTtlConfig
            .newBuilder(Time.hours(36))
            .setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite)
            .setStateVisibility(
                    StateTtlConfig.StateVisibility.NeverReturnExpired)
            .cleanupInRocksdbCompactFilter(50000000L)
            .build();
    // 开启 TTL
    keyedStateDuplicated.enableTimeToLive(ttlConfig);
    // 从状态后端恢复状态
    isExist = getRuntimeContext().getState(keyedStateDuplicated);
}
 
Example 5
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testValueStateWorkWithTtl() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);
	try {
		ValueStateDescriptor<MutableLong> kvId = new ValueStateDescriptor<>("id", MutableLong.class);
		kvId.enableTimeToLive(StateTtlConfig.newBuilder(Time.seconds(1)).build());

		ValueState<MutableLong> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
		backend.setCurrentKey(1);
		state.update(new MutableLong());
		state.value();
	} finally {
		backend.close();
		backend.dispose();
	}
}
 
Example 6
Source File: OuterJoinRecordStateViews.java    From flink with Apache License 2.0 5 votes vote down vote up
private JoinKeyContainsUniqueKey(RuntimeContext ctx, String stateName, BaseRowTypeInfo recordType, StateTtlConfig ttlConfig) {
	TupleTypeInfo<Tuple2<BaseRow, Integer>> valueTypeInfo = new TupleTypeInfo<>(recordType, Types.INT);
	ValueStateDescriptor<Tuple2<BaseRow, Integer>> recordStateDesc = new ValueStateDescriptor<>(
		stateName,
		valueTypeInfo);
	if (!ttlConfig.equals(StateTtlConfig.DISABLED)) {
		recordStateDesc.enableTimeToLive(ttlConfig);
	}
	this.recordState = ctx.getState(recordStateDesc);
	// the result records always not more than 1
	this.reusedRecordList = new ArrayList<>(1);
	this.reusedTupleList = new ArrayList<>(1);
}
 
Example 7
Source File: JoinRecordStateViews.java    From flink with Apache License 2.0 5 votes vote down vote up
private JoinKeyContainsUniqueKey(
		RuntimeContext ctx,
		String stateName,
		BaseRowTypeInfo recordType,
		StateTtlConfig ttlConfig) {
	ValueStateDescriptor<BaseRow> recordStateDesc = new ValueStateDescriptor<>(
		stateName,
		recordType);
	if (!ttlConfig.equals(StateTtlConfig.DISABLED)) {
		recordStateDesc.enableTimeToLive(ttlConfig);
	}
	this.recordState = ctx.getState(recordStateDesc);
	// the result records always not more than 1
	this.reusedList = new ArrayList<>(1);
}
 
Example 8
Source File: TtlStateTest.java    From bravo with Apache License 2.0 5 votes vote down vote up
@Override
public void open(Configuration parameters) throws Exception {
	MapStateDescriptor<String, Integer> mapDesc = new MapStateDescriptor<>("Map", String.class,
			Integer.class);
	mapDesc.enableTimeToLive(StateTtlConfig.newBuilder(Time.minutes(100)).build());
	mapState = getRuntimeContext().getMapState(mapDesc);

	ListStateDescriptor<Integer> listDesc = new ListStateDescriptor<>("List", Integer.class);
	listDesc.enableTimeToLive(StateTtlConfig.newBuilder(Time.minutes(100)).build());
	listState = getRuntimeContext().getListState(listDesc);

	ValueStateDescriptor<Integer> valueStateDesc = new ValueStateDescriptor<Integer>("Val", Integer.class);
	valueStateDesc.enableTimeToLive(StateTtlConfig.newBuilder(Time.minutes(100)).build());
	valueState = getRuntimeContext().getState(valueStateDesc);
}
 
Example 9
Source File: OuterJoinRecordStateViews.java    From flink with Apache License 2.0 5 votes vote down vote up
private JoinKeyContainsUniqueKey(RuntimeContext ctx, String stateName, RowDataTypeInfo recordType, StateTtlConfig ttlConfig) {
	TupleTypeInfo<Tuple2<RowData, Integer>> valueTypeInfo = new TupleTypeInfo<>(recordType, Types.INT);
	ValueStateDescriptor<Tuple2<RowData, Integer>> recordStateDesc = new ValueStateDescriptor<>(
		stateName,
		valueTypeInfo);
	if (ttlConfig.isEnabled()) {
		recordStateDesc.enableTimeToLive(ttlConfig);
	}
	this.recordState = ctx.getState(recordStateDesc);
	// the result records always not more than 1
	this.reusedRecordList = new ArrayList<>(1);
	this.reusedTupleList = new ArrayList<>(1);
}
 
Example 10
Source File: JoinRecordStateViews.java    From flink with Apache License 2.0 5 votes vote down vote up
private JoinKeyContainsUniqueKey(
		RuntimeContext ctx,
		String stateName,
		RowDataTypeInfo recordType,
		StateTtlConfig ttlConfig) {
	ValueStateDescriptor<RowData> recordStateDesc = new ValueStateDescriptor<>(
		stateName,
		recordType);
	if (ttlConfig.isEnabled()) {
		recordStateDesc.enableTimeToLive(ttlConfig);
	}
	this.recordState = ctx.getState(recordStateDesc);
	// the result records always not more than 1
	this.reusedList = new ArrayList<>(1);
}
 
Example 11
Source File: MiniBatchDeduplicateKeepFirstRowFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open(ExecutionContext ctx) throws Exception {
	super.open(ctx);
	ValueStateDescriptor<Boolean> stateDesc = new ValueStateDescriptor<>("existsState", Types.BOOLEAN);
	StateTtlConfig ttlConfig = createTtlConfig(minRetentionTime);
	if (ttlConfig.isEnabled()) {
		stateDesc.enableTimeToLive(ttlConfig);
	}
	state = ctx.getRuntimeContext().getState(stateDesc);
}
 
Example 12
Source File: DeduplicateKeepFirstRowFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open(Configuration configure) throws Exception {
	super.open(configure);
	ValueStateDescriptor<Boolean> stateDesc = new ValueStateDescriptor<>("existsState", Types.BOOLEAN);
	StateTtlConfig ttlConfig = createTtlConfig(minRetentionTime);
	if (ttlConfig.isEnabled()) {
		stateDesc.enableTimeToLive(ttlConfig);
	}
	state = getRuntimeContext().getState(stateDesc);
}
 
Example 13
Source File: DeduplicateKeepLastRowFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open(Configuration configure) throws Exception {
	super.open(configure);
	ValueStateDescriptor<RowData> stateDesc = new ValueStateDescriptor<>("preRowState", rowTypeInfo);
	StateTtlConfig ttlConfig = createTtlConfig(minRetentionTime);
	if (ttlConfig.isEnabled()) {
		stateDesc.enableTimeToLive(ttlConfig);
	}
	state = getRuntimeContext().getState(stateDesc);
}
 
Example 14
Source File: MiniBatchDeduplicateKeepLastRowFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open(ExecutionContext ctx) throws Exception {
	super.open(ctx);
	ValueStateDescriptor<RowData> stateDesc = new ValueStateDescriptor<>("preRowState", rowTypeInfo);
	StateTtlConfig ttlConfig = createTtlConfig(minRetentionTime);
	if (ttlConfig.isEnabled()) {
		stateDesc.enableTimeToLive(ttlConfig);
	}
	state = ctx.getRuntimeContext().getState(stateDesc);
}
 
Example 15
Source File: AbstractStreamOperatorTestHarnessTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetTtlTimeProvider() throws Exception {
	AbstractStreamOperator<Integer> operator = new AbstractStreamOperator<Integer>() {};
	try (AbstractStreamOperatorTestHarness<Integer> result = new AbstractStreamOperatorTestHarness<>(
			operator,
			1,
			1,
			0)) {

		result.config.setStateKeySerializer(IntSerializer.INSTANCE);

		Time timeToLive = Time.hours(1);
		result.initializeState(new OperatorSubtaskState());
		result.open();

		ValueStateDescriptor<Integer> stateDescriptor = new ValueStateDescriptor<>("test", IntSerializer.INSTANCE);
		stateDescriptor.enableTimeToLive(StateTtlConfig.newBuilder(timeToLive).build());
		KeyedStateBackend<Integer> keyedStateBackend = operator.getKeyedStateBackend();
		ValueState<Integer> state = keyedStateBackend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, stateDescriptor);

		int expectedValue = 42;
		keyedStateBackend.setCurrentKey(1);
		result.setStateTtlProcessingTime(0L);
		state.update(expectedValue);
		Assert.assertEquals(expectedValue, (int) state.value());
		result.setStateTtlProcessingTime(timeToLive.toMilliseconds() + 1);
		Assert.assertNull(state.value());
	}
}