在Spark中通过Scala + Mongodb实现连接池
How to implement connection pool in spark
问题所在
dstream.foreachRDD { rdd =>
rdd.foreachPartition { partitionOfRecords =>
// ConnectionPool is a static, lazily initialized pool of connections
val connection = ConnectionPool.getConnection()
partitionOfRecords.foreach(record => connection.send(record))
ConnectionPool.returnConnection(connection) // return to the pool for future reuse
}}
可是如何具体实现呢?
Scala + Mongodb实现连接池
一个通常意义上的连接池,能够请求获取资源,也能释放资源。不过MongoDB java driver已经帮我们实现了这一套逻辑。
Note: The Mongo object instance actually represents a pool of connections to the database; you will only need one object of class Mongo even with multiple threads. See the concurrency doc page for more information.
The Mongo class is designed to be thread safe and shared among threads. Typically you create only 1 instance for a given DB cluster and use it across your app. If for some reason you decide to create many mongo intances, note that:
all resource usage limits (max connections, etc) apply per mongo instance to dispose of an instance, make sure you call mongo.close() to clean up resources
也就是说,我们的pool,只要能获得Mongo就可以了。也就是说每次请求,在executor端,能get已经创建好了MongoClient就可以了。
object MongoPool {
var instances = Map[String, MongoClient]()
//node1:port1,node2:port2 -> node
def nodes2ServerList(nodes : String):java.util.List[ServerAddress] = {
val serverList = new java.util.ArrayList[ServerAddress]()
nodes.split(",")
.map(portNode => portNode.split(":"))
.flatMap{ar =>{
if (ar.length==2){
Some(ar(0),ar(1).toInt)
}else{
None
}
}}
.foreach{case (node,port) => serverList.add(new ServerAddress(node, port))}
serverList
}
def apply(nodes : String) : MongoClient = {
instances.getOrElse(nodes,{
val servers = nodes2ServerList(nodes)
val client = new MongoClient(servers)
instances += nodes -> client
println("new client added")
client
})
}
}
这样,一个static 的MongoPool的Object已经创建,scala Ojbect类,在每个JVM中会初始化一次。
rdd.foreachPartition(partitionOfRecords => {
val nodes = "node:port,node2:port2"
lazy val client = MongoPool(nodes)
lazy val coll2 = client.getDatabase("dm").getCollection("profiletags")
partitionOfRecords.grouped(500).foreach()
})
注意,此处client用lazy修饰,等到executor使用client的时候,才会执行。否则的话,会出现client not serializable.
优点分析
1.不重复创建,销毁跟数据库的连接,效率高。 Spark 每个executor 申请一个JVM进程,task是多线程模型,运行在executor当中。task==partition数目。Object只在每个JVM初始化一次。
2.代码design pattern
参考资料
在Spark中通过Scala + Mongodb实现连接池的更多相关文章
- java操作mongodb(连接池)(转)
原文链接: java操作mongodb(连接池) Mongo的实例其实就是一个数据库连接池,这个连接池里默认有10个链接.我们没有必要重新实现这个链接池,但是我们可以更改这个连接池的配置.因为Mong ...
- 如何在 Swoole 中优雅的实现 MySQL 连接池
如何在 Swoole 中优雅的实现 MySQL 连接池 一.为什么需要连接池 ? 数据库连接池指的是程序和数据库之间保持一定数量的连接不断开, 并且各个请求的连接可以相互复用, 减少重复连接数据库带来 ...
- 【转】SSH中 整合spring和proxool 连接池
[摘要:比来做的一个项目中应用到了毗邻池技巧,大概我们人人比拟认识的开源毗邻池有dbcp,c3p0,proxool.对那三种毗邻池来讲,从机能战失足率来讲,proxool轻微比前两种好些.本日我首要简 ...
- MongoDB设置连接池操作百万级以上数据
开发环境 spring 4.3.7 + springBoot 1.5.2 + dubbo 2.6.5 + mongoDB 4.0.0 连接池配置 mongo-pool.properties sprin ...
- Golang 连接 MongoDB使用连接池
可以免费试用 MongoDB ,500MB 平时做测试没有问题啦,连接数据库可能因为网络有点慢,但是我们是测试啊,不在乎这点吧~ 这是怎么申请试用版的博客,感谢这位大佬.注册好用起来很方便~ 传送门 ...
- SSH框架中配置Hibernate使用proxool连接池
一.导入proxool.jar包 案例用的是proxool-0.8.3.jar,一般通过MyEclipse配置的SSH都会包含这个jar,如果没有,就去网上搜下下载导入就好了. 二.新建Proxool ...
- 使用MongoDB 2.6 C++驱动中的连接池
.post p{text-indent: 2em;} MongoDB2.6的CXX驱动(mongo-cxx-driver-26compat),内置包含了数据库连接池,方便管理数据库连接,但是官方文档说 ...
- Spring框架中 配置c3p0连接池 完成对数据库的访问
开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...
- 在Tomcat中配置连接池和数据源
1.DataSource接口介绍 (1)DataSource 概述 JDBC1.0原来是用DriverManager类来产生一个对数据源的连接.JDBC2.0用一种替代的方法,使用DataSource ...
随机推荐
- ubuntu 下安装摄像头驱动
sudo apt-get install cheese sudo apt-get install camorama 然后可以打开应用cheese,观察可以得到图像. 也可以通过代码获取图像.pytho ...
- sublime Text3 JsFormat
https://github.com/sprinng/JsFormat 下载解压 放到 首选项 --> 浏览插件目录 重启sublime ctrl+shift+p 安装 使用: 右键JsForm ...
- javaScriptObject转String
function obj2str(o){ var r = []; if(typeof o =="string") return "\""+o.repl ...
- eclipse手动指定启动的jdk版本
在eclipse.ini中添加 -vm D:/wwjDocument/JDK6/jre/bin/client/jvm.dll -vmargs -Dosgi.requiredJavaVersion=1. ...
- rlwrap插件,实现sqlplus上下翻页
oracle在Linux下,sqlplus中不能上下翻,最主要我经常打错字!嘿嘿 01.下载 RPM :http://rpmfind.net/linux/rpm2html/search.php?qu ...
- Linear Algebra Courses
Lecture 1:Demonstrate the columns of a matrix (imagine the vectors) in N-dimension space.How to mult ...
- PHP-手册阅读笔记
1.第一次遇到$_ENV为空数组的情况, 原来是PHP.INI中variables_order为'GPCS'(表示系统在定义PHP预定义变量时的顺序是GET,POST,COOKIES,SERVER,只 ...
- 那些遇到的position-fixed无效事件
本篇文章由:http://xinpure.com/position-fixed-encountered-an-invalid-event/ 第一次无效事件 事件主角: transform 应用环境: ...
- mybatis报错 Specified class is an interface
本文转自:http://blog.csdn.net/u014331288/article/details/53869303 1.错误原因 Caused by: org.springframework. ...
- MFC中创建多线程
1. 列举几种进程的同步机制,并比较其优缺点. 原子操作 信号量机制 自旋锁 管程,会合,分布式系统 2. 进程之间通信的途径 共享存储系统 消息传递系统 ...