Python method to read and write files LMDB

Original: https: //www.jb51.net/article/142985.htm

LMDB stands Lightning Memory-Mapped Database (lightning fast memory map database), its simple structure file, a data file contains the file and a lock:

LMDB files can be opened simultaneously by multiple processes, with high speed data access, access is simple, no need to run separate database management process, as long as the references in the code to access the data in the LMDB library, to access the path to the file.

Allow the system to access a large number of small files is expensive, but LMDB using memory-mapped access to a file within the file so that addressing overhead is very small, using pointer arithmetic can be achieved. Single database file can reduce the overhead of copying data collection / transmission process.

Lmdb used in python: linux, use the command 'pip install lmdb' lmdb installation package.

1. Create an empty database file lmdb

. 1  # - * - Coding: UTF-. 8 - * - 
2  Import lmdb 
 . 3    
. 4  # If the folder is not data.mbd train or lock.mdb file, a blank is generated, if any, will not be overwritten 
. 5  # map_size defines the maximum storage capacity, the unit is kb, the following definitions 1TB capacity 
. 6 the env = lmdb.open ( " ./train " , map_size = 1099511627776 ) 
 . 7 env.close ()

 

 2. LMDB data add, modify, delete

. 1  # - * - Coding: UTF-. 8 - * - 
2  Import lmdb 
 . 3    
. 4  # map_size defines the maximum storage capacity, the unit is kb, the following definitions 1TB capacity 
. 5 the env = lmdb.open ( " ./train " , map_size = 1099511627776 ) 
 . 6    
. 7 TXN = env.begin (Write = True) 
 . 8    
. 9  # add data and key 
10 txn.put (Key = ' . 1 ' , value = ' AAA ' ) 
 . 11 txn.put (Key = ' 2 ' , value = ' BBB ' ) 
 12 istxn.put (Key = ' . 3 ' , value = ' CCC ' ) 
 13 is    
14  # delete data key 
15 txn.delete (Key = ' . 1 ' ) 
 16    
. 17  # modified data 
18 is txn.put (Key = ' . 3 ' , value = ' ddd ' ) 
 . 19    
20 is  # () function changes submitted by the commit 
21 is  txn.commit () 
 22 is env.close ()

 

3. Query database content lmdb

. 1  # - * - Coding: UTF-. 8 - * - 
2  Import lmdb 
 . 3    
. 4 the env = lmdb.open ( " ./train " ) 
 . 5    
. 6  # parameter set to True can write write 
. 7 TXN = env.begin (write = True) 
 8  # ########################################### added, modify, delete data 
. 9    
10  # add data and key 
. 11 txn.put (Key = ' . 1 ' , value = ' AAA ' ) 
 12 is txn.put (Key = ' 2 ' , value = ' BBB' ) 
 13 is txn.put (Key = ' . 3 ' , value = ' CCC ' ) 
 14    
15  # By deleting the key data 
16 txn.delete (Key = ' . 1 ' ) 
 . 17    
18 is  # modify data 
. 19 txn.put (Key = ' . 3 ' , value = ' ddd ' ) 
 20 is    
21 is  # function changes submitted by the commit () 
22 is  txn.commit () 
 23 is  # ###################### ##################### data query lmdb 
24- txn =env.begin () 
 25    
26  # GET functions to query the data by key 
27  Print txn.get (str (2 )) 
 28    
29  # by the cursor () loop through all the data and key 
30  for Key, value in txn.cursor () : 
 31 is    Print (Key, value) 
 32      
33 is  # ####################################### #### 
34 is    
35 env.close ()

 

4. Read the contents of an existing .mdb file

. 1  # - * - Coding: UTF-. 8 - * - 
2  Import lmdb 
 . 3    
. 4 env_db = lmdb.Environment ( ' trainC ' ) 
 . 5  # env_db = lmdb.open ( "./ trainC") 
. 6    
. 7 TXN = env_db.begin ( ) 
 . 8    
. 9  # GET function through the key data query, if the query does not correspond to the key data output None 
10  Print txn.get (STR (200 is )) 
 . 11    
12 is  for Key, value in txn.cursor (): # traverse 
13 is    Print (Key, value) 
 14    
15 env_db.close ()

 

Guess you like

Origin www.cnblogs.com/KAKAFEIcoffee/p/11958959.html