数据库的连接池建议放在类似settings.py的配置模块中,因为基本都是配置项,方便统一管理。

1) 连接池类#settings.py

import os
from DBUtils.PooledDB import PooledDB
from elasticsearch import Elasticsearch
import pymysql
class Config(object):
POOL = PooledDB(
creator = pymysql,
maxconnections = 20,
mincached = 3,
maxcached = 3,
host = '10.208.116.41',
port = 3306,
user = 'hadoop',
password= 'XXXXX',
database = 'hadoop_tools'
)
#elasticsearch connect
es_conn = Elasticsearch(
['10.208.116.33', '10.208.116.34', '10.208.116.35'],
sniff_timeout=60,
sniff_on_start=True
)
es_mapping = {
'properties': {
'title': {
'type': 'text',
'analyzer': 'ik_max_word',
'search_analyzer': 'ik_max_word'
}
}
} #POOL test
if __name__ == '__main__':
conn = Config.POOL.connection()
#cur = conn.cursor(pymysql.cursors.DictCursor)
cur = conn.cursor()
cur.execute("select * from hs2_status")
ret = cur.fetchall()
for i in ret:
print(i)

2)封装MySQL的操作

utils/helper.py

import pymysql
from ops.settings import Config
def connect():
conn = Config.POOL.connection()
cur = conn.cursor(pymysql.cursors.DictCursor)
return conn,cur
def connect_close(conn,cur):
conn.close()
cur.close() def fetch_all(sql,args):
conn,cur = connect()
cur.execute(sql,args)
result = cur.fetchall()
connect_close(conn,cur)
return result def fetch_one(sql,args):
conn,cur = connect()
cur.execute(sql,args)
result = cur.fetchone()
connect_close(conn,cur)
return result def insert(sql,args):
conn, cur = connect()
row = cur.execute(sql,args)
conn.commit()
connect_close(conn,cur)
return row def update(sql,args):
conn, cur = connect()
row = cur.execute(sql,args)
conn.commit()
connect_close(conn,cur)
return row def delete(sql,args):
conn, cur = connect()
row = cur.execute(sql,args)
conn.commit()
connect_close(conn,cur)
return row

##下面这段调试用,可以去掉
if __name__ == '__main__':
in_zk = 'YES'
ip = '10.208.106.159'
# row = update("update hs2_status set in_zk=%s where host_ip = %s",(in_zk,ip))
# print("row is %s" %(row)) # 获得表头
# sql = "SHOW FIELDS FROM hs2_status"
# tb_h = fetch_one(sql,()) # 执行sql
# #hs2_header = [l[0] for l in tb_h]
# # for i in tb_h:
# print("this is %s" %(tb_h))
#获取内容
# hs2_content = fetch_all(sql, ())
sql = "select * from hs2_status"
hs2_content = fetch_all(sql,())
for i in hs2_content:
print(i)

3)ES连接引用示例:

#!/usr/bin/env python
# encoding: UTF-8
from ops.settings import Config #create index,change mappings
Config.es_conn.indices.create(index='news', ignore=400)
Config.es_conn.indices.put_mapping(index='news', doc_type='politics', body=Config.es_mapping) datas = [
{
'title': '美国留给伊拉克的是个烂摊子吗',
'url': 'http://view.news.qq.com/zt2011/usa_iraq/index.htm',
'date': '2011-12-16'
},
{
'title': '公安部:各地校车将享最高路权',
'url': 'http://www.chinanews.com/gn/2011/12-16/3536077.shtml',
'date': '2011-12-16'
},
{
'title': '中韩渔警冲突调查:韩警平均每天扣1艘中国渔船',
'url': 'https://news.qq.com/a/20111216/001044.htm',
'date': '2011-12-17'
},
{
'title': '中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首',
'url': 'http://news.ifeng.com/world/detail_2011_12/16/11372558_0.shtml',
'date': '2011-12-18'
}
] #insert data
for data in datas:
Config.es_conn.index(index='news', doc_type='politics', body=data) #query data
result = Config.es_conn.search(index='news')
print(result)

  

MySQL&ES连接池的更多相关文章

  1. Node.js使用MySQL的连接池

    使用Nodejs+MySQL肯定比PHP和MySQL的组合更适合做服务器端的开发. 使用Nodejs你会从他的异步行为中获益良多.比如,提升性能,你无须在从已有的MySQL数据库迁移到其他的NoSQL ...

  2. [nodejs]解决mysql和连接池(pool)自动断开问题

    最近在做一个个人项目,数据库尝试使用了mongodb.sqlite和mysql.分享一下关于mysql的连接池用法.项目部署于appfog,项目中我使用连接池链接数据库,本地测试一切正常.上线以后,经 ...

  3. hibernate+mysql的连接池配置

    1:连接池的必知概念    首先,我们还是老套的讲讲连接池的基本概念,概念理解清楚了,我们也知道后面是怎么回事了. 以前我们程序连接数据库的时候,每一次连接数据库都要一个连接,用完后再释放.如果频繁的 ...

  4. Tomcat 下 mysql的连接池配置和使用

    最近维护的一个项目出了问题,最后分析是卡在数据库连接池上,然后就做了些学习. 先把我自己的方法写出来,再说下网上其他的没有成功的方法. 1.首先当然是先把mysql的jar包放在lib目录下,tonc ...

  5. Python下Mysql数据连接池——单例

    # coding:utf-8 import threading import pymysql from DBUtils.PooledDB import PooledDB from app.common ...

  6. nodejs mysql 创建连接池

    用Nodejs连接MySQL 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javas ...

  7. MySQL 使用连接池封装pymysql

    备注:1,记得先修改连接的数据库哦,(用navicat更方便一点):2,分开两个py文件写入,运行sqlhelper.py文件 一.在utils.py中写 import pymysqlfrom DBU ...

  8. 码海拾遗:基于MySQL Connector/C++的MySQL操作(连接池)

    1.MySQL安装及简单设置 (1)安装:在OSX系统下,可以使用万能的“brew install”命令来进行安装:brew isntall mysql(默认安装最新版的MySQL) (2)启动:br ...

  9. mysql HikariCP连接池配置

    #连接池配置 #最小空闲连接,默认值10,小于0或大于maximum-pool-size,都会重置为maximum-pool-size spring.datasource.hikari.minimum ...

随机推荐

  1. 题解 P6271 [湖北省队互测2014]一个人的数论

    通过这道题学了伯努利数,写篇题解推一下 题目 先推一下式子 \[\sum_{i=1}^ni^d[gcd(i,n)=1] \] \[\sum_{i=1}^{n}i^d\sum_{k|i}\sum_{k| ...

  2. SSM自学笔记(五)

    10.MyBatis入门操作 1.MyBatis的简介 1.1 原始jdbc操作(查询数据) 1.2 原始jdbc操作(插入数据) ##### 1.3 **原始**jdbc操作的分析 原始jdbc开发 ...

  3. 痞子衡嵌入式:其实i.MXRT下改造FlexSPI driver同样支持AHB方式去写入NOR Flash

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT下改造FlexSPI driver以AHB方式去写入NOR Flash. 痞子衡前段时间写过一篇 <串行NAND Fl ...

  4. 被MySQL慢日志查询搞废了?3分钟教你快速定位慢查询问题!

    一条慢查询会造成什么后果?刚开始使用MySQL的开发.初级DBA 以为就是简单的查询变慢些,体验稍微有一丢丢影响,殊不知,慢查询的破坏力远不止如此.业务高峰期,这头SQL还没处理完,大量新的查询请求堆 ...

  5. linux下C编程初篇

    对于程序设计员来说,makefile是我们绕不过去的一个坎.可能对于习惯Visual C++的用户来说,是否会编写makefile无所谓.毕竟工具本身已经帮我们做好了全部的编译流程.但是在Linux上 ...

  6. 一招解决下载或下拉GitHub项目速度太慢的问题

    相信很多朋友都有过这样的体验,就是从Github上下载或clone别人的项目时特别慢,甚至还会出现链接意外终止的情况,那么今天就来给大家分享一个提速的方法,步骤也非常简单,亲测有效! 首先进入你的目标 ...

  7. JAVA简单精确计算工具类

    1 public class ArithUtil { 2 3 // 默认除法运算精度 4 private static final int DEF_DIV_SCALE = 10; 5 6 privat ...

  8. Docker容器 关于镜像构建的安全问题

    写在前面 确保容器中服务与应用安全是容器化演进的关键点.容器安全涉及到应用开发与维护的整个生命周期,本文主要从镜像构建的视角来看docker容器的一些安全问题及应对措施. 一.权限管理 1.避免以容器 ...

  9. freeswitch简介

    freeswitch简介 freeswitch是开源的,免费的. freeswitch是一款非常好用的电话软交换框架,支持跨平台,扩展性良好,配置灵活. freeswitch可以在很多平台上运行,包括 ...

  10. asp语言中if判断语句的求助

    If a < 5 Then   Response.Redirect("1.asp")ElseIf a > 5 And a < 8 Then   Response. ...