A database that holds a key-value data format (byte array). It is written in C ++ and can be used with JNI from Java. However, Java's API has not caught up with C ++'s API.
Wiki https://ja.wikipedia.org/wiki/RocksDB
The simple usage is described below. See the official documentation for details.
https://github.com/facebook/rocksdb/wiki/RocksJava-Basics
Maven Use the following libraries. If you use only RocksDb, you only need rocksdbjni. Kryo is added for serialization and deserialization.
<dependency>
<groupId>org.rocksdb</groupId>
<artifactId>rocksdbjni</artifactId>
<version>6.0.1</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>3.0.1</version>
</dependency>
There is also a method for closing, but we recommend using try-with-resources, so let's follow it. It is necessary to specify the path of the DB save destination for open. If you specify as follows, a folder for DB will be created directly under the project.
try (RocksDB rocksDb = RocksDB.open("sample")) {
//Write the process here
}
This is a basic API usage example. Read is get, write is put, and delete is delete. In addition, usage examples of all acquisition (newIterator), range deletion (deleteRange), and multiple acquisition (multiGetAsList) are described.
public void execute() {
//Open RocksDB. Specify the DB save destination for open (created directly under the project if the following is specified)
try (RocksDB rocksDb = RocksDB.open("sample")) {
//Serialize using Kryo
byte[] binaryKey = serialize("key");
byte[] binaryValue = serialize("Value");
//Specify Key and Value and put to RocksDB (API#put)
//Even if you register a duplicate Key, no error will occur, just overwrite it
rocksDb.put(binaryKey, binaryValue);
rocksDb.put(binaryKey, binaryValue);
//Obtained from RocksDB using serialized Key (API#get)
try (Input input = new Input(rocksDb.get(binaryKey))) {
System.out.println(kryo.readObject(input, String.class));
}
//Delete from RockDB by specifying Key (API#delete)
rocksDb.delete(binaryKey);
if (null == rocksDb.get(binaryKey)) {
System.out.println("Can't get");
}
for (int i = 0; i < 10; i++) {
//Serialize using Kryo
byte[] tempBinaryKey = serialize("key" + String.valueOf(i));
byte[] tempBinaryValue = serialize("Value"+ String.valueOf(i));
//Specify Key and Value and put to RocksDB
rocksDb.put(tempBinaryKey, tempBinaryValue);
}
//Get all the data in RocksDB (API#newIterator)
System.out.println("Get all entries");
RocksIterator iterator = rocksDb.newIterator();
iterator.seekToFirst();
while (iterator.isValid()) {
try (Input input = new Input(iterator.key())) {
System.out.println(kryo.readObject(input, String.class));
}
iterator.next();
}
//Delete in RocksDB by specifying a key range(API#deleteRange)
//Specified in beginKey ~ Immediately before EndKey is deleted
rocksDb.deleteRange(serialize("key0"), serialize("key8"));
System.out.println("Range deletion key0-key7");
RocksIterator iterator2 = rocksDb.newIterator();
iterator2.seekToFirst();
while (iterator2.isValid()) {
try (Input input = new Input(iterator2.value())) {
System.out.println(kryo.readObject(input, String.class));
}
iterator2.next();
}
//Get multiple in RocksDB(#multiGetAsList)
System.out.println("Get multiple");
List<byte[]> binaryKeyList = new ArrayList<>();
binaryKeyList.add(serialize("key8"));
binaryKeyList.add(serialize("key9"));
List<byte[]> binaryValueList = rocksDb.multiGetAsList(binaryKeyList);
for (byte[] binary:binaryValueList) {
try (Input input = new Input(binary)) {
System.out.println(kryo.readObject(input, String.class));
}
}
} catch (RocksDBException e) {
throw new RuntimeException(e);
}
}
private byte[] serialize(String str) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (Output output = new Output(baos)) {
kryo.writeObject(output, str);
}
return baos.toByteArray();
}
You can use it just by installing the library, so you don't even have to use RDB, I think it will be useful in situations where data persistence is required.
https://github.com/EsotericSoftware/kryo https://github.com/facebook/rocksdb/wiki/RocksJava-Basics
Recommended Posts