Java driver
The Java driver was developed by the TypeDB team to enable TypeDB support for Java software and developers.
Install
See the Version Compatibility table to check what versions of TypeDB and Java driver are compatible.
For Linux: the minimum version of glibc
is 2.25.0
.
Add the code below to the pom.xml
file in the Maven project.
Replace the |
<repositories>
<repository>
<id>repo.typedb.com</id>
<url>https://repo.typedb.com/public/public-release/maven/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.vaticle.typedb</groupId>
<artifactId>typedb-driver</artifactId>
<version>{version}</version>
</dependency>
</dependencies>
For more information, see the build systems setup page.
To use the TypeQL library, see the Java query builder page for installation instructions.
Add logging config
By default, the Java driver uses Logback to print errors and debugging info to standard output.
As it is quite verbose, you can use the following steps to set the minimum log level to ERROR
:
-
Create a file in the
resources
path (src/main/resources
by default in a Maven project) namedlogback.xml
. -
Copy the following document into the
logback.xml
:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="ERROR">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
Quickstart
See below a short example of Java code that connects to a local TypeDB Core,
creates a database named access-management-db
, defines a schema, inserts some data, and then reads it.
package org.example;
import com.vaticle.typedb.driver.TypeDB;
import com.vaticle.typedb.driver.api.TypeDBDriver;
import com.vaticle.typedb.driver.api.TypeDBSession;
import com.vaticle.typedb.driver.api.TypeDBTransaction;
import com.vaticle.typeql.lang.TypeQL;
import com.vaticle.typeql.lang.common.TypeQLArg;
import com.vaticle.typeql.lang.common.TypeQLToken;
import com.vaticle.typeql.lang.query.TypeQLDefine;
import com.vaticle.typeql.lang.query.TypeQLInsert;
import com.vaticle.typeql.lang.query.TypeQLFetch;
public class Main {
public static void main(String[] args) {
final String DB_NAME = "access-management-db";
final String SERVER_ADDR = "127.0.0.1:1729";
try (TypeDBDriver driver = TypeDB.coreDriver(SERVER_ADDR)) {
if (driver.databases().contains(DB_NAME)) {
driver.databases().get(DB_NAME).delete();
}
driver.databases().create(DB_NAME);
try (TypeDBSession session = driver.session(DB_NAME, TypeDBSession.Type.SCHEMA)) {
try (TypeDBTransaction tx = session.transaction(TypeDBTransaction.Type.WRITE)) {
TypeQLDefine defineQuery = TypeQL.define(
TypeQL.type("person").sub(TypeQLToken.Type.ENTITY).owns("name"),
TypeQL.type("name").sub(TypeQLToken.Type.ATTRIBUTE).value(TypeQLArg.ValueType.STRING)
);
tx.query().define(defineQuery);
tx.commit();
}
}
try (TypeDBSession session = driver.session(DB_NAME, TypeDBSession.Type.DATA)) {
try (TypeDBTransaction tx = session.transaction(TypeDBTransaction.Type.WRITE)) {
TypeQLInsert insertQuery = TypeQL.insert(
TypeQL.cVar("p1").isa("person").has("name", "Alice"),
TypeQL.cVar("p2").isa("person").has("name","Bob")
);
tx.query().insert(insertQuery);
tx.commit();
}
try (TypeDBTransaction tx = session.transaction(TypeDBTransaction.Type.READ)) {
TypeQLFetch fetchQuery = TypeQL.match(
TypeQL.cVar("p").isa("person")
).fetch(TypeQL.cVar("p").map("name"));
tx.query().fetch(fetchQuery).forEach(result -> System.out.println(result.toString()));
}
}
}
}
}
The above example uses Java query builder syntax to construct TypeQL queries. For more code examples, see the Tutorial and API reference links below.
Learn more
Any questions about the driver after reading the documentation? Feel free to ask on our Discord community server. |
Version Compatibility
Java driver | Protocol encoding version | TypeDB Core | TypeDB Cloud |
---|---|---|---|
2.28.0 |
3 |
2.28.0 |
2.28.0 |
2.27.0 |
3 |
2.27.0 |
2.27.0 |
2.26.6 |
3 |
2.26.6 |
2.26.6 |
2.25.8 |
3 |
2.25.7 |
2.25.7 |
2.24.15 |
2 |
2.24.17 |
2.24.17 |
See older versions
Java driver | Protocol encoding version | TypeDB Core | TypeDB Cloud |
---|---|---|---|
2.18.0, 2.18.1 |
1 |
2.18.0 to 2.23.0 |
2.18.0 to 2.23.0 |
2.17.0 to 2.17.1 |
N/A |
2.17.0 |
2.17.0 |
2.16.1 |
N/A |
2.16.1 |
2.16.1 |
2.14.1 to 2.14.3 |
N/A |
2.14.1 to 2.15 |
2.14.1 to 2.15.0 |
2.12.0 |
N/A |
2.12.0 to 2.13.0 |
2.13.0 |
2.9.0 to 2.11.1 |
N/A |
2.9.0 to 2.11.1 |
2.9.0 to 2.11.2 |
2.8.0 |
N/A |
2.8.0 |
N/A |
2.6.0 to 2.6.2 |
N/A |
2.6.0 to 2.7.1 |
N/A |
2.5.0 |
N/A |
2.1.2 to 2.5.0 |
2.5.0 |
2.1.0 to 2.4.0 |
N/A |
2.1.2 to 2.5.0 |
2.1.2 to 2.3.0 |
2.0.1 |
N/A |
2.0.2 |
2.0.2 |
2.0.0 |
N/A |
2.0.0, 2.0.1 |
2.0.0, 2.0.1 |
1.8.3 |
N/A |
1.8.0 to 1.8.4 |
N/A |
1.8.2 |
N/A |
1.8.0, 1.8.1 |
N/A |
1.8.0 to 1.8.1 |
N/A |
1.8.0 |
N/A |