Official RocksDB Java API for JVM-based applications
The official Java binding for RocksDB provides a comprehensive API for JVM-based applications. It includes full support for core RocksDB features including transactions, column families, iterators, and advanced configuration options.
import org.rocksdb.*;public class RocksDBExample { static { RocksDB.loadLibrary(); } public static void main(String[] args) { try (final Options options = new Options().setCreateIfMissing(true); final RocksDB db = RocksDB.open(options, "/path/to/db")) { // Put a key-value pair db.put("hello".getBytes(), "world".getBytes()); // Get a value byte[] value = db.get("hello".getBytes()); System.out.println("Value: " + new String(value)); // Delete a key db.delete("hello".getBytes()); } catch (RocksDBException e) { System.err.println("Error: " + e.getMessage()); } }}
Column families allow you to logically partition your data within a single database:
import org.rocksdb.*;import java.util.*;RocksDB.loadLibrary();// Open DB with multiple column familiesList<ColumnFamilyDescriptor> cfDescriptors = Arrays.asList( new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, new ColumnFamilyOptions()), new ColumnFamilyDescriptor("new_cf".getBytes(), new ColumnFamilyOptions()));List<ColumnFamilyHandle> cfHandles = new ArrayList<>();try (final DBOptions options = new DBOptions(); final RocksDB db = RocksDB.open(options, dbPath, cfDescriptors, cfHandles)) { // Write to specific column family db.put(cfHandles.get(1), "key".getBytes(), "value".getBytes()); // Atomic write across column families try (final WriteBatch batch = new WriteBatch()) { batch.put(cfHandles.get(0), "key1".getBytes(), "value1".getBytes()); batch.put(cfHandles.get(1), "key2".getBytes(), "value2".getBytes()); db.write(new WriteOptions(), batch); } // Drop column family db.dropColumnFamily(cfHandles.get(1));} catch (RocksDBException e) { e.printStackTrace();} finally { for (ColumnFamilyHandle handle : cfHandles) { handle.close(); }}
import org.rocksdb.*;try (final RocksDB db = RocksDB.open(options, dbPath); final RocksIterator iterator = db.newIterator()) { // Iterate forward from the first key for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) { byte[] key = iterator.key(); byte[] value = iterator.value(); System.out.println(new String(key) + " => " + new String(value)); } // Iterate backward from the last key for (iterator.seekToLast(); iterator.isValid(); iterator.prev()) { byte[] key = iterator.key(); byte[] value = iterator.value(); System.out.println(new String(key) + " => " + new String(value)); } // Seek to a specific key iterator.seek("prefix".getBytes()); if (iterator.isValid()) { System.out.println("Found: " + new String(iterator.key())); }} catch (RocksDBException e) { e.printStackTrace();}
RocksDB objects must be explicitly closed to prevent memory leaks. Always use try-with-resources or explicitly call close().
The Java binding uses JNI to interact with the native RocksDB library. All RocksDB objects extend RocksObject or AbstractNativeReference and must be properly disposed:
// Good: Using try-with-resourcestry (final Options options = new Options(); final RocksDB db = RocksDB.open(options, dbPath)) { // Use database} catch (RocksDBException e) { e.printStackTrace();}// Also acceptable: Manual cleanupOptions options = new Options();RocksDB db = null;try { db = RocksDB.open(options, dbPath); // Use database} catch (RocksDBException e) { e.printStackTrace();} finally { if (db != null) db.close(); options.close();}
The Options class combines both DBOptions and ColumnFamilyOptions:
// Options = DBOptions + ColumnFamilyOptionsOptions options = new Options(); .setCreateIfMissing(true) // DB option .setWriteBufferSize(64 * 1024) // CF option .setMaxBackgroundJobs(4); // DB option// Or use separate options for more controlDBOptions dbOptions = new DBOptions().setCreateIfMissing(true);ColumnFamilyOptions cfOptions = new ColumnFamilyOptions().setWriteBufferSize(64 * 1024);
Many options that appear as database options actually apply as defaults for column families. See the understanding_options.md guide for details.