Get started with FoundationDB Java bindings in minutes.
Add to your pom.xml:
<dependency>
<groupId>com.apple.foundationdb</groupId>
<artifactId>fdb-java</artifactId>
<version>7.4.3</version>
</dependency>import com.apple.foundationdb.FDB;
FDB fdb = FDB.selectAPIVersion(740);import com.apple.foundationdb.Database;
try (Database db = fdb.open()) {
// Use database
}import com.apple.foundationdb.*;
FDB fdb = FDB.selectAPIVersion(740);
try (Database db = fdb.open()) {
// Write data
db.run(tr -> {
tr.set("key".getBytes(), "value".getBytes());
return null;
});
// Read data
String value = db.run(tr -> {
byte[] result = tr.get("key".getBytes()).join();
return result != null ? new String(result) : null;
});
System.out.println("Value: " + value);
}Use Database.run() for automatic retry on transient errors:
db.run(tr -> {
// All operations here automatically retry on conflicts
tr.set("key1".getBytes(), "value1".getBytes());
tr.set("key2".getBytes(), "value2".getBytes());
return null;
});Use Database.read() for read-only operations:
List<String> keys = db.read(tr -> {
List<String> result = new ArrayList<>();
for (KeyValue kv : tr.getRange("prefix:".getBytes(), "prefix;".getBytes())) {
result.add(new String(kv.getKey()));
}
return result;
});import com.apple.foundationdb.tuple.Tuple;
Tuple key = Tuple.from("users", 1001, "profile");
byte[] packedKey = key.pack();
db.run(tr -> {
tr.set(packedKey, "user data".getBytes());
return null;
});import com.apple.foundationdb.subspace.Subspace;
import com.apple.foundationdb.tuple.Tuple;
Subspace users = new Subspace(Tuple.from("users"));
byte[] key = users.pack(Tuple.from(1001));
db.run(tr -> {
tr.set(key, "user data".getBytes());
return null;
});FDB.selectAPIVersion() before any other operations