Python Lmdb Demo This interface is as disgusting as BDB!

Source: Internet
Author: User

Import Lmdblmdb_img_name="Test.lmdb"Env= Lmdb.open (Lmdb_img_name, map_size=1e6) with Env.begin (write=True) as Txn:txn.put ("Key","xxxxxxxxxxxxxxxxxx") Txn.put ("Key2","?") Txn.put ("Key3","some value")    #Txn.commit ()env.close () env= Lmdb.open (Lmdb_img_name, readonly=True) with Env.begin () as TXN:PrintTxn.get (b'Key') Cursor=txn.cursor () forKey, Valueinchcursor:Print(Key, value)

See more: http://blog.csdn.net/ayst123/article/details/44077903, excerpt as follows:

Write
db_img = lmdb.Environment(lmdb_img_name,map_size=int(1e12))txn_img = db_img.begin(write=True,buffers=True)datum_img = getImgDatum(img_path)txn_img.put(key,datum_img.SerializeToString())txn_img.commit()db_img.close()

Here, first establish the Lmdb instance, db_img. Then start TXN (there may be a lot of starts?) )。 Put storage, and finally must pay attention to commit (). Otherwise there is no deposit in, check the generated Lmdb file can be seen. Finally, close ()

Here the generated datum is the function with Caffe, do io.py, called Caffe.io.array_to_datum, the array into datum.

Note:

Datum must use Datum when it is saved. Serializetostring (), turn it into a string ()

Segmented Write
for ind,obj in enumerate(list):    if ind%100=0: txn.commit() txn = env.begin(write=True)txn.commit()env.close()

After each commit, you need to define Txn=env.begin (Write=true) again
For loop out of the last commit

Read
img_lmdb = imdb.open("name")txn= img_lmdb.begin()cursor = txn.cursor()cursor.iternext()key = cursor.key()value = cursor.value()datum = caffe_pb2.Datum()datum.ParseFromString(value)

Note:

Cursor the first is empty, Iternext () is the first value

Loops

TXN = ...
cursor = Txn.cursor ()
For (IDX, (key, value)) in enumerate (cursor):
................

It's going to look like a loop.

Python Lmdb Demo This interface is as disgusting as BDB!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.