flink学习之state

state作用

保留当前key的历史状态。

state用法

ListState<Integer> vipList = getRuntimeContext().getListState(new ListStateDescriptor<Integer>("vipList", TypeInformation.of(Integer.class)));

有valueState listState mapstate 。冒失没有setstate

state案例

比如起点的小说不能被下载。别人只能通过截屏,提取文字的方式盗版小说。

起点做了防爬措施。防爬措施如下。

1.如果一个用户1s翻1页,或者速度更快,连续10次,那么就认为用户是机器人。

上述两种情况,用户 不断的发起点击事件

userId=1 click_time=2023-09-07 00:00:00

userId=1 click_time=2023-09-07 00:00:01

我们如何判断1呢?

lastClickState 保留用户上一次的点击时间。

clickcntState 保留用户1s1页连续点击次数。

来一条数据就与上一次的lastClickState去对比,

如果间隔<1s clickcntState就+1

如果>1s clickcntState就置于0

同时判断clickcntState次数是否>=10如果大于就将该userid 输出到sink

来个demo直接说话。

复制代码
package com.chenchi.pojo;

public  class User {
    public Integer userId;
    public Integer vip;

    public long clickTime=System.currentTimeMillis();
    public User() {
    }

    public User(Integer userId, Integer vip) {
        this.userId = userId;
        this.vip = vip;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Integer getVip() {
        return vip;
    }

    public void setVip(Integer vip) {
        this.vip = vip;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", vip=" + vip +
                ", clickTime=" + clickTime +
                '}';
    }

    public long getClickTime() {
        return clickTime;
    }

    public void setClickTime(long clickTime) {
        this.clickTime = clickTime;
    }
}

package com.chenchi.pojo;

import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.Random;

public class UserSource implements SourceFunction<User> {
    boolean run = true;
    public UserSource(){}
    int randomBound=1000;
    int interval=1000;
    public UserSource(Integer randomBound){
        this.randomBound=randomBound;
    }
    public UserSource(Integer randomBound,int interval){
        this.randomBound=randomBound;
        this.interval=interval;
    }
    @Override
    public void run(SourceContext<User> sourceContext) throws Exception {
        while (true) {
            Integer userId = new Random().nextInt(randomBound);
            Integer vip = new Random().nextInt(10);
            sourceContext.collect(new User(userId, vip));
            Thread.sleep(interval);
        }
    }

    @Override
    public void cancel() {
        run = false;
    }
}

package com.chenchi.state;

import com.chenchi.pojo.User;
import com.chenchi.pojo.UserSource;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

public class ValueStateDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        CustomProcess customProcess = new CustomProcess();
        DataStreamSource<User> userDataStreamSource = env.addSource(new UserSource(4,100));
        userDataStreamSource.print();
        userDataStreamSource
                .keyBy(user->user.userId)
                .process(customProcess)
                        .print();
        env.execute();
    }

     static class CustomProcess extends KeyedProcessFunction<Integer,User,String> {
        ValueState<Long> lastClickTime;
        ValueState<Integer> cnt;
        @Override
        public void open(Configuration parameters) throws Exception {
            lastClickTime= getRuntimeContext().getState(new ValueStateDescriptor<Long>("click", TypeInformation.of(Long.class)));
            cnt= getRuntimeContext().getState(new ValueStateDescriptor<Integer>("cnt",Integer.class));
            super.open(parameters);
        }

        @Override
        public void processElement(User user, KeyedProcessFunction<Integer, User, String>.Context context, Collector<String> collector) throws Exception {
            Integer userId = user.getUserId();
            long clickTime = user.getClickTime();
            Long last = lastClickTime.value();
            Integer value = cnt.value();
            if (value==null){
                value=0;
            }
            if (last!=null&&clickTime-last<1000){
                //点击太快
                cnt.update(value+1);
            }else {
                //之前可能是不喜欢的页数突然点快了 点击慢就置0
                cnt.update(0);
            }
            //保存当前的listState
            lastClickTime.update(clickTime);
            if (cnt.value()>10){
                collector.collect("userId="+userId+",clickCnt="+cnt.value()+",click太快 注意注意");
            }
        }
    }
}

打印日志

10> User{userId=0, vip=0, clickTime=1694083167883}

11> User{userId=0, vip=4, clickTime=1694083167994}

12> User{userId=2, vip=4, clickTime=1694083168102}

1> User{userId=2, vip=7, clickTime=1694083168212}

2> User{userId=2, vip=3, clickTime=1694083168320}

3> User{userId=1, vip=0, clickTime=1694083168428}

4> User{userId=0, vip=7, clickTime=1694083168537}

5> User{userId=2, vip=3, clickTime=1694083168646}

6> User{userId=2, vip=0, clickTime=1694083168757}

7> User{userId=2, vip=3, clickTime=1694083168866}

8> User{userId=0, vip=9, clickTime=1694083168975}

9> User{userId=0, vip=3, clickTime=1694083169084}

10> User{userId=2, vip=7, clickTime=1694083169191}

11> User{userId=0, vip=7, clickTime=1694083169298}

12> User{userId=0, vip=6, clickTime=1694083169406}

1> User{userId=3, vip=9, clickTime=1694083169513}

2> User{userId=0, vip=4, clickTime=1694083169618}

3> User{userId=3, vip=3, clickTime=1694083169726}

4> User{userId=1, vip=8, clickTime=1694083169833}

5> User{userId=2, vip=1, clickTime=1694083169942}

6> User{userId=3, vip=2, clickTime=1694083170050}

7> User{userId=2, vip=8, clickTime=1694083170158}

8> User{userId=1, vip=4, clickTime=1694083170267}

9> User{userId=1, vip=2, clickTime=1694083170374}

10> User{userId=0, vip=1, clickTime=1694083170481}

11> User{userId=3, vip=6, clickTime=1694083170589}

12> User{userId=0, vip=9, clickTime=1694083170696}

1> User{userId=3, vip=1, clickTime=1694083170804}

2> User{userId=1, vip=8, clickTime=1694083170911}

3> User{userId=1, vip=3, clickTime=1694083171018}

4> User{userId=0, vip=7, clickTime=1694083171126}

5> User{userId=1, vip=8, clickTime=1694083171233}

6> User{userId=3, vip=5, clickTime=1694083171341}

7> User{userId=0, vip=8, clickTime=1694083171448}

9> userId=0,clickCnt=11,click太快 注意注意

效果符合预期。

相关推荐
i***13242 分钟前
SpringCloud实战十三:Gateway之 Spring Cloud Gateway 动态路由
java·spring cloud·gateway
微露清风3 分钟前
系统学习C++-第二十一讲-用哈希表封装 myunordered_map 和 myunordered_set
c++·学习·散列表
计算机徐师兄4 分钟前
Java基于微信小程序的食堂线上预约点餐系统【附源码、文档说明】
java·微信小程序·食堂线上预约点餐系统小程序·食堂线上预约点餐微信小程序·java食堂线上预约点餐小程序·食堂线上预约点餐小程序·食堂线上预约点餐系统微信小程序
Chunyyyen4 分钟前
【第三十周】OCR学习03
学习·ocr
无心水1 小时前
【分布式利器:腾讯TSF】10、TSF故障排查与架构评审实战:Java架构师从救火到防火的生产哲学
java·人工智能·分布式·架构·限流·分布式利器·腾讯tsf
我的xiaodoujiao2 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 38--Allure 测试报告
python·学习·测试工具·pytest
好奇龙猫8 小时前
【AI学习-comfyUI学习-第三十节-第三十一节-FLUX-SD放大工作流+FLUX图生图工作流-各个部分学习】
人工智能·学习
Boilermaker19928 小时前
[Java 并发编程] Synchronized 锁升级
java·开发语言
saoys8 小时前
Opencv 学习笔记:图像掩膜操作(精准提取指定区域像素)
笔记·opencv·学习
Cherry的跨界思维8 小时前
28、AI测试环境搭建与全栈工具实战:从本地到云平台的完整指南
java·人工智能·vue3·ai测试·ai全栈·测试全栈·ai测试全栈