pyhs(0.2.4):python2.7+handlersocket

Posted qq_784583650

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyhs(0.2.4):python2.7+handlersocket相关的知识,希望对你有一定的参考价值。

1.pyhs安装

#pip安装
pip install python-handler-socket
#验证是否成功安装
python
>>>from pyhs import __version__
>>>print __version__
>>>0.2.4

安装过程可参考https://pypi.python.org/pypi/python-handler-socket
注:实践之前确认在mysql中已经安装并开启了Handlersocket线程,可参考percona mysql5.5 + Handlersocket安装与实践

2.pyhs实践
pyhs分为两个部分,high level和low level,一般使用high level,可执行简单的CRUD的MySQL操作。low level则提供更细致的MySQL操作。

  • 首先建立好测试表格:
# 登录MySQL中,在test数据库中创建表格t
CREATE TABLE IF NOT EXISTS `test`.`t` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `a` varchar(10) NOT NULL,
  `b` varchar(10) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `a_b` (`a`,`b`)
) ENGINE=InnoDB;
  • high level:
# 进入python命令行界面
In [1]: from pyhs import Manager
# 建立本地连接
In [2]: hs = Manager()
# 如果还有其他的主机存在,可以和多个主机建立连接,当操作在一个连接失败时,会继续在其他连接上操作
# When several hosts are available, client code will try to use both of them
# to balance the load and will retry requests in case of failure on one of them.
# read_servers = [('inet', '1.1.1.1', 9998), ('inet', '2.2.2.2', 9998)]
# write_servers = [[('inet', '1.1.1.1', 9999), ('inet', '2.2.2.2', 9999)]]
# hs = Manager(read_servers, write_servers)

# 1.insert插入一条数据
# SQL语句原型:insert into test.t values(1, 'a1', 'b1');
# Note Look how the data is passed - it is a list of field-value pairs. Make sure that all values are strings.
# 注意:数据和项成对出现,**插入的数据一律为字符串形式**
# insert(db, table, fields, index_name=None)
In [3]: hs.insert('test', 't', [('id', '1'), ('a', 'a1'), ('b', 'b1')])
Out[3]: True

# 2.get键值条件查找数据
# SQL语句原型:select * from test.t where id = 1;
# Note get() is a wrapper over find(). It only fetches one row searched for by a single comparison value and uses only primary index for this. For more complex operations please use find. Make sure that the first field in the fields list is the one that is searched by and that the list is ordered in the same way fields are present in the index.
find and get return list of field-value pairs as result.
# get()功能包含在find()函数中,只能得到用键查找数据的一行,更复杂的查找需要用到find()
# get()在参数要求:用于查找的键id需要放到第一个,得到的结果会按照['id', 'a', 'b']给出
# get():**其实在实践过程中,键id随便放,因为默认进行键值查找,打开的索引是PRIMARY**
# get(db, table, fields, value)
In [4]: data = hs.get('test', 't', ['id', 'a', 'b'], '1')
# 查看数据
In [5]: data
Out[5]: [('id', '1'), ('a', 'a1'), ('b', 'b1')]
In [6]: hs.get('test', 't', ['a', 'b', 'id'], '1')
Out[6]: [('a', 'a1'), ('b', 'b1'), ('id', '1')]

# 3.find键值范围查找数据
# SQL语句原型:select * from test.t where id > 0;
# 注:发现只能对键值进行查找,index_name = "PRIMARY",没有弄明白怎么设置,非键值查找没有成功
# 查看函数说明
hs.find?
# 查看函数源码
hs.find??
# 新增一条数据,用于演示
hs.insert('test', 't', [('id', '2'), ('a', 'a1'), ('b', 'b2'])
# find(db, table, operation, fields, values, index_name=None, limit=0, offset=0)
# 查看FIND_OPERATIONS,只能用这里面的operation
In [7]: pyhs.sockets.HandlerSocket.FIND_OPERATIONS
Out[7]: ('=', '>', '>=', '<', '<=')
# find不会显示所有的结果,自己限定结果数目,limit = 2
In [8]: hs.find('test', 't', '>', ['id', 'a', 'b'], ['0'], limit = 2)
Out[8]: 
[[('id', '1'), ('a', 'a1'), ('b', 'b1')],
 [('id', '2'), ('a', 'a1'), ('b', 'b2')]]

# 4.update更新操作
# SQL语句原型:update test.t set id = 2, a = 'a2', b = 'b2' where id =2;
# 注:键值更新,非键值更新未成功
# update(db, table, operation, fields, values, update_values, index_name=None, limit=0, offset=0, return_original=False)
# 返回值TRUE或者是成功更新的行数
In [9]: hs.update('test', 't', '=', ['id', 'a', 'b'], ['2'], ['2', 'a2', 'b2'])
Out[9]: 1

# 5.incr整型项增操作
# incr(db, table, operation, fields, values, step=['1'], index_name=None, limit=0, offset=0, return_original=False)
In [10]: hs.incr('test', 't', '=', ['id'], ['2'], step=['2'], return_original=True)
Out[10]: [[('id', '2')]]
# 查看MySQL中test.t,可以发现id=2的项,增加step2

# 6.decr整型项减操作
# decr(db, table, operation, fields, values, step=['1'], index_name=None, limit=0, offset=0, return_original=False)
In [11]: hs.decr('test', 't', '=', ['id'], ['4'], step=['2'], return_original=True)
Out[11]: [[('id', '4')]
# 查看MySQL中test.t,可以发现id=4的项,减少step2

# 7.delete删除操作
# SQL语句原型:delete from test.t where id = 2;
# delete(db, table, operation, fields, values, index_name=None, limit=0, offset=0, return_original=False)
In [12]: hs.delete('test', 't', '=', ['id', 'a', 'b'], ['2'], return_original=True)
Out[12]: [[('id', '2'), ('a', 'a2'), ('b', 'b2')]]
# 显示已经删除的数据

# 8.purge清理连接和索引缓存
hs.purge()
# Purges all read and write connections. All requests after that operation will open new connections, index caches will be cleaned too.

# 9.read_socket,write_socket
  • low level:
    建立表格以便测试
mysql> create table student(
    -> id int,
    -> name char(8),
    -> age int,
    -> primary key(id));
Query OK, 0 rows affected (0.06 sec)

mysql> describe student;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   | PRI | 0       |       |
| name  | char(8) | YES  |     | NULL    |       |
| age   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into student values(1, 'Bob', 21);
Query OK, 1 row affected (0.03 sec)
In [1]: from pyhs.sockets import ReadSocket

In [2]: hs = ReadSocket([('inet', '127.0.0.1', 9998)])

In [3]: hs.get_index_id('test', 'student', ['id'])
Out[3]: 0

In [4]: hs.get_index_id('test', 't', ['id'])
Out[4]: 1

In [5]: hs.get_index_id('test', 'student', ['id', 'name', 'age'])
Out[5]: 2

In [6]: hs.find(2, '=', ['1'])
Out[6]: [('1', 'Bob', '21')]

In [7]: hs.get_index_id('test', 't', ['id', 'a', 'b'])
Out[7]: 3

In [8]: hs.find(3, '=', ['2'])
Out[8]: [('2', 'a2', 'b2')]

In [9]: hs.find(3, '=', ['1'])
Out[9]: [('1', 'a1', 'b1')]
# 可以看出get_index_id将为需要查询的表格格式映射一个id,之后可以按照这个id超找匹配的键值,如89

想要了解更多可查看参考文档以及下载源码查看
1.源码python-handler-socket-0.2.4.tar.gz (md5)
2.pyhs0.2.4参考文档

以上是关于pyhs(0.2.4):python2.7+handlersocket的主要内容,如果未能解决你的问题,请参考以下文章

pyhs(0.2.4) + MySQLdb性能比较

HiveAccessControlException 权限被拒绝。 [关于 pyhs2]

centos 安装 py pyhs2

blog开发环境环境设置

python2.6 升级到 python2.7

升级Python2.7