You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-zh@flink.apache.org by cs <58...@qq.com> on 2021/04/12 03:29:47 UTC
回复:Re: CheckpointedFunction#snapshotState访问键控状态报错
我们需求这个状态需要可以被外部查询,quable state我们生产端口会屏蔽掉。所以只能在checkpoint的时候自己将状态输出到外部存储。但是感觉没有很好的api获取statetable里面实际kv状态
---原始邮件---
发件人:"Yun Tang"<myasuka@live.com>
发送时间:2021年4月10日 星期六 下午9:50
收件人:"user-zh"<user-zh@flink.apache.org>
主题:Re: CheckpointedFunction#snapshotState访问键控状态报错
Hi
snapshotState主要是给operator state用的,异常原因是keyed state 访问时需要设置currentKey的,但是currentKey是当前正在处理的record的key,与snapshotState的执行时候的语义不一样,执行snapshotState方法的时候,是可以没有当前record的。
如果想要访问整个keyed state,可以通过 KeyedStateBackend#getKeys(String state, N namespace) 来访问,但还是不建议将keyed state写入到HBase,因为Flink更希望你是按照per record的访问,而不是全局访问,后者效率和性能都不好。
祝好
唐云
________________________________
From: cs <58683632@qq.com>
Sent: Tuesday, April 6, 2021 21:52
To: user-zh <user-zh@flink.apache.org>
Subject: CheckpointedFunction#snapshotState访问键控状态报错
class A extends KeyedProcessFunction<String, Object, String&gt; implements CheckpointedFunction {
private MapState<String, LiveBean&gt; liveBeanState;
&nbsp; @Override
&nbsp; &nbsp; public void initializeState(FunctionInitializationContext context) throws Exception {
&nbsp; &nbsp; &nbsp; &nbsp; MapStateDescriptor<String, LiveBean&gt; descriptor = new MapStateDescriptor<&gt;("liveState", BasicTypeInfo.STRING_TYPE_INFO, TypeInformation.of(LiveBean.class));
&nbsp; &nbsp; &nbsp; &nbsp; this.liveBeanState = context.getKeyedStateStore().getMapState(descriptor);
&nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp;@Override
&nbsp; &nbsp; public void snapshotState(FunctionSnapshotContext context) throws Exception {
&nbsp; &nbsp; &nbsp; &nbsp; Iterator<Map.Entry<String, LiveBean&gt;&gt; iter = liveBeanState.iterator(); // -&gt; Exception here
&nbsp; &nbsp; &nbsp; &nbsp; while (iter.hasNext()) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map.Entry<String, LiveBean&gt; e = iter.next();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hbaseDao.singlePut("table", StringUtils.reverse(e.getKey()), "cf", "info", JSON.toJSONString(e.getValue()));
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }
}
异常信息如下
java.lang.NullPointerException: No key set. This method should not be called outside of a keyed context.
需要在checkpoint的时候将键控状态存入hbase,该如何处理呢
Re: 回复:Re: CheckpointedFunction#snapshotState访问键控状态报错
Posted by Yun Tang <my...@live.com>.
Hi,
如果想要可以被外部查询,更合适的方式是 queryable state,如果想要把数据同时存储在外部,更合适的方式其实是直接使用外部store,例如HBase,这样子数据的查询是实时的。
checkpoint时候再向外存储的话,一来更新不是及时的,而且数据有冗余存储(分别在Flink的state和外部存储中)。
如果非要想获取实际的kv数据,可以参照我前面说的 KeyedStateBackend#getKeys 或者 KeyedStateBackend#applyToAllKeys 这两个接口,你可以自己实现一个stream operator而不是function,这样就能看到里面的keyed statebackend了。
祝好
唐云
________________________________
From: cs <58...@qq.com>
Sent: Monday, April 12, 2021 11:29
To: user-zh <us...@flink.apache.org>
Subject: 回复:Re: CheckpointedFunction#snapshotState访问键控状态报错
我们需求这个状态需要可以被外部查询,quable state我们生产端口会屏蔽掉。所以只能在checkpoint的时候自己将状态输出到外部存储。但是感觉没有很好的api获取statetable里面实际kv状态
---原始邮件---
发件人:"Yun Tang"<myasuka@live.com>
发送时间:2021年4月10日 星期六 下午9:50
收件人:"user-zh"<user-zh@flink.apache.org>
主题:Re: CheckpointedFunction#snapshotState访问键控状态报错
Hi
snapshotState主要是给operator state用的,异常原因是keyed state 访问时需要设置currentKey的,但是currentKey是当前正在处理的record的key,与snapshotState的执行时候的语义不一样,执行snapshotState方法的时候,是可以没有当前record的。
如果想要访问整个keyed state,可以通过 KeyedStateBackend#getKeys(String state, N namespace) 来访问,但还是不建议将keyed state写入到HBase,因为Flink更希望你是按照per record的访问,而不是全局访问,后者效率和性能都不好。
祝好
唐云
________________________________
From: cs <58683632@qq.com>
Sent: Tuesday, April 6, 2021 21:52
To: user-zh <user-zh@flink.apache.org>
Subject: CheckpointedFunction#snapshotState访问键控状态报错
class A extends KeyedProcessFunction<String, Object, String&gt; implements CheckpointedFunction {
private MapState<String, LiveBean&gt; liveBeanState;
&nbsp; @Override
&nbsp; &nbsp; public void initializeState(FunctionInitializationContext context) throws Exception {
&nbsp; &nbsp; &nbsp; &nbsp; MapStateDescriptor<String, LiveBean&gt; descriptor = new MapStateDescriptor<&gt;("liveState", BasicTypeInfo.STRING_TYPE_INFO, TypeInformation.of(LiveBean.class));
&nbsp; &nbsp; &nbsp; &nbsp; this.liveBeanState = context.getKeyedStateStore().getMapState(descriptor);
&nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp;@Override
&nbsp; &nbsp; public void snapshotState(FunctionSnapshotContext context) throws Exception {
&nbsp; &nbsp; &nbsp; &nbsp; Iterator<Map.Entry<String, LiveBean&gt;&gt; iter = liveBeanState.iterator(); // -&gt; Exception here
&nbsp; &nbsp; &nbsp; &nbsp; while (iter.hasNext()) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map.Entry<String, LiveBean&gt; e = iter.next();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hbaseDao.singlePut("table", StringUtils.reverse(e.getKey()), "cf", "info", JSON.toJSONString(e.getValue()));
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }
}
异常信息如下
java.lang.NullPointerException: No key set. This method should not be called outside of a keyed context.
需要在checkpoint的时候将键控状态存入hbase,该如何处理呢