import org.elasticsearch.cluster.routing.Murmur3HashFunction;
import org.elasticsearch.common.math.MathUtils; // 自定义Partitioner
class 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的更多相关文章

  1. spark出现task不能序列化错误的解决方法

    应用场景:使用JavaHiveContext执行SQL之后,希望能得到其字段名及相应的值,但却出现"Caused by: java.io.NotSerializableException: ...

  2. Linux出现Read-only file system错误的解决方法

    造成这个问题的解决办法大多数是由于非正常关机后导致文件系统受损引起的,在系统重新启动之后,受损分区就会被Linux自己主动挂载为仅仅读.解决办法是通过fsck来修复文件系统,然后重新启动就可以,下面是 ...

  3. Eclipse启动时发生An internal error occurred during: "Initializing Java Tooling".错误的解决方法

    问题描述: Eclipse启动时发生An internal error occurred during: "Initializing JavaTooling".错误的解决方法 解决 ...

  4. GCC-4.6.3编译linux2.6.32.12内核出现“重复的成员‘page’”错误的解决方法

    使用gcc4.6.3编译linux2.6.32.12内核出现错误如下: In file included from drivers/net/igbvf/ethtool.c:36:0: drivers/ ...

  5. Linux系统Vsftp 传文件出现 553 Could Not Create File错误的解决方法

    解决方法: 登录出现了这个错误提示:553 Could not create file SELinux设置如下 查看SELinux设置 [root@localhost ~]# getsebool -a ...

  6. [转]权限问题导致Nginx 403 Forbidden错误的解决方法

    权限问题导致Nginx 403 Forbidden错误的解决方法 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-08-22 这篇文章主要介绍了权限问题导致Nginx 403 F ...

  7. ueditor上传大容量视频报http请求错误的解决方法

    故障现象: 当使用百度编辑器ueditor上传大容量视频或大容量图片的时候,编辑器报"http请求错误"的解决方法详解: 原因分析: 目前很多CMS整合了百度的ueditor编辑器 ...

  8. window10 安装出现the error code is 2503错误的解决方法

    window10 安装出现the error code is 2503错误的解决方法:  设置 C:\WINDOWS\TEMP的权限

  9. MyEclipse+Tomcat 启动时出现A configuration error occured during startup错误的解决方法

    MyEclipse+Tomcat 启动时出现A configuration error occured during startup错误的解决方法 分类: javaweb2013-06-03 14:4 ...

随机推荐

  1. MySQL按中文拼音排序

    好多时候,我们希望查询出来的记录能够按照汉语拼音即英文的26个字母排序,但是utf字符集是外国人弄的,不是按照汉语拼音的顺序排列的,因此,我们需要将要排序的字段把编码设定为GBK或者BG2312再进行 ...

  2. 20145305 《网络对抗》逆向及Bof基础实践

    逆向及Bof基础实践说明 实践说明地址 实验过程及结果截图

  3. 20145324王嘉澜 《网络对抗》进阶实践之 shellcode注入和Return-to-libc攻击深入

    Shellcode注入 •Shellcode实际是一段代码,但却作为数据发送给受攻击服务器,将代码存储到对方的堆栈中,并将堆栈的返回地址利用缓冲区溢出,覆盖成为指向 shellcode的地址 •实验参 ...

  4. 微信小程序——2、配置json文件

    配置文件详解 主配置文件app.json 主配置文件位于主目录中,用于进行全局配置.包括页面文件的路径.窗口表现.设置网络超时时间.设置多tab等 下面通过微信最初自带小程序来学习 { "p ...

  5. 20145319 《网络渗透》web安全基础实践

    20145319 <网络渗透>web安全基础实践 问题回答 Sql注入攻击原理,如何防御 攻击原理:由于对于用户输入并没做出相应限制,因此可以通过构造特定的sql语句,达到自身的一些非法目 ...

  6. Python3基础 try-指定except-as reason 捕获打开一个不存在的文件的时候,会产生OSError异常的示例

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. Harmonic Number (调和级数+欧拉常数)题解

    Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...

  8. 【第八章】 springboot + mybatis + 多数据源

    在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1)DatabaseType列出所有的数据源的key---key 2)DatabaseConte ...

  9. Ubuntu 14.04 更新gcc版本至4.9.2

    参考: ubuntu 14.04 更新 gcc/g++ 4.9.2 Ubuntu 14.04 更新gcc版本至4.9.2 1.更新源,安装gcc v4.9.2 $ sudo add-apt-repos ...

  10. UVa 714 抄书(贪心+二分)

    https://vjudge.net/problem/UVA-714 题意:把一个包含m个正整数的序列划分成k个非空的连续子序列,使得每个正整数恰好属于一个序列.设第i个序列的各数之和为S(i),你的 ...