Java connector
Build, connect, query and handle errors with the Raft DB Java connector (Java 17, no dependencies).
The Java connector speaks the Raft DB binary protocol (specification) and offers the same operations as every other connector. Download the package from the Download page.
No TLS yet. User names, passwords and tokens travel in clear text. Use only on trusted networks.
Requirements#
Java 17. Standard library only.
Install#
Download the jar from the Download page and put it on your class path:
javac -cp raftdb-java-connector-<version>.jar MyApp.java
java -cp raftdb-java-connector-<version>.jar;. MyApp # use : instead of ; on Linux and macOSThe jar holds the compiled classes of the package io.raftdb. Kotlin, Scala and Clojure use the same jar, it is an ordinary JVM library.
Connect#
import io.raftdb.*;
// or connect(host, port, user, password, token, timeoutMs); the timeout default is 30 s
try (Client c = Client.connect("127.0.0.1", 7480)) {
c.ping();
}Query#
try (Client c = Client.connect("127.0.0.1", 7480)) {
c.put("u1", "{\"name\":\"Asha\",\"age\":31}"); // returns true if the key existed
String row = c.get("u1"); // JSON text, or null
long n = c.count("{\"age\":{\"$gte\":30}}");
Page p = c.querySql("SELECT id FROM documents ORDER BY id", 70, 0);
for (String r : c.scan("{}")) System.out.println(r); // follows cursors
}A SQL page and a filter scan#
c.createIndex("age", true);
Page page = c.querySql("SELECT name FROM documents ORDER BY age LIMIT 10", 0, 0);
page.rows().forEach(r -> System.out.println("row: " + r));
for (String row : c.scan("{\"addr.city\":\"Pune\"}")) {
System.out.println("scan: " + row);
}Errors#
RaftDBException (unchecked) is a server refusal and has code(). After code 7 the connection is closed. IOException (checked) is a network failure, a timeout, a protocol violation or use after close(); scan iterators wrap it in UncheckedIOException.
try {
c.count("{\"age\":");
} catch (RaftDBException e) {
System.out.println("error code " + e.code() + ": " + e.getMessage());
}Transactions and prepared statements#
The connector has no transaction call and no prepared-statement call: the protocol has none (every operation is one request and one reply, see the specification), and none of the connector's README, tests or examples uses one. Use the MySQL or MariaDB front-end with a standard driver for those: see Standard MySQL and MariaDB drivers.
Notes#
- Rows are
StringJSON; parse them with the JSON library you prefer. - Methods are
synchronized: a shared client is safe but serialised. Use one client per thread for parallelism. - Cursors are unsigned 64-bit values held in a
long;Page.nextText()prints them unsigned.
Conformance status#
24/24 conformance scenarios. The scenarios are listed in the specification page. The package's README.txt has the install steps and a short example.