spark出現task不能序列化錯誤的解決方案 org.apache.spark.SparkException: Task not serializable

來源:互聯網
上載者:User

標籤:driver   with   create   last   utils   util   odi   方法   ror   

import org.elasticsearch.cluster.routing.Murmur3HashFunction;import org.elasticsearch.common.math.MathUtils;// 自訂Partitionerclass ESShardPartitioner(settings: String) extends org.apache.spark.Partitioner {  protected var _numPartitions = -1;    protected var _hashFunction = new org.elasticsearch.cluster.routing.Murmur3HashFunction;//此處會出現序列化錯誤  override def numPartitions: Int = {    val newSettings = new org.elasticsearch.hadoop.cfg.PropertiesSettings().load(settings);    // 生產環境下,需要自行設定索引的 index/type,我是以web/blog作為實驗的index    newSettings.setResourceRead("web/blog"); // ******************** !!! modify it !!! ********************     newSettings.setResourceWrite("web/blog"); // ******************** !!! modify it !!! ********************     val repository = new org.elasticsearch.hadoop.rest.RestRepository(newSettings);    val targetShards = repository.getWriteTargetPrimaryShards(newSettings.getNodesClientOnly());    repository.close();    // targetShards ??? data structure    _numPartitions = targetShards.size();    println("********************numPartitions*************************");    println(_numPartitions);    _numPartitions;  }  override def getPartition(docID: Any): Int = {        val r = _hashFunction.hash(docID.toString());    val shardId = org.elasticsearch.common.math.MathUtils.mod(r, _numPartitions);    println("********************shardId*************************");    println(shardId)    shardId;  }}

根源:出現“task not serializable"這個錯誤,一般是因為在map、filter等的參數使用了外部的變數,但是這個變數不能序列化。特別是當引用了某個類(經常是當前類)的成員函數或變數時,會導致這個類的所有成員(整個類)都需要支援序列化。

解決方案:

class ESShardPartitioner(settings: String) extends org.apache.spark.Partitioner {  protected var _numPartitions = -1;    override def numPartitions: Int = {    val newSettings = new org.elasticsearch.hadoop.cfg.PropertiesSettings().load(settings);    // 生產環境下,需要自行設定索引的 index/type,我是以web/blog作為實驗的index    newSettings.setResourceRead("web/blog"); // ******************** !!! modify it !!! ********************     newSettings.setResourceWrite("web/blog"); // ******************** !!! modify it !!! ********************     val repository = new org.elasticsearch.hadoop.rest.RestRepository(newSettings);    val targetShards = repository.getWriteTargetPrimaryShards(newSettings.getNodesClientOnly());    repository.close();    // targetShards ??? data structure    _numPartitions = targetShards.size();    println("********************numPartitions*************************");    println(_numPartitions);    _numPartitions;  }  override def getPartition(docID: Any): Int = {    val _hashFunction = new org.elasticsearch.cluster.routing.Murmur3HashFunction;    val r = _hashFunction.hash(docID.toString());    val shardId = org.elasticsearch.common.math.MathUtils.mod(r, _numPartitions);    println("********************shardId*************************");    println(shardId)    shardId;  }}

 

Job aborted due to stage failure: Task not serializable:

If you see this error:

org.apache.spark.SparkException: Job aborted due to stage failure: Task not serializable: java.io.NotSerializableException: ...

The above error can be triggered when you intialize a variable on the driver (master), but then try to use it on one of the workers. In that case, Spark Streaming will try to serialize the object to send it over to the worker, and fail if the object is not serializable. Consider the following code snippet:

NotSerializable notSerializable = new NotSerializable();JavaRDD<String> rdd = sc.textFile("/tmp/myfile");rdd.map(s -> notSerializable.doSomething(s)).collect();

This will trigger that error. Here are some ideas to fix this error:

  • Serializable the class
  • Declare the instance only within the lambda function passed in map.
  • Make the NotSerializable object as a static and create it once per machine.
  • Call rdd.forEachPartition and create the NotSerializable object in there like this:
rdd.forEachPartition(iter -> {  NotSerializable notSerializable = new NotSerializable();  // ...Now process iter});

參考:https://databricks.gitbooks.io/databricks-spark-knowledge-base/content/troubleshooting/javaionotserializableexception.html

spark出現task不能序列化錯誤的解決方案 org.apache.spark.SparkException: Task not serializable

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.