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. P2880 [USACO07JAN]平衡的阵容Balanced Lineup

    P2880 [USACO07JAN]平衡的阵容Balanced Lineup RMQ RMQ模板题 静态求区间最大/最小值 (开了O2还能卡到rank9) #include<iostream&g ...

  2. 05:ModelForm 数据验证 & 生成html & 数据库操作

    目录:Django其他篇 01:Django基础篇 02:Django进阶篇 03:Django数据库操作--->Model 04: Form 验证用户数据 & 生成html 05:Mo ...

  3. Duilib 实现右下角弹出像QQ新闻窗口,3秒后自动关闭(一)

    转载:https://blog.twofei.com/667/ 自绘或子类化控件时,有时需要处理鼠标进入(MouseEnter)/鼠标离开(MouseLeave)/鼠标停留(MouseHover)消息 ...

  4. React 回忆录(一)为什么使用 React?

    Hi 各位,欢迎来到 React 回忆录!

  5. 以太坊(Ethereum) - 节点时间未同步和区块同步失败案例分析

    背景 以太坊技术搭建的区块链网络,节点间需要保证时间一致,才能正常有序的发送交易和生成区块,使得众多节点共同维护分布式账本(区块数据+状态数据).但是,网络中节点的系统时间不一致回出现什么现象呢,我们 ...

  6. 导出数据库表为world文档说明,以及PowerDesigner导出表结构pdm设计文档

    如何使用“mysql导出数据库结构为world工具”以及如何使用powerdesigner映射数据库模型 一.通过powerdesigner配置ojdbc 1.安装并打开powerdesigner,新 ...

  7. hystrix两种隔离模式分析

    hystrix隔离模式目前有两种方式:信号量模式和线程池模式. 但信号量并不支持超时,当被调服务发生问题时,有少部分用户会长时间无法得到响应. 另外,使用线程池模式无法传递Header,我估计是由于线 ...

  8. Vue项目中使用Vuex + axios发送请求

    本文是受多篇类似博文的影响写成的,内容也大致相同.无意抄袭,只是为了总结出一份自己的经验. 一直以来,在使用Vue进行开发时,每当涉及到前后端交互都是在每个函数中单独的写代码,这样一来加大了工作量,二 ...

  9. 《EMCAScript6入门》读书笔记——16.Generator函数的语法

    鼠标指针移到图片上,右键,选择在“在新标签页中打开”,放大即可看到清晰文字.

  10. 项目梳理4——WebApi项目,使用注释填充Description字段

    web.config中添加连接字符串: 为webapi添加Description,使用注释来填充此字段 对于所有引用的xxxx.base项目设置生成的xml文档,注意release.debug下都需设 ...