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 hang and srv_error_monitor_thread using 100% cpu(已解决)

    昨天晚上,运维过来说有台生产服务器的mysql cpu一直100%,新的客户端登录不了,但是已经在运行的应用都正常可用. 登录服务器后,top -H看了下,其中一个线程的cpu 一直100%,其他的几 ...

  2. Unix/Linux系统编程

    一,开发工具 编译器 GCC 调试工具 GDB 代码编辑 Vim 1. 编译命令 gcc hello.c -o hello # 第二个hello为新生成的可执行文件名 -o 为生成的可执行文件指定名称 ...

  3. 20145322 Exp5 利用nmap扫描

    20145322 Exp5 利用nmap扫描 实验过程 使用命令创建一个msf所需的数据库 service postgresql start msfdb start 使用命令msfconsole开启m ...

  4. 定义c/c++全局变量/常量几种方法的区别(转载)

    出自:http://www.cnblogs.com/yaozhongxiao/archive/2010/08/08/1795338.html 在讨论全局变量之前我们先要明白几个基本的概念:  1. 编 ...

  5. 【自动化】基于Spark streaming的SQL服务实时自动化运维

    设计背景 spark thriftserver目前线上有10个实例,以往通过监控端口存活的方式很不准确,当出故障时进程不退出情况很多,而手动去查看日志再重启处理服务这个过程很低效,故设计利用Spark ...

  6. 【ssh免登录】设置集群环境ssh免登录步骤

    1.每台机器都需要执行,生成自己的密钥 # ssh-keygen -t rsa 过程中遇到选项,全部enter #cd ~/.ssh # cat id_rsa.pub > authorized_ ...

  7. 51nod 1201 整数划分 基础DP

    1201 整数划分  基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 将N分为若干个不同整数的和,有多少种不同的划分方式,例如:n = 6,{6} ...

  8. hive学习4(hive的脚本执行)

    hive的脚本执行 hive -e "SQL" hvie -f file 实例 [root@spark1 ~]# hive -e "show tables" # ...

  9. C# 新Form各事件执行顺序

    1. 构造函数 2. Load() 3. Show() 4. Acticated()

  10. nohup 日志切割

    最近遇到日志切割的问题,即程序是通过命令: nohup python *.py & 放到后台执行的,这样程序的日志输出到了nohup自动生成的nohup.out文件. 问题就来了,nohup. ...