Using LevelDB with Java and Go: Basic API Operations
This article introduces LevelDB, a Google‑built key/value store, and demonstrates how to integrate it in Java and Go projects by showing dependency setup, basic operations such as opening, putting, getting, deleting, iterating, and closing the database, and explains the typical use cases and file layout.
Recently I started learning LevelDB and, after a brief study, have mastered its basic usage without any major obstacles.
In this article I share the basic LevelDB API usage in both Java and Go. LevelDB feels like a simplified version of Redis and works well as a local database, especially for caching. The official use case targets heavy write, light read workloads, making it suitable for performance‑test data recording. I may later build a performance‑test data collection tool based on LevelDB.
LevelDB Introduction
LevelDB is a key/value store built by Google that provides an ordered mapping from string keys to string values. Its core storage architecture is a Log‑Structured Merge (LSM) tree, a write‑optimized variant of a B‑tree, optimized for large sequential writes rather than small random writes.
Note: When using a library to operate LevelDB you do not need to install LevelDB itself; this misunderstanding cost me about an hour.
Java
Although the code is Java, it can also be used from Groovy; for readability I only share the Java usage.
Dependency
Maven:
<!-- https://mvnrepository.com/artifact/org.iq80.leveldb/leveldb -->
<dependency>
<groupId>org.iq80.leveldb</groupId>
<artifactId>leveldb</artifactId>
<version>0.12</version>
</dependency>Gradle:
// https://mvnrepository.com/artifact/org.iq80.leveldb/leveldb
implementation group: 'org.iq80.leveldb', name: 'leveldb', version: '0.12'Basic Operations
Open a database file:
Iq80DBFactory factory = Iq80DBFactory.factory;
Options options = new Options();
DB db = factory.open(new File("fun"), options);Add a key and value:
db.put(bytes("scores"), bytes("scoreJSON.toString())"));The org.iq80.leveldb.impl.Iq80DBFactory#bytes method is used here, but you can wrap it yourself if you prefer.
Get the value of a key:
byte[] scores = db.get(bytes("scores"));The returned value is also a byte array and needs to be converted to the target object.
Delete a key:
db.delete(bytes("fun"));Iterate over the database:
DBIterator iterator = db.iterator();
while (iterator.hasNext()) {
Map.Entry
next = iterator.next();
output(next.getKey());
}Close the database:
db.close();After closing, you will see a directory named fun containing a set of files with extensions such as *.sst as well as files like CURRENT, LOCK, etc.
Go
Dependency
github.com/syndtr/goleveldb v1.0.0Basic Operations
Open a database file:
db, err := leveldb.OpenFile("funtester", nil)
if err != nil {
log.Println("creation error!", err)
}Add a key and value:
db.Put([]byte(task.FunTester), fun, nil)It is recommended to wrap this in a helper method.
Get the value of a key:
get, er := db.Get([]byte(task.FunTester), nil)The returned value is also a byte array and must be transformed into the desired object.
Delete a key:
db.Delete([]byte("test"), nil)Iterate over the database:
iterator := db.NewIterator(nil, nil)
for iterator.Next() {
key := iterator.Key()
value := iterator.Value()
log.Printf("data key:%s, value:%s", key, value)
}Close the database:
db.Close()Welcome to follow FunTester, Have Fun ~ Tester!
140 Interview Questions (UI, Linux, MySQL, API, Security)
HTTP Mind Map Illustrated
Fiddler Learning Pack
Thread Pool Batch API Request Practice
10 000 QPS: K6, Gatling vs FunTester Showdown
Performance Bottleneck Tuning (Fan Submissions)
How to Choose an API Testing Tool
API Testing Tips for Beginners
API Automation Testing Guide
API Testing Basics
Data‑Driven Automation Testing
Balancing Automated and Manual Testing
43 Common Software Testing Categories
- END -
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.