gpt4 book ai didi

java - 任务未读取 Spark 累加器值

转载 作者:行者123 更新时间:2023-12-03 01:44:39 25 4
gpt4 key购买 nike

我正在初始化累加器

final Accumulator<Integer> accum = sc.accumulator(0);

然后在 map 函数中,我尝试增加累加器,然后使用累加器值设置变量。

JavaRDD<UserSetGet> UserProfileRDD1 = temp.map(new Function<String, UserSetGet>() {

@Override
public UserSetGet call(String arg0) throws Exception {

UserSetGet usg = new UserSetGet();

accum.add(1);
usg.setPid(accum.value().toString();


}
});

但是我收到以下错误。

16/03/14 09:12:58 ERROR executor.Executor: Exception in task 0.0 in stage 2.0 (TID 2) java.lang.UnsupportedOperationException: Can't read accumulator value in task

已编辑 - 根据 Avihoo Mamka 的回答,不可能在任务中获取累加器值。

那么我是否可以并行实现相同的目标。这样每次在我的 map 函数中递增变量(例如静态变量)时都会设置 Pid 值。

最佳答案

来自Spark docs

Accumulators are variables that are only “added” to through an associative operation and can therefore be efficiently supported in parallel. They can be used to implement counters (as in MapReduce) or sums

...

Only the driver program can read the accumulator’s value, using its value method.

因此,当尝试从 Spark 中的任务中读取累加器的值时,意味着您尝试从工作线程读取其值,这违背了仅从驱动程序读取累加器值的概念。

关于java - 任务未读取 Spark 累加器值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35983824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com