【Flink】如何定义一个HashSet的ValueState?

在之前用Flink的ValueState时,我总会把ValueStateDescriptor<Integer>("count", Integer.class)的第二个参数写作XXX.class,导致今天在测试时,无法把HashSet.class作为参数给ValueStateDescriptor。

于是我尝试用Class.forName来传入HashSet:

ValueState<HashSet<Integer>> set ;
@Override
public void open(Configuration parameters) throws Exception {
    set = getRuntimeContext().getState(new ValueStateDescriptor<HashSet<Integer>>("hashSet", (Class<HashSet<Integer>>) Class.forName("java.util.HashSet")));
}

虽然问题得以解决,但是参数名过于冗余,我便查看Flink的源代码,寻找其他思路:

public ValueStateDescriptor(String name, Class<T> typeClass) {
    super(name, typeClass, (Object)null);
}

public ValueStateDescriptor(String name, TypeInformation<T> typeInfo) {
    super(name, typeInfo, (Object)null);
}

public ValueStateDescriptor(String name, TypeSerializer<T> typeSerializer) {
    super(name, typeSerializer, (Object)null);
}

翻阅ValueStateDescriptor的源码,我发现,ValueStateDescriptor并不是只能传入Class这一种参数,还可以传入TypeInformation,TypeSerializer,我便想到了第二种方法:

ValueState<HashSet<Integer>> set;
            
@Override
public void open(Configuration parameters) throws Exception {
    pvCount = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("pvCount", Integer.class));
    set = getRuntimeContext().getState(new ValueStateDescriptor<HashSet<Integer>>("uvCount", TypeInformation.of(new TypeHint<HashSet<Integer>>() {})));
}

至此,问题得以解决。

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FlinkValueStateFlink状态编程中的一种状态类型。它用于在算子的处理函数中保和访问一个单一的值。ValueState可以在算子的不同处理函数中共享和访问,以便跨时间和事件保持状态。 使用ValueState,可以在算子的处理函数中储和更新一个值。这个值可以是任何类型,比如基本数据类型、自定义对象等。通过ValueState,算子可以在处理事件流时维护一些状态信息,从而实现一些有状态的计算逻辑。 要使用ValueState,首先需要在算子的运行时上下文中获取一个ValueStateDescriptor对象,该对象指定了ValueState的名称和类型。然后,可以通过调用ValueStateDescriptor的getState方法来获取具体的ValueState对象。通过这个ValueState对象,可以访问和更新储在其中的值。 以下是一个示例代码片段,演示了如何在Flink中使用ValueState: ```java // 导入所需的类 import org.apache.flink.api.common.functions.RichFlatMapFunction; import org.apache.flink.api.common.state.ValueState; import org.apache.flink.api.common.state.ValueStateDescriptor; import org.apache.flink.util.Collector; public class MyFlatMapFunction extends RichFlatMapFunction<Integer, String> { // 声明一个ValueState变量 private transient ValueState<Integer> countState; @Override public void open(Configuration parameters) throws Exception { // 初始化ValueState ValueStateDescriptor<Integer> descriptor = new ValueStateDescriptor<>("countState", Integer.class); countState = getRuntimeContext().getState(descriptor); } @Override public void flatMap(Integer value, Collector<String> out) throws Exception { // 获取当前状态值 Integer currentCount = countState.value(); if (currentCount == null) { currentCount = 0; } // 更新状态值 currentCount += value; countState.update(currentCount); // 输出结果 out.collect("Current count: " + currentCount); } } ``` 在上述代码中,我们通过调用`getRuntimeContext().getState(descriptor)`获取了一个`ValueState<Integer>`对象,该对象用于储和访问一个整数值。在`flatMap`函数中,我们首先通过`countState.value()`获取当前状态值,然后根据业务逻辑更新状态值,并通过`countState.update(currentCount)`方法更新状态。最后,我们使用`out.collect`方法将结果输出。 这只是一个简单的示例,实际中可以根据业务需求使用ValueState来实现更复杂的状态计算逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值