spark出现task不能序列化错误的解决方法 org.apache.spark.SparkException: Task not serializable
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的更多相关文章
- spark出现task不能序列化错误的解决方法
应用场景:使用JavaHiveContext执行SQL之后,希望能得到其字段名及相应的值,但却出现"Caused by: java.io.NotSerializableException: ...
- Linux出现Read-only file system错误的解决方法
造成这个问题的解决办法大多数是由于非正常关机后导致文件系统受损引起的,在系统重新启动之后,受损分区就会被Linux自己主动挂载为仅仅读.解决办法是通过fsck来修复文件系统,然后重新启动就可以,下面是 ...
- Eclipse启动时发生An internal error occurred during: "Initializing Java Tooling".错误的解决方法
问题描述: Eclipse启动时发生An internal error occurred during: "Initializing JavaTooling".错误的解决方法 解决 ...
- 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/ ...
- Linux系统Vsftp 传文件出现 553 Could Not Create File错误的解决方法
解决方法: 登录出现了这个错误提示:553 Could not create file SELinux设置如下 查看SELinux设置 [root@localhost ~]# getsebool -a ...
- [转]权限问题导致Nginx 403 Forbidden错误的解决方法
权限问题导致Nginx 403 Forbidden错误的解决方法 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-08-22 这篇文章主要介绍了权限问题导致Nginx 403 F ...
- ueditor上传大容量视频报http请求错误的解决方法
故障现象: 当使用百度编辑器ueditor上传大容量视频或大容量图片的时候,编辑器报"http请求错误"的解决方法详解: 原因分析: 目前很多CMS整合了百度的ueditor编辑器 ...
- window10 安装出现the error code is 2503错误的解决方法
window10 安装出现the error code is 2503错误的解决方法: 设置 C:\WINDOWS\TEMP的权限
- MyEclipse+Tomcat 启动时出现A configuration error occured during startup错误的解决方法
MyEclipse+Tomcat 启动时出现A configuration error occured during startup错误的解决方法 分类: javaweb2013-06-03 14:4 ...
随机推荐
- Linux下tomcat的shutdown命令可以关闭服务但是杀不死进程
Linux下tomcat的shutdown命令可以关闭服务但是杀不死进程 原因: 一般造成这种原因是因为项目中有非守护线程的存在: 解决方案: 一.从Tomcat上解决 方案1:(推荐的方案:因为一台 ...
- HTML 和 JavaScript 编写简单的 404 界面
编写简单的 404 界面,也可以用来做 500 报错界面,还会飘东西,特别好,蛮漂亮的! <!DOCTYPE html> <html> <head> <met ...
- Android Socket 知识点汇总
版权声明:这篇博客资料来源 https://blog.csdn.net/carson_ho/article/details/53366856 , 未经授权,严禁转发! 一.网络基础 1.1 计算机网络 ...
- Android实践项目汇报(总结)
天气客户端开发报告 1 系统需求分析 1.1功能性需求分析 天气预报客户端,最基本就是为用户提供准确的天气预报信息.天气查询结果有两种:一种是当天天气信息,信息结果比较详细,除温度.天气状况外还 ...
- UVa 714 Copying Books - 二分答案
求使最大值最小,可以想到二分答案. 然后再根据题目意思乱搞一下,按要求输出斜杠(这道题觉得就这一个地方难). Code /** * UVa * Problem#12627 * Accepted * T ...
- CEF解决加载慢问题
转载:http://blog.csdn.net/weolar/article/details/51994895 CEF加载慢的时候,加上以下代码,通过命令行的方式: CefRefPtr<CefC ...
- 【监控】dubbo监控中心安装
使用dubbo的话,两个工具是不可少的: 1:dubbo的管理控制台,在之前的笔记中介绍过 2:简易控制中心monitor 简单介绍下monitor: Simple Monitor挂掉不会影响到Con ...
- Java-master(github)教材整理
helloworld class HelloWorld { public static void main(String[] args) { System.out.println("hell ...
- C#学习笔记(七):结构体、数组、冒泡排序和调试
结构体 结构体不能重写默认无参构造函数 一位数组 using System; using System.Collections.Generic; using System.Linq; using Sy ...
- DPDK无法分出大页面:EAL: No free hugepages reported in hugepages-2048kB 解决方法
参考: [dpdk-users] Fw: DPDK Error --> EAL: No free hugepages reported in hugepages-2048kB DPDK无法分出连 ...