MySQL&ES连接池
数据库的连接池建议放在类似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连接池的更多相关文章
- Node.js使用MySQL的连接池
使用Nodejs+MySQL肯定比PHP和MySQL的组合更适合做服务器端的开发. 使用Nodejs你会从他的异步行为中获益良多.比如,提升性能,你无须在从已有的MySQL数据库迁移到其他的NoSQL ...
- [nodejs]解决mysql和连接池(pool)自动断开问题
最近在做一个个人项目,数据库尝试使用了mongodb.sqlite和mysql.分享一下关于mysql的连接池用法.项目部署于appfog,项目中我使用连接池链接数据库,本地测试一切正常.上线以后,经 ...
- hibernate+mysql的连接池配置
1:连接池的必知概念 首先,我们还是老套的讲讲连接池的基本概念,概念理解清楚了,我们也知道后面是怎么回事了. 以前我们程序连接数据库的时候,每一次连接数据库都要一个连接,用完后再释放.如果频繁的 ...
- Tomcat 下 mysql的连接池配置和使用
最近维护的一个项目出了问题,最后分析是卡在数据库连接池上,然后就做了些学习. 先把我自己的方法写出来,再说下网上其他的没有成功的方法. 1.首先当然是先把mysql的jar包放在lib目录下,tonc ...
- Python下Mysql数据连接池——单例
# coding:utf-8 import threading import pymysql from DBUtils.PooledDB import PooledDB from app.common ...
- nodejs mysql 创建连接池
用Nodejs连接MySQL 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javas ...
- MySQL 使用连接池封装pymysql
备注:1,记得先修改连接的数据库哦,(用navicat更方便一点):2,分开两个py文件写入,运行sqlhelper.py文件 一.在utils.py中写 import pymysqlfrom DBU ...
- 码海拾遗:基于MySQL Connector/C++的MySQL操作(连接池)
1.MySQL安装及简单设置 (1)安装:在OSX系统下,可以使用万能的“brew install”命令来进行安装:brew isntall mysql(默认安装最新版的MySQL) (2)启动:br ...
- mysql HikariCP连接池配置
#连接池配置 #最小空闲连接,默认值10,小于0或大于maximum-pool-size,都会重置为maximum-pool-size spring.datasource.hikari.minimum ...
随机推荐
- uwp Button的动态效果
你应该覆盖Button样式 <Page.Resources> <Style TargetType="Button" x:Key="CustomButto ...
- C语言 Ubuntu系统 UTF-8 文字处理
关于UTF-8的规则:https://baike.baidu.com/item/UTF-8/481798?fr=aladdin 使用windows系统下的Ubuntu子系统,实现C语言对UTF-8编码 ...
- COM笔记-引用计数
参考网站:https://www.cnblogs.com/fangyukuan/archive/2010/06/06/1752621.html com组件将维护一个称作是引用计数的数值.当客户从组件取 ...
- spring boot , spring security 安全的认证
pom 文件 ------------------------------------------------------------------- <dependencies> < ...
- Math.round() 函数返回一个数字四舍五入后最接近的整数。
语法: Math.round(x); 参数:x 返回值:给定数字的值四舍五入到最接近的整数 描述: 如果参数的小数部分大于 0.5,则舍入到相邻的绝对值更大的整数. 如果参数的小数部分小于 0.5,则 ...
- LeetCoded第739题题解--每日温度
每日温度 请根据每日 气温 列表,重新生成一个列表.对应位置的输出为:要想观测到更高的气温,至少需要等待的天数.如果气温在这之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temper ...
- 2014 12 27 bestcoder 第一题
水的不行不行的一道题 也是自己做的第一道题 纪念下 1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h ...
- Ajax 与 Struts 1
Xml配置 <action path="/product/product/validateCurrencyDecimalSupport" type="com.neu ...
- playwright-python 截图、录制视频、录制接口(二)
截图 fullPage为True时,截取全屏,默认为False clip截取特定部分的图片,{"x": float, "y": float, "wid ...
- 多线程-synchorized
synchorized锁升级过程: synchorized锁升级过程中只能升级不能降级,起初是JDK早期(1.5之前),是重量级锁,是找操作系统申请OS锁.所谓重量级锁是说获取锁和释放锁都需要经过操作 ...