Exploring Leveldb Embedded Database With Python Part 1

I got to know about LevelDB from the guys of Nodeup, a podcast program for node.js and they shared load of goodness about the project and it really got me excited to start exploring. You can heard the podcast here. In short, LevelDB is an open source on-disk key-value store written by Jeffrey Dean and Sanjay Ghemawat of Google.

Check out the benchmarks. LevelDB performs very well for sequential writes, bulk reads and writes. Not as good for random reads.

Riak another on-disk key-value database adopted LevelDB through ElevelDB as an alternative storage engine.

Now let’s get our hands dirty!

Download LevelDB and follow the commands below for installation. Do note that the installation location is not specified.

Install LevelDB
1
2
3
$ make 
$ sudo cp libleveldb.a /usr/local/lib 
$ sudo cp -r include/leveldb /usr/local/include

Plyvel is the python wrapper for LevelDB and it is very well documented.

Install Plyvel
1
2
3
$ sudo apt-get install libleveldb1 libleveldb-dev 
$ pip install plyvel 
$ python -c 'import plyvel'

With a couple of lines you can create the DB, write a record, retrieve it and print it out.

test_script.py
1
2
3
4
5
import plyvel
db = plyvel.DB('/tmp/hello_world/', create_if_missing=True)
key = 'hello world'
db.put(key, b'<html><body><h1>Hello World</h1></body></html>')
print db.get(key)

As you can see LevelDB is a very modular, it can be coupled with other technologies to handle different storage needs.