spark 2.2.0 共享变量使用方法 java版

package cn.spark.study.core;


import java.util.*;


import org.apache.spark.SparkConf;

import org.apache.spark.api.java.JavaRDD;

import org.apache.spark.api.java.JavaSparkContext;

import org.apache.spark.api.java.function.Function;

import org.apache.spark.api.java.function.VoidFunction;

import org.apache.spark.broadcast.Broadcast;


public class BroadcastVairable {

public static void main(String[] args) {

SparkConf conf =new SparkConf().setAppName("persist").setMaster("local");

JavaSparkContext sc =new JavaSparkContext(conf);

final int factor = 3;

final Broadcast fac =sc.broadcast(factor);

List numberlist =Arrays.asList(1,2,3,4,5);

JavaRDD num =sc.parallelize(numberlist);

JavaRDD mulnum.map(new Function() {


@Override

public Integer call(Integer arg0) throws Exception {

int fact = fac.value();

return arg0 *fact;

}

});

mul.foreach(new VoidFunction() {

@Override

public void call(Integer arg0) throws Exception {

System.out.println(arg0);

}

});

sc.close();

}

}

你可能感兴趣的:(spark,共享变量)