Flink读取state的状态

1、复制flink opt目录下的flink-queryable-state-runtime_2.11-1.9.0.jar包到lib目录

2、修改配conf/flink-conf.yaml配置文件

添加如下配置

# queryable-state
queryable-state.enable: true
3、提交如下代码示例

public class WordCount {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
 
        env.enableCheckpointing(5000);
        env.setStateBackend(new RocksDBStateBackend("hdfs://nameservice1/user/hdfs/flink/checkpoints"));
 
        env
                .socketTextStream("192.168.0.1", 9999)
                .flatMap(new FlatMapFunction<String, Tuple2<Long, Long>>() {
                    @Override
                    public void flatMap(String s, Collector<Tuple2<Long, Long>> collector) throws Exception {
                        collector.collect(new Tuple2<>(Long.parseLong(s), 1L));
                    }
                })
                .keyBy(0)
                .flatMap(new CountWindowAverage())
                .keyBy(0)
                .print();
        
        env.execute(WordCount.class.getCanonicalName());
    }
 
    static class CountWindowAverage extends RichFlatMapFunction<Tuple2<Long, Long>, Tuple2<Long, Long>> {
 
        private transient ValueState<Tuple2<Long, Long>> sum; // a tuple containing the count and the sum
 
        @Override
        public void open(Configuration config) {
            ValueStateDescriptor<Tuple2<Long, Long>> descriptor =
                    new ValueStateDescriptor<>(
                            "average", // the state name
                            TypeInformation.of(new TypeHint<Tuple2<Long, Long>>() {})); // type information
            descriptor.setQueryable("query123");
 
            sum = getRuntimeContext().getState(descriptor);
        }
 
        @Override
        public void flatMap(Tuple2<Long, Long> input, Collector<Tuple2<Long, Long>> out) throws Exception {
            Tuple2<Long, Long> currentSum = sum.value();
            if (currentSum == null) {
                currentSum = new Tuple2<>(0L, 1L);
            }
 
            currentSum.f0 += 1;
            currentSum.f1 += 1;
            sum.update(currentSum);
 
            if (currentSum.f0 >= 2) {
                out.collect(new Tuple2<>(input.f0, currentSum.f1));
            }
        }
    }
}
3.1提交job查看taskmanage的地址

2019-09-04 11:50:30,679 INFO  org.apache.flink.queryablestate.server.KvStateServerImpl      - Started Queryable State Server @ /192.168.0.3:9067.
2019-09-04 11:50:30,683 INFO  org.apache.flink.queryablestate.client.proxy.KvStateClientProxyImpl  - Started Queryable State Proxy Server @ /192.168.0.3:9069.
Started Queryable State Proxy Server @ /192.168.0.3:9069. 就是要查询的地址

4、客户端查询

4.1添加依赖包

<dependency>
  <groupId>org.apache.flink</groupId>
  <artifactId>flink-core</artifactId>
  <version>1.9.0</version>
</dependency>
<dependency>
  <groupId>org.apache.flink</groupId>
  <artifactId>flink-queryable-state-client-java</artifactId>
  <version>1.9.0</version>
</dependency>
4.2查询客户端代码

public class QueryState {
    public static void main(String[] args) throws UnknownHostException, InterruptedException {
        QueryableStateClient client = new QueryableStateClient(
                "192.168.0.3", // taskmanager的地址
                9069);// 默认是9069端口,可以在flink-conf.yaml文件中配置
 
        // the state descriptor of the state to be fetched.
        ValueStateDescriptor<Tuple2<Long, Long>> descriptor =
                new ValueStateDescriptor<>(
                        "average",
                        TypeInformation.of(new TypeHint<Tuple2<Long, Long>>() {}));
 
        while (true) {
 
            CompletableFuture<ValueState<Tuple2<Long, Long>>> resultFuture =
                    client.getKvState(
                            JobID.fromHexString("ccf23e29590475f9d18422752d23c2fa"), // 从webui中获取JID
                            "query123", // wordcount中设置的名字 descriptor.setQueryable("query123");
                            1L, // key的值
                            BasicTypeInfo.LONG_TYPE_INFO, // key 的类型
                            descriptor);
 
            // now handle the returned value
            resultFuture.thenAccept(response -> {
                try {
                    Tuple2<Long, Long> res = response.value();
                    System.out.println("res: " + res);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            
            Thread.sleep(1000);
        }
    }
}
 
————————————————
版权声明:本文为CSDN博主「修身从修心开始」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/whr_yy/article/details/100535945

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值