syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
In Aerospike how I do CRUD with Python?

The acronym CRUD describes the verbs: Create, Retrieve, Update, and Delete.

Many systems allow me to do CRUD.

For this demo I install Aerospike on my laptop:

http://www.syntax.us/posts/aerospike

I check that Aerospike is up:
dan@hp /tmp $
dan@hp /tmp $ cd
dan@hp ~ $ cd aero1
dan@hp ~/aero1 $
dan@hp ~/aero1 $ bin/aerospike status
info: process running
dan@hp ~/aero1 $
dan@hp ~/aero1 $
I ensure that I have Aerospike Python Client:
dan@hp ~/aero1 $ 
dan@hp ~/aero1 $ cd
dan@hp ~ $ 
dan@hp ~ $ 
dan@hp ~ $ sudo apt-get install liblua5.1-dev libssl-dev
 ... snip ...
dan@hp ~ $ 
dan@hp ~ $ 
dan@hp ~ $ which python
/home/dan/anaconda/bin/python
dan@hp ~ $ 
dan@hp ~ $ which pip
/home/dan/anaconda/bin/pip
dan@hp ~ $ 
dan@hp ~ $ 
dan@hp ~ $ pip install aerospike
Requirement already satisfied ...
Cleaning up...
dan@hp ~ $ 
dan@hp ~ $ 
I check that I can python-import aerospike:
dan@hp ~ $
dan@hp ~ $
dan@hp ~ $ python
Python 2.7.9 |Anaconda 2.1.0 (64-bit)| (default, Dec 12 2014, 14:52:24)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>>
>>> import aerospike
>>>
>>>
I connect to asd daemon:
dan@hp ~ $ 
dan@hp ~ $ python
Python 2.7.9 |Anaconda 2.1.0 (64-bit)| (default, Dec 12 2014, 14:52:24) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> 
>>> import aerospike
>>> import sys
>>> import os.path
>>> 
>>> mypath = os.path.dirname('__file__')
>>> 
>>> config = {'hosts': [('127.0.0.1',3000)],'lua': {'user_path': mypath }}
>>> 
>>> client = aerospike.client(config).connect()
>>> 
>>> 
The C in CRUD means Create.

I try to create a bin inside a record inside a set which is inside 'test' namespace.

Remember:
Aerospike has Namespaces has Sets has Records has bins.
Namespace > Set > Record > bin
>>> mynamspace = 'test'
>>> myset      = 'dogs'
>>> myrecname  = 'Fred'
>>> myrecpk    = 100
>>> foodbin    = 'Fred likes catfood'
>>> agebin     = 4
>>>
>>> myrecord   = {'myrecname': myrecname, 'myrecpk': myrecpk, 'foodbin': foodbin, 'agebin': agebin}
>>>
>>> keytuple     = (mynamspace,myset,myrecpk)
>>>
>>> client.put(keytuple, myrecord)
0L
>>>
>>>
The R in CRUD means Retrieve.

Just do it:
>>>
>>> (apk, metadata, arecord) = client.get(keytuple)
>>>
>>> apk
('test', 'dogs', 100, bytearray(b'\xd7\xd6\xdd\xb1}\xfd\xc4\xd7\xfd\xbfs\xae\xc2\xce\xc8y\x08\xa9\x1aH'))
>>>
>>> metadata
{'gen': 1, 'ttl': 2591654}
>>>
>>> arecord
{'myrecpk': 100, 'foodbin': u'Fred likes catfood', 'myrecname': u'Fred', 'agebin': 4}
>>>
>>>
So, that was the easy way to do a retrieve operation.

If the record is really large though, I might want a more granular operation.

For that, Aerospike offers 'select':
client.select(keytuple, ('foodbin', 'agebin'))
Also if I check that a record exists, that operation could be considered a retrieval call:
client.exists(keytuple)
The U in CRUD means Update:
>>>
>>> arecord['foodbin'] = 'Fred likes canned food'
>>>
>>> client.put(keytuple, arecord)
0L
>>>
>>> (apk, metadata, arecord2) = client.get(keytuple)
>>>
>>> arecord2
{'myrecpk': 100, 'foodbin': u'Fred likes canned food', 'myrecname': u'Fred', 'agebin': 4}
>>>
>>>
So that was the easy way to update, just do another put.

If the record is really large though, Aerospike offers 3 types of update operations which are more granular:
client.prepend(keytuple, 'foodbin', 'Mr. ')
client.append(keytuple, 'foodbin', ' usually.')
client.increment(keytuple, 'agebin', 2)
client.select(keytuple, ('foodbin','agebin'))[2]
{'foodbin': u'Mr. Fred likes canned food usually.', 'agebin': 6}
The D in CRUD means Delete:
>>>
>>> client.remove(keytuple)
0L
>>>
>>> client.exists(keytuple)
(('test', 'dogs', 100, bytearray(b'\xd7\xd6\xdd\xb1}\xfd\xc4\xd7\xfd\xbfs\xae\xc2\xce\xc8y\x08\xa9\x1aH')), None)
>>>
>>> (apk, metadata, arecord3) = client.get(keytuple)
>>>
>>> apk
('test', 'dogs', 100, bytearray(b'\xd7\xd6\xdd\xb1}\xfd\xc4\xd7\xfd\xbfs\xae\xc2\xce\xc8y\x08\xa9\x1aH'))
>>>
>>> metadata
>>>
>>> arecord3
>>>
So that is CRUD (Create, Retrieve, Update, Delete) of Aerospike data with Python.

syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me