Flink about setting the state life cycle

Table of contents

scenes to be used

set life cycle


scenes to be used

        Generally, the purpose of setting the life cycle of the state is nothing more than to reduce the resource consumption of the server. In the production environment, set the life cycle of the state, for example, automatically clear the state after 1 day (null)

        It is generally used to find indicators such as the number of unique users of the day, such as the number of unique users browsing the page, that is, uv, we can set a status to mark whether the user has visited the page that day, if the status is null, add 1, no If it is null, it is necessary to judge whether the status (generally stored is the date the user browsed) is equal to the current day, if not, add 1 and update the status, otherwise, do not process.

        There is a problem involved here. If the status is not null, then if the user does not visit for 5 days, the status will also be saved for 5 days. Obviously, when the amount of data is large, it will undoubtedly take up resources.

        At this time, we can set a life cycle for the state, set it to 1 day and when the state is updated, the life cycle will also be refreshed. If a user last browsed on 2023-03-04 8:00:00, the state has no Update, then the status will expire (null) at 2023-03-05 8:00:00, even if the user browses at 2023-03-05 7:00:00 or 2023-03-05 9:00:00 , also add 1, which is also in line with our daily life uv.

set life cycle

private ValueState<String> valueState;

@Override
public void open(Configuration parameters) throws Exception {
    // 设置状态生命周期
    StateTtlConfig stateTtlConfig = new StateTtlConfig
        .Builder(Time.days(1)) // 周期为1天
        .setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite) // 创建或者更新状态时重新刷新生命周期
        .build();
    ValueStateDescriptor<String> valueStateDs = new ValueStateDescriptor<>("last_order_state", String.class);
    valueStateDs.enableTimeToLive(stateTtlConfig);
    valueState = getRuntimeContext().getState(valueStateDs);
}

Guess you like

Origin blog.csdn.net/m0_55868614/article/details/129335426