如何在spark 2(java)中创建广播变量?

时间:2017-08-26 13:16:13

标签: java apache-spark-2.0

在Spark 1中,我们可以使用以下代码创建Spark广播变量:

SparkConf conf = new SparkConf();
conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer");

JavaSparkContext sc = new JavaSparkContext("local", "JavaAPISuite", conf);

ArrayList<Strin'valuesg> sampleList = new ArrayList<String>();
sampleList.add("value");

final Broadcast<ArrayList<String> broadcastVar = sc.broadcast(sampleList);

我们如何使用下面显示的API在Spark 2中做同样的事情?

SparkSession sc = SparkSession.setappName("SparkApp").getorcreate();
sc.sparkcontext().broadcast(T value, scala.reflect.ClassTag<T> evidence$11)

1 个答案:

答案 0 :(得分:1)

例如,如果您要广播名为Test的类,则应该实现java.io.Serializable,然后执行:

import scala.reflect.ClassTag;

ClassTag<Test> classTagTest = scala.reflect.ClassTag$.MODULE$.apply(Test.class);
Broadcast<Test> broadcastTest = sc.sparkcontext().broadcast(new Test(), classTagTest);