Run Zookeeper x python (kazoo) on Mac OS X

What is zookeeper?

The guidance of the head family is here. I understand that it is easy to implement distributed processing of batches, which is difficult when assembled properly.

Installation

$ brew install zookeeper

Verification

$ ls /usr/local/etc/zookeeper
defaults         log4j.properties zoo.cfg          zoo_sample.cfg

Run in standalone mode

Even if you compare the setting file that is placed by default with Tutorial, it is almost the same, so start it as it is Try.

$ cat /usr/local/etc/zookeeper/zoo.cfg | egrep -v "#"
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/var/run/zookeeper/data
clientPort=2181
$ which zkServer
/usr/local/bin/zkServer

$ sudo zkServer start
ZooKeeper JMX enabled by default
Using config: /usr/local/etc/zookeeper/zoo.cfg
Starting zookeeper ... STARTED

The log is output below

$ tail -f /usr/local/var/log/zookeeper/zookeeper.log
2017-01-09 06:40:05 QuorumPeerMain [WARN] Either no config or no quorum defined in config, running  in standalone mode

Something is out. Quorum refers to the destination server, a warning that it does not exist. It's a trial stand-alone here, so it doesn't matter if it's out.

Connection test

You can connect to the port specified as clientPort in the configuration file.

$ cat /usr/local/etc/zookeeper/zoo.cfg | grep clientPort
clientPort=2181

Since port 2181 is specified, connect to it.

$ which zkCli
/usr/local/bin/zkCli

$ sudo zkCli -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
Welcome to ZooKeeper!
JLine support is enabled

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181(CONNECTED) 0]

Check available commands

[zk: 127.0.0.1:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
	stat path [watch]
	set path data [version]
	ls path [watch]
	delquota [-n|-b] path
	ls2 path [watch]
	setAcl path acl
	setquota -n|-b val path
	history
	redo cmdno
	printwatches on|off
	delete path [version]
	sync path
	listquota path
	rmr path
	get path [watch]
	create [-s] [-e] path data acl
	addauth scheme auth
	quit
	getAcl path
	close
	connect host:port

Create a znode

Follow the tutorial (http://oss.infoscience.co.jp/hadoop/zookeeper/docs/r3.3.1/zookeeperStarted.html). znode refers to each node on the ZooKeeper tree.

#Directory check
[zk: 127.0.0.1:2181(CONNECTED) 0] ls /
[zookeeper]

#node(znode)Create
[zk: 127.0.0.1:2181(CONNECTED) 1] create /test_node my_test_data
Created /test_node

#Creation confirmation
[zk: 127.0.0.1:2181(CONNECTED) 2] ls /
[test_node, zookeeper]

#Make sure the data you created is associated with the node
[zk: 127.0.0.1:2181(CONNECTED) 3] get /test_node
my_test_data
cZxid = 0x8
ctime = Mon Jan 09 07:00:17 JST 2017
mZxid = 0x8
mtime = Mon Jan 09 07:00:17 JST 2017
pZxid = 0x8
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 0

#Try changing the associated data
[zk: 127.0.0.1:2181(CONNECTED) 4] set /test_node replaced_my_test_data
cZxid = 0x8
ctime = Mon Jan 09 07:00:17 JST 2017
mZxid = 0x9
mtime = Mon Jan 09 07:03:21 JST 2017
pZxid = 0x8
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 21
numChildren = 0

#Verification
[zk: 127.0.0.1:2181(CONNECTED) 5] get /test_node
replaced_my_test_data # <-changed
cZxid = 0x8
ctime = Mon Jan 09 07:00:17 JST 2017
mZxid = 0x9
mtime = Mon Jan 09 07:03:21 JST 2017
pZxid = 0x8
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 21
numChildren = 0

#Delete
[zk: 127.0.0.1:2181(CONNECTED) 6] delete /test_node

#Verification
[zk: 127.0.0.1:2181(CONNECTED) 7] ls /
[zookeeper]

Try using it in python

python x zookeeper uses a library called kazoo. I will write various things according to the following documents. kazoo

installation of kazoo

$ pip install kazoo

Roughly try

hello_kazoo.py


# coding:utf-8
from kazoo.client import KazooClient
from kazoo.client import KazooState
from kazoo.client import KeeperState
from kazoo.handlers.gevent import SequentialGeventHandler
import logging
logging.basicConfig()

#znode root
root = '/root'

#zookeeper client
zk = KazooClient(hosts='127.0.0.1:2181', read_only=True, handler=SequentialGeventHandler())

#Start in asynchronous mode
event = zk.start_async()
event.wait(timeout=3)

#Suspend processing if unable to connect to zookeeper server
if not zk.connected:
    zk.stop()
    raise Exception("Unable to connect.")

def listener(state):
    '''
Listener when changing State
    '''
    print('current state is ' + state)

zk.add_listener(listener)

@zk.add_listener
def watch_for_ro(state):
    if state == KazooState.CONNECTED:
        if zk.client_state == KeeperState.CONNECTED_RO:
            print('state is read_only')
        else:
            print('state is writable')

def print_status(znode):
    '''
Get node status
    '''
    print('#####[DEBUG]#####')
    #Check version and registration data
    data, stat = zk.get(znode)
    print('Version: %s, data: %s' % (stat.version, data.decode('utf-8')))
    #Get a list of root child nodes
    children = zk.get_children(root)
    print("There are %s children with names %s" % (len(children), children))

def doAsync(async_obj):
    '''
Asynchronous callback function(There is no particular meaning in the processing content)
    '''
    znodes = async_obj.get()
    try:
        children = async_obj.get()
        #Output the names of all child nodes
        print('#####[print child znodes]#####')
        for child in children:
            print(child)
    except (ConnectionLossException, NoAuthException):
        print("ERROR!!!")
        sys.exit(1)

if __name__ == '__main__':
    #Transaction start
    tx = zk.transaction()
    ##Check the basic usage
    #Path generation
    zk.ensure_path(root)
    #Create if znode has not been created
    znode = root + '/sample_znode'
    if zk.exists(znode) is None:
        zk.create(znode, b'sample_data')
    print_status(znode)
    #Data update
    zk.set(znode, b'updated_data')
    print_status(znode)
    #Add child node
    znode2 = root + '/sample_znode2'
    if zk.exists(znode2) is None:
        zk.create(znode2, b'sample_data2')
    print_status(znode2)
    ##Asynchronous processing is used like this
    async_obj = zk.get_children_async(root)
    async_obj.rawlink(doAsync)
    #Delete node
    zk.delete(root, recursive=True)
    #commit
    results = tx.commit()
    print('#####[Result]#####')
    print(results)

Execution result

$ python hello_kazoo.py
#####[DEBUG]#####
Version: 0, data: sample_data
There are 1 children with names ['sample_znode']
#####[DEBUG]#####
Version: 1, data: updated_data
There are 1 children with names ['sample_znode']
#####[DEBUG]#####
Version: 0, data: sample_data2
There are 2 children with names ['sample_znode', 'sample_znode2']
#####[print child znodes]#####
sample_znode
sample_znode2
#####[Result]#####
[]
$ sudo zkCli -server 127.0.0.1:2181

You can check various movements by connecting to the ZooKeeper server with and writing the code little by little while checking.

Recommended Posts

Run Zookeeper x python (kazoo) on Mac OS X
Run NASA CEA on Mac OS X
Shpinx (Python documentation builder) on Mac OS X
Run Tensorflow 2.x on Python 3.7
Memo on Mac OS X
Build a Python development environment on Mac OS X
Install Sphinx on Mac OS X
Install mitmproxy on Mac OS X
Install pgmagick on Mac OS X 10.9
Using multiple versions of Python on Mac OS X (2) Usage
Using NAOqi 2.4.2 Python SDK on Mac OS X El Capitan
python on mac
Installed aws-cli On Mac OS X Lion
How to erase Python 2.x on Mac.
Using multiple versions of Python on Mac OS X (1) Multiple Ver installation
Continuation ・ Notes on preparing the Python development environment on Mac OS X
Install lp_solve on Mac OS X and call it with python.
Preparing to run ImageMagick + im4java on Mac OS
Install Python on Mac
Install Python 3 on Mac
Put Python 2.7.x on Mac OSX 10.15.5 with pyenv
Run OpenMVG on Mac
Install Python 3.4 on Mac
Run Qiita API v2 Python wrapper in Python3 environment (Mac OS X 10.11 (El Capitan))
Preparing to use aws cli on Mac OS X
Building an environment for "Tello_Video" on Mac OS X
Build a machine learning Python environment on Mac OS
Try using E-Cell 4 on Windows 7 or Mac OS X
mac OS X 10.15.x pyenv Python If you can't install
Install PyQt5 with homebrew on Mac OS X Marvericks (10.9.2)
Create a Python development environment on OS X Lion
pangolin x python x mac os build failed memorandum unsolved
Install pygame on python3.4 on mac
Put Python 3.x on Ubuntu
Handling of python on mac
Update python on Mac to 3.7-> 3.8
Install pandas 0.14 on python3.4 [on Mac]
Notes on installing Python on Mac
Run Python CGI on CORESERVER
Run unix command on python
When import tkinter is not possible on Mac OS X 10.11.3 (El Capitan) + pyenv + Python 3.5.1.
Steps to use the AWS command line interface (Python / awscli) on Mac OS X
Word Count with Apache Spark and python (Mac OS X)
Test Python with Miniconda on OS X and Linux with travis-ci
How to install Theano on Mac OS X with homebrew
Note: When Python is run on ImageJ Fiji, java.lang.IllegalArgumentException appears and its solution (mac OS)
Building a Python environment on Mac
Install Python 3.8 on Ubuntu 18.04 (OS standard)
Install Python 3.8 on Ubuntu 20.04 (OS standard)
Create a Python environment on Mac (2017/4)
Python environment construction memo on Mac
Install Python 3.7 Anaconda on MAC, but Python 2
Install Scipy on Mac OS Sierra
Install python3 on Mac (El Capitan)
Run TensorFlow Docker Image on Python3
Environment construction of python3.8 on mac
Install Python 3.9 on Ubuntu 20.04 (OS standard?)
If Python 3.5.0 installation fails on Mac
Install Python 2.7 on Ubuntu 20.04 (OS standard?)
Steps to install python3 on mac
Call C / C ++ from Python on Mac