MariaDB NoSQL with HandlerSocket

MariaDB NoSQL with HandlerSocket

環境:
CentOS7.1 x64
mariadb-10.1.13

安裝配置HandlerSocket

INSTALL SONAME 'handlersocket'python


[mysqld]mysql

handlersocket_address = "127.0.0.1"git

handlersocket_port = "9998"github

 

handlersocket_port_wr = "9999"sql


root@jlive:~#/etc/init.d/mysql restart數據庫

 

Restarting mysql (via systemctl):                          [  肯定  ]ruby

root@jlive:~#netstat -tunlp|grep mysqlsocket

tcp            0 127.0.0.1:9998          0.0.0.0:*               LISTEN      24591/mysqld        tcp

tcp            0 127.0.0.1:9999          0.0.0.0:*               LISTEN      24591/mysqld        ui

 

tcp6           0 :::3306                 :::*                    LISTEN      24591/mysql

(jlive)[isfdb]>SHOW PROCESSLIST;

+----+-------------+---------------------+---------------+---------+------+-------------------------------------------+------------------+----------+

| Id | User        | Host                | db            | Command | Time | State                                     | Info             | Progress |

+----+-------------+---------------------+---------------+---------+------+-------------------------------------------+------------------+----------+

1 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

2 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

3 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

4 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

5 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

6 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

7 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

8 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

9 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

| 10 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

| 11 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

| 12 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

| 13 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

| 14 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

| 15 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

| 16 | system user | connecting host     | NULL          | Connect | NULL | handlersocket: mode=rd, 0 conns, 0 active | NULL               0.000 |

| 17 | system user | connecting host     | handlersocket | Connect | NULL | handlersocket: mode=wr, 0 conns, 0 active | NULL               0.000 |

| 21 | zabbix      | localhost           | zabbix        | Sleep     4 |                                           | NULL               0.000 |

| 22 | jlive       | 192.168.130.1:63960 | isfdb         | Query     0 | init                                      | SHOW PROCESSLIST |    0.000 |

| 23 | zabbix      | localhost           | zabbix        | Sleep   |   12 |                                           | NULL               0.000 |

| 24 | zabbix      | localhost           | zabbix        | Sleep   |   12 |                                           | NULL               0.000 |

| 25 | zabbix      | localhost           | zabbix        | Sleep     2 |                                           | NULL               0.000 |

+----+-------------+---------------------+---------------+---------+------+-------------------------------------------+------------------+----------+

 

22 rows in set (0.00 sec)


[mysqld]

# HandlerSocket Read Threads: 

handlersocket_threads = 16

# HandlerSocket Write Threads: 

handlersocket_threads_wr = 1 


handlersocket_plain_secret = 'readSocketPassword' 

handlersocket_plain_secret_wr = 'writeSocketPassword' 


提示:

handlersocket默認狀況下會啓動16個讀線程和1個寫線程,能夠經過上面參數修改線程數,讀寫的最大線程數最大值爲3000,官方推薦寫線程數設爲1,讀線程數爲CPU核數的兩倍

還能夠設置爲讀寫handlersocket設置不一樣的明文密碼



libhsclient

https://github.com/DeNA/HandlerSocket-Plugin-for-MySQL

git clone https://github.com/DeNA/HandlerSocket-Plugin-for-MySQL.git

cd HandlerSocket-Plugin-for-MySQL/libhsclient

make

mkdir -p /usr/local/include/handlersocket

 

install -m 644 *.hpp /usr/local/include/handlersocket

 

install -m 644 libhsclient.a /usr/local/lib

 

ln -s /usr/local/include/handlersocket/* /usr/local/include/



HandlerSocket perl client庫

 

1.安裝perl handlersocket庫

cd HandlerSocket-Plugin-for-MySQL/perl-Net-HandlerSocket/

 

perl Makefile.PL

make -j4 && make install

2.建立示例數據

CREATE DATABASE IF NOT EXISTS test;

USE test;

DROP TABLE IF EXISTS hs_test;

CREATE TABLE hs_test (

  id SERIAL PRIMARY KEY,

  givenname varchar(64),

  surname varchar(64)

);

INSERT INTO hs_test VALUES

  (1,"William","Hartnell"), 

  (2,"Patrick","Troughton"),

  (3,"John","Pertwee"), 

  (4,"Tom","Baker"),

  (5,"Peter","Davison"), 

 

  (6,"Colin","Baker"); 

3.建立示例perl腳本

#!/usr/bin/perl

use strict;

use warnings;

use Net::HandlerSocket;

my $read_args = { host => 'localhost', port => 9998 };

my $hs = new Net::HandlerSocket($read_args);


my $res = $hs->open_index(0, 'test', 'hs_test', 'PRIMARY', 'id,givenname,surname');

        die $hs->get_error() if $res != 0;


my $pk = 1;


$res = $hs->execute_single(0, '=', [ "$pk" ], 10, 0); 

        die $hs->get_error() if $res->[0] != 0;

shift(@$res);


while ( $res->[0] ) { 

        printf("%s\t%s\t%s\n",$res->[0],$res->[1],$res->[2]);

        $pk++;

        $res = $hs->execute_single(0, '=', [ "$pk" ], 20, 0); 

                die $hs->get_error() if $res->[0] != 0;

        shift(@$res);

 

}



root@jlive:~#perl hs_read_test.pl 

1 William Hartnell

2 Patrick Troughton

3 John Pertwee

4 Tom Baker

5 Peter Davison

 

6 Colin Baker


HandlerSocket python client庫

1.安裝python handlersocket庫

https://bitbucket.org/excieve/pyhs

yum -y install mercurial

hg clone http://bitbucket.org/excieve/pyhs

cd pyhs

 

sed -i '4a ext_errors = (CCompilerError, DistutilsExecError,DistutilsPlatformError)' setup.py 

python setup.py install
注意:ext_errors至今仍然有個未定義的bug, https://bitbucket.org/excieve/pyhs/ issue/11/setuppy-error-ext_errors-not-defined

2.建立示例python腳本

#!/bin/env python

from pyhs import Manager


hs = Manager()

data = hs.get('test', 'hs_test', ['id', 'givenname', 'surname'], '5')


print dict(data)


from pyhs.sockets import ReadSocket

hsr = ReadSocket([('inet', '127.0.0.1', 9998)])

r_id = hsr.get_index_id('test', 'hs_test', ['id', 'givenname', 'surname'])


print hsr.find(r_id, '=', ['5'])

print hsr.find(r_id, '=', ['6'])

print hsr.find(r_id, '>=', ['1'],20)


root@jlive:~#python py_hs_read.py 

{'givenname': 'Peter', 'surname': 'Davison', 'id': '5'}

[('5', 'Peter', 'Davison')]

[('6', 'Colin', 'Baker')]

[('1', 'William', 'Hartnell'), ('2', 'Patrick', 'Troughton'), ('3', 'John', 'Pertwee'), ('4', 'Tom', 'Baker'), ('5', 'Peter', 'Davison'), ('6', 'Colin', 'Baker')]


HandlerSocket ruby client庫

1.安裝ruby handlersocket庫

 

gem install handlersocket

2.建立示例ruby腳本

#!/bin/env ruby

require 'rubygems'

require 'handlersocket'


hs = HandlerSocket.new(:host => '127.0.0.1',:port => '9998'

hs.open_index(0,'test','hs_test','PRIMARY','id,givenname,surname')


p hs.execute_single(0,'=',[1])

p hs.execute_single(0,'>',[1],2,2)

 

p hs.execute_single(0,'>=',[1],20)


提示:也能夠直接經過交互式的irb來執行上面語句

root@jlive:~#ruby rb_hs_read.rb 

[0, [["1", "William", "Hartnell"]]]

[0, [["4", "Tom", "Baker"], ["5", "Peter", "Davison"]]]

 

[0, [["1", "William", "Hartnell"], ["2", "Patrick", "Troughton"], ["3", "John", "Pertwee"], ["4", "Tom", "Baker"], ["5", "Peter", "Davison"], ["6", "Colin", "Baker"]]]



HandlerSocket Telnet


root@jlive:~#telnet 127.0.0.1 9998

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

P       0 test hs_test PRIMARY id,givenname,surname

0 1

0 = 1 1

0 3 1 William Hartnell

0 > 1 2 2

0 3 3 John Pertwee 4 Tom Baker

0 >= 1 1 20

0 3 1 William Hartnell 2 Patrick Troughton 3 John Pertwee 4 Tom Baker 5 Peter Davison 6 Colin Baker

^]

 

telnet> Connection closed.

注意:分隔符爲Tab

第一行是定義到數據庫的信道鏈接

      0 test hs_test PRIMARY id,givenname,surname

P關鍵字,第二列爲確認信道,這裏設爲0,第三列爲數據庫,第三列爲數據庫中的表,第五列爲search key,第六列爲查詢字段

0 = 1 1

0是信道,第二列爲操做符(>,>=,<,<=,=),第三列是索引,只能<=第一行中定義的search key數量,由於只定義了一個search key因此這裏設爲1,第四列爲查詢第幾字段,1表示id,2表givenname,3表示surname

0 3 1 William Hartnell

0狀態返回碼,0爲成功,第二列爲查詢字段數量,定義鏈接時定義了3個查詢字段,這裏返回3,以後的幾列就是id,givenname,surname的值

0 > 1 2 2

查詢第2字段(givenname)比PRIMARY大的記錄,第五字段限制返回記錄的數量,這裏爲2最多返回2條記錄,沒有第五列則只返回1行記錄

相關文章
相關標籤/搜索