Spark设置Kryo序列化缓冲区大小

背景

今天在开发SparkRDD的过程中出现Buffer Overflow错误,查看具体Yarn日志后发现是因为Kryo序列化缓冲区溢出了,日志建议调大spark.kryoserializer.buffer.max的value,搜索了一下设置keyo序列化缓冲区的方法,特此整理记录下来。

20/01/08 17:12:55 WARN scheduler.TaskSetManager: Lost task 1.0 in stage 1.0 (TID 4, s015.test.com, executor 1): org.apache.spark.SparkException: Kryo serialization failed: Buffer overflow. Available: 0, required: 10300408. To avoid this, increase spark.kryoserializer.buffer.max value.
    at org.apache.spark.serializer.KryoSerializerInstance.serialize(KryoSerializer.scala:315)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:367)
    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)

方法一:通过conf参数设置spark.kryoserializer.buffer.max

spark-submit在提交spark作业时可以带很多参数,其中有一个参数--conf可以设置spark.kryoserializer.buffer.max的大小,具体如下。

./bin/spark-submit \
  --class <main-class> \
  --master <master-url> \
  --deploy-mode <deploy-mode> \
  --conf spark.kryoserializer.buffer.max=512m \
  ... # other options
  <application-jar> \
  [application-arguments]

上面的--conf spark.kryoserializer.buffer.max=512m即代表把Kryo序列化缓冲区的buffer大小设置为512mb。

方法二:通过程序中拿到sparkConf对象设置spark.kryoserializer.buffer.max

1.设置Kryo为序列化类

//设置Kryo为序列化类(默认为Java序列类)
sparkConf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer");

2.设置spark.kryoserializer.buffer.max的值

//两种设置方法
sparkConf.set("spark.kryoserializer.buffer.max", "128m");
sparkConf.set("spark.kryoserializer.buffer.max.mb", "128");

3.检查是否成功设置Kryo参数

//打印日志,检查是否成功设置
System.out.println( sparkConf.get("spark.kryoserializer.buffer.max") );

参考文献

[1]【大数据进击】如何设置spark.kryoserializer.buffer.max value
[2]Spark official docs: Submitting Applications

posted @ 2020-01-08 23:39  JasonCeng  阅读(...)  评论(...编辑  收藏