This section will discuss how to develop an application with Grakn, using the Java API.

Declaring The Dependency In Maven

All applications will require the client-java dependency to be declared on the pom.xml of your application.

<repositories>
  <repository>
    <id>releases</id>
    <url>https://oss.sonatype.org/content/repositories/releases</url>
  </repository>
</repositories>

<properties>
    <grakn.version>1.3.0</grakn.version>
</properties>

<dependencies>
  <dependency>
    <groupId>ai.grakn</groupId>
    <artifactId>client-java</artifactId>
    <version>${grakn.version}</version>
  </dependency>
</dependencies>

Opening A Session And Transaction

A session object is responsible for maintaining a connection to a specific keyspace in the knowledge graph. Opening a session is performed by invoking the session() method on the grakn client. Once the session is open, you can proceed by creating a transaction in order to manipulate the data in the keyspace.

The following snippet shows how to open a Grakn session and transaction:

import ai.grakn.GraknTxType;
import ai.grakn.Keyspace;
import ai.grakn.client.Grakn;
import ai.grakn.util.SimpleURI;

public class App {
  public static void main(String[] args) {
    SimpleURI localGrakn = new SimpleURI("localhost", 48555);
    Keyspace keyspace = Keyspace.of("grakn");
    Grakn grakn = new Grakn(localGrakn);
    try (Grakn.Session session = grakn.session(keyspace)) {
      try (Grakn.Transaction transaction = session.transaction(GraknTxType.WRITE)) {
        // ...
        transaction.commit();
      }
    }
  }
}

Keyspace Uniqueness

A “Keyspace” uniquely identifies the knowledge graph and allows you to create different knowledge graphs.

Please note that keyspaces are not case sensitive. This means that these grakn, Grakn, and GrAkn names refer to the same keyspace.

Transaction Types

We currently support three transaction types:

  • GraknTxType.WRITE - A transaction that allows mutations to be performed on the knowledge graph
  • GraknTxType.READ - Prohibits any mutations to be performed to the knowledge graph
  • GraknTxType.BATCH - Allows faster mutations to be performed to the knowledge graph at the cost of switching off some internal consistency checks. This option should only be used if you are certain that you are loading a clean dataset.

Where Next?

The pages in this section of the documentation cover some of the public APIs available to Java developers:

There is also a page (in progress) that discusses advanced topics in Java development, such as transactions and multi-threading.

There is an example described in our blog that discusses how to get set up to develop using Java, and how to work with the Java API.

Tags: java