在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 ...
随机推荐
- 页面回到顶部的三种实现(锚标记,js)
一.使用锚标记返回页面顶部 使用HTML锚标记最简单,就是看起来有点不好看,点击后会在地址栏显示这个锚标记,其它的倒没什么. 页面顶部放置: <a name="top" id ...
- 各种样式的table 及 代码
1.模板一 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...
- 用Python读取大文件
通常我们在读取文件的时候,会用到read(), readline(), readlines(). 通常可能会有这样的用法: def test1(): with open("/tmp/test ...
- POJ 1163 The Triangle DP题解
寻找路径,动态规划法题解. 本题和Leetcode的triangle题目几乎相同一样的,本题要求的是找到最大路径和. 逆向思维.从底往上查找起就能够了. 由于从上往下能够扩展到非常多路径.而从下往上个 ...
- Eclipse Source not found
Eclipse debug模式下找不到Java源文件 CreateTime--2018年3月19日10:43:39 Author:Marydon 与MyEclipse不同,每次Eclipse导入新 ...
- sqlplus命令手冊
show和set命令是两条用于维护SQLPlus系统变量的命令 : SQL> show all --查看全部系统变量值 SQL>show user --显示当前连接用户 SQL>sh ...
- 【Docker】安装并测试安装成功
1.环境描述 Centos 7 2.安装步骤 通过命令yum install docker安装 等待下载安装-,出现下图,按y继续 继续等待-出现下图按y继续 再继续等待- 知道出现上图表示安装完毕 ...
- SSO之CAS + LDAP
本来主要详细是介绍CAS和LDAP整合实现单点登录的步骤. 1. 依<SSO之安装CAS Server>所述安装好CAS Server.2. 安装ApacheDS.安装好ApacheDS后 ...
- RabbitMQ消息队列生产者和消费者
概述 生产者生产数据至 RabbitMQ 队列,消费者消费 RabbitMQ 队列里的数据. 详细 代码下载:http://www.demodashi.com/demo/10723.html 一.准备 ...
- HDUOJ -----Color the ball
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...