Skip to Content

Java Client SDK

Installing Java client SDK

<dependency> <groupId>io.github.oxia-db</groupId> <artifactId>oxia-client</artifactId> <version>${VERSION}</version> </dependency>

Javadoc documentation is available at https://oxia-db.github.io/oxia-client-java/apidocs/latest 

Sync and Async APIs

Oxia provides two API flavors in the client:

  • Sync: Easier and convenient to use. Methods are blocking and return the result of the operation.
  • Async: Returns futures to track completion of the operations. Allows having more operations outstanding and achieving higher throughput.

Client API

Initializing the client

To use the Oxia client, you need to create a client instance. Once created, a client instance will be valid until it’s explicitly closed, and it can be used from different threads.

SyncOxiaClient client = OxiaClientBuilder.create("localhost:6648") .namespace("my-namespace") .syncClient();

For the full list of options when creating an Oxia clint, please refer to the OxiaClientBuilder Javadocs .

Writing records

// Write a record to Oxia with the specified key and value, and with the expectation // that the record does not already exist. PutResult res = client.put("my-key", "my-value".getBytes(), Set.of(IfRecordDoesNotExist)); // Write a record with the expectation that it has not changed since the previous write. // If there was any change, the operation will fail client.put("my-key", "my-value-2".getBytes(), Set.of(IfVersionIdEquals(res.version().versionId())));

All the options for the put operation are available in the Javadocs .

Reading records

Reading the value of a record

GetResult res = client.get("my-key"); log.info("Got key: {} - value: {} - version: {}", res.getKey(), res.getValue(), res.getVersion());

All the options for the get operation are available in the Javadocs .

Deleting records

Delete a single record by key. Supports conditional deletes using version-based expectations.

// Unconditional delete client.delete("my-key"); // Conditional delete: only succeed if the version matches client.delete("my-key", Set.of(IfVersionIdEquals(version.versionId())));

All the options for the delete operation are available in the Javadocs .

Deleting a range of records

Delete all records whose keys fall within a specified range.

// Delete all keys in the range [minKeyInclusive, maxKeyExclusive) client.deleteRange("/users/", "/users//");

Listing keys

List the keys within a specified range, without fetching the associated values.

List<String> keys = client.list("/users/", "/users//"); keys.forEach(key -> log.info("Key: {}", key));

Scanning records

Scan records in a key range, returning both keys and values.

List<GetResult> results = client.rangeScan("/users/", "/users//"); results.forEach(r -> log.info("Key: {} - Value: {} - Version: {}", r.getKey(), new String(r.getValue()), r.getVersion()));

For the complete list of options available on all operations, please refer to the Javadocs .

Last updated on