本文主要是介绍java-spark广播变量,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 1. java spark使用广播变量方式
- 2. 运行时spark任务报错
- 1. 原因
- 2. 解决方案
1. java spark使用广播变量方式
在java spark中如果想要使用广播变量需要使用JavaSparkContext.broadcast()
方法
代码如下
SparkSession sparkSession = SparkSession.builder().config(sparkConf).getOrCreate();JavaSparkContext javaSparkContext = JavaSparkContext.fromSparkContext(sparkSession.sparkContext());Dataset<Row> labelDimensionTable = sparkSession.read().parquet(labelDimPath);Map<String, Long> labelNameToId = getNameToId(labelDimensionTable);Broadcast<Map<String, Long>> labelNameIdBroadcast = javaSparkContext.broadcast(labelNameToId);Map<String, Long> getNameToId(Dataset<Row> labelDimTable) {return labelDimTable.javaRDD().mapToPair(new PairFunction() {@Overridepublic Tuple2 call(Object object) throws Exception {Row curRow = (Row) object;Long labelId = curRow.getAs("label_id");String labelTitle = curRow.getAs("label_title");return Tuple2.apply(labelTitle, labelId);}}).collectAsMap();}
2. 运行时spark任务报错
20/09/09 18:23:00 INFO scheduler.TaskSetManager: Starting task 0.0 in stage 5.0 (TID 4008, node-hadoop67.com, executor 3, partition 0, RACK_LOCAL, 8608 bytes)
20/09/09 18:23:00 INFO storage.BlockManagerInfo: Added broadcast_9_piece0 in memory on node-hadoop67.com:23191 (size: 41.1 KB, free: 2.5 GB)
20/09/09 18:23:01 INFO storage.BlockManagerInfo: Added broadcast_8_piece0 in memory on node-hadoop67.com:23191 (size: 33.5 KB, free: 2.5 GB)
20/09/09 18:23:02 INFO storage.BlockManagerInfo: Added broadcast_5_piece1 in memory on node-hadoop67.com:23191 (size: 698.1 KB, free: 2.5 GB)
20/09/09 18:23:02 INFO storage.BlockManagerInfo: Added broadcast_5_piece0 in memory on node-hadoop67.com:23191 (size: 4.0 MB, free: 2.5 GB)
20/09/09 18:23:02 WARN scheduler.TaskSetManager: Lost task 0.0 in stage 5.0 (TID 4008, node-hadoop67.com, executor 3): java.io.IOException: java.lang.UnsupportedOperationExceptionat org.apache.spark.util.Utils$.tryOrIOException(Utils.scala:1367)at org.apache.spark.broadcast.TorrentBroadcast.readBroadcastBlock(TorrentBroadcast.scala:207)at org.apache.spark.broadcast.TorrentBroadcast._value$lzycompute(TorrentBroadcast.scala:66)at org.apache.spark.broadcast.TorrentBroadcast._value(TorrentBroadcast.scala:66)at org.apache.spark.broadcast.TorrentBroadcast.getValue(TorrentBroadcast.scala:96)at com.kk.search.user_profile.task.user_profile.UserLabelProfile$1.call(UserLabelProfile.java:157)at org.apache.spark.sql.Dataset$$anonfun$44.apply(Dataset.scala:2605)at org.apache.spark.sql.Dataset$$anonfun$44.apply(Dataset.scala:2605)at org.apache.spark.sql.execution.MapPartitionsExec$$anonfun$5.apply(objects.scala:188)at org.apache.spark.sql.execution.MapPartitionsExec$$anonfun$5.apply(objects.scala:185)at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsInternal$1$$anonfun$apply$25.apply(RDD.scala:836)at org.apache.spark.rdd.RDD$$anonfun$mapPartitionsInternal$1$$anonfun$apply$25.apply(RDD.scala:836)at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:49)at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:49)at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:49)at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:49)at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:324)at org.apache.spark.rdd.RDD.iterator(RDD.scala:288)at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)at org.apache.spark.scheduler.Task.run(Task.scala:109)at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:381)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.UnsupportedOperationExceptionat java.util.AbstractMap.put(AbstractMap.java:209)at com.esotericsoftware.kryo.serializers.MapSerializer.read(MapSerializer.java:162)at com.esotericsoftware.kryo.serializers.MapSerializer.read(MapSerializer.java:39)at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:790)at org.apache.spark.serializer.KryoDeserializationStream.readObject(KryoSerializer.scala:278)at org.apache.spark.broadcast.TorrentBroadcast$$anonfun$8.apply(TorrentBroadcast.scala:308)at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1394)at org.apache.spark.broadcast.TorrentBroadcast$.unBlockifyObject(TorrentBroadcast.scala:309)at org.apache.spark.broadcast.TorrentBroadcast$$anonfun$readBroadcastBlock$1$$anonfun$apply$2.apply(TorrentBroadcast.scala:235)at scala.Option.getOrElse(Option.scala:121)at org.apache.spark.broadcast.TorrentBroadcast$$anonfun$readBroadcastBlock$1.apply(TorrentBroadcast.scala:211)at org.apache.spark.util.Utils$.tryOrIOException(Utils.scala:1360)... 29 more20/09/09 18:23:02 INFO scheduler.TaskSetManager: Starting task 0.1 in stage 5.0 (TID 4009, node-hadoop64.com, executor 7, partition 0, RACK_LOCAL, 8608 bytes)
关注一下具体的cause
Caused by: java.lang.UnsupportedOperationExceptionat java.util.AbstractMap.put(AbstractMap.java:209)
1. 原因
原来是因为序列化的问题,在使用java api的时候,如果broadcast的变量是使用line_RDD_2.collectAsMap()
的方式产生的,那么被广播的类型就是Map, kryo 不知道真实的对象类型,所以就会采用AbstractMap来进行解析。
2. 解决方案
所以我们要新建一个map,将line_RDD_2.collectAsMap()
放入新建的map即可。
原来的代码为
Map<String, Long> getNameToId(Dataset<Row> labelDimTable) {return labelDimTable.javaRDD().mapToPair(new PairFunction() {@Overridepublic Tuple2 call(Object object) throws Exception {Row curRow = (Row) object;Long labelId = curRow.getAs("label_id");String labelTitle = curRow.getAs("label_title");return Tuple2.apply(labelTitle, labelId);}}).collectAsMap();}
修改为
Map<String, Long> getNameToId(Dataset<Row> labelDimTable) {Map<String, Long> res = new HashMap<>();Map<String, Long> apiMap= labelDimTable.javaRDD().mapToPair(new PairFunction() {@Overridepublic Tuple2 call(Object object) throws Exception {Row curRow = (Row) object;Long labelId = curRow.getAs("label_id");String labelTitle = curRow.getAs("label_title");return Tuple2.apply(labelTitle, labelId);}}).collectAsMap();res.putAll(apiMap);return res;}
参考
https://www.jianshu.com/p/f478376bdbb9
https://stackoverflow.com/questions/43023961/spark-kryo-serializers-and-broadcastmapobject-iterablegowalladatalocation
这篇关于java-spark广播变量的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!