在spark streaming的文档里,有这么一段:

def sendPartition(iter):
# ConnectionPool is a static, lazily initialized pool of connections
connection = ConnectionPool.getConnection()
for record in iter:
connection.send(record)
# return to the pool for future reuse
ConnectionPool.returnConnection(connection) dstream.foreachRDD(lambda rdd: rdd.foreachPartition(sendPartition))

但是怎么让worker得到一个ConectionPool呢?简单的想法是在使用static变量指向一个ConnectionPool。但这里有一个讲究:怎么保证这个ConnectionPool是worker上的,而不是driver上的?

用pyhton为例:

在ConnectionPool.py里实现一个pool

#/usr/bin/python
#connection_pool.py
import psycopg2
import settings from DBUtils.PooledDB import PooledDB pool = PooledDB(psycopg2, settings.connection_pool_size,
host=settings.db_host,
database=settings.database,
user=settings.db_user,
password=settings.db_password)
def getConnection():
return pool.connection()

假设stream的主代码在main.py里,提交spark

spark-submit --py-files connection_pool.py main.py

这样connection_pool.py将被发送到worker执行,main.py里的 sendPartition 在worker节点上执行的时候就可以获得ConnectionPool.getConnection()调用。

这里的关键是明白哪些代码在driver上跑,哪些在worker上跑。

 

How to implement connection pool in spark streaming的更多相关文章

  1. Flume整合Spark Streaming

    Spark版本1.5.2,Flume版本:1.6 Flume agent配置文件:spool-8.51.conf agent.sources = source1 agent.channels = me ...

  2. 基于案例贯通 Spark Streaming 流计算框架的运行源码

    本期内容 : Spark Streaming+Spark SQL案例展示 基于案例贯穿Spark Streaming的运行源码 一. 案例代码阐述 : 在线动态计算电商中不同类别中最热门的商品排名,例 ...

  3. 通过案例对 spark streaming 透彻理解三板斧之三:spark streaming运行机制与架构

    本期内容: 1. Spark Streaming Job架构与运行机制 2. Spark Streaming 容错架构与运行机制 事实上时间是不存在的,是由人的感官系统感觉时间的存在而已,是一种虚幻的 ...

  4. Spark Streaming官方文档学习--上

    官方文档地址:http://spark.apache.org/docs/latest/streaming-programming-guide.html Spark Streaming是spark ap ...

  5. Apache Spark 2.2.0 中文文档 - Spark Streaming 编程指南 | ApacheCN

    Spark Streaming 编程指南 概述 一个入门示例 基础概念 依赖 初始化 StreamingContext Discretized Streams (DStreams)(离散化流) Inp ...

  6. Spark Streaming编程指南

    Overview A Quick Example Basic Concepts Linking Initializing StreamingContext Discretized Streams (D ...

  7. 通过Spark Streaming的foreachRDD把处理后的数据写入外部存储系统中

    转载自:http://blog.csdn.net/erfucun/article/details/52312682 本博文主要内容包括: 技术实现foreachRDD与foreachPartition ...

  8. Spark Streaming通过JDBC操作数据库

    本文记录了学习使用Spark Streaming通过JDBC操作数据库的过程,源数据从Kafka中读取. Kafka从0.10版本提供了一种新的消费者API,和0.8不同,因此Spark Stream ...

  9. 基于Spark Streaming + Canal + Kafka对Mysql增量数据实时进行监测分析

    Spark Streaming可以用于实时流项目的开发,实时流项目的数据源除了可以来源于日志.文件.网络端口等,常常也有这种需求,那就是实时分析处理MySQL中的增量数据.面对这种需求当然我们可以通过 ...

随机推荐

  1. windows系统bat方式启动tomcat出现java.lang.OutOfmemoryError:PermGen Space 错误

    1.问题情景: 在部署项目时,将两个应用部署到同一个tomcat下,通过startup.bat启动服务时,控制台出现出现java.lang.OutOfmemoryError:PermGen Space ...

  2. 记一次ping: unknown host错误

    虚拟机上一台主机,之前一直在用,可以通过xshell连接,但是忽然发现ping百度失败了! [root@mgt02 ~]# ping www.baidu.com ping: unknown host ...

  3. Kafka学习之(六)搭建kafka集群

    想要搭建kafka集群,必须具备zookeeper集群,关于zookeeper集群的搭建,在Kafka学习之(五)搭建kafka集群之Zookeeper集群搭建博客有说明.需要具备两台以上装有zook ...

  4. Sybase 存储过程中IF的用法

    Sybase 存储过程中IF的用法 --@i_val 为参数 or @i_val is null then begin --执行内容 end; end if;

  5. c++中的字符集与中文

    就非西欧字符而言,比如中国以及港澳台,在任何编程语言的开发中都不得不考虑字符集及其表示.在c++中,对于超过1个字节的字符,有两种方式可以表示: 1.多字节表示法:通常用于存储(空间效率考虑). 2. ...

  6. mysql 批处理文件出错后继续执行

    在升级批处理sql脚本的时候,由于各种编写的不规范.不可重复执行,我们通常希望在sql脚本出错后不中止,而是执行完成.虽然这些问题可通过编写可重复执行的mysql存储过程比如add_column/dr ...

  7. 汽车OBD接口定义

    汽车上的OBD-II接口(母):  ELM327用到的引脚: 2: SAE-J1850 PWM和SAE-1850 VPW总线(+) 4. 车身地 5. 信号地 6. CAN high (ISO 157 ...

  8. 在awk中通过system调用sql语句来说明引号的使用

    一个实际应用例子: 在awk中,通过system调用连接数据库并作select操作,select语句中where条件来自于一个文件(file)的第一个域($1). $ cat file ... ... ...

  9. linux下命令行工具gcp显示拷贝进度条

    1.环境: ubuntu16.04 Linux jello 4.4.0-89-generic #112-Ubuntu SMP Mon Jul 31 19:38:41 UTC 2017 x86_64 x ...

  10. Win7系统中如何查看当前文件被哪一个程序占用了

    https://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows ...