Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
5 contributors

Users who have contributed to this file

@sorsaffari @lolski @jmsfltchr @kasper-piskorski @haikalpribadi
210 lines (168 sloc) 6.76 KB
pageTitle keywords longTailKeywords Summary templatePath
Client Java
grakn, client, java
grakn java client, grakn client java, client java, java client
API Reference of Grakn Client Java.
03-client-api/references/

Dependencies

Client Java Grakn Core Grakn KGMS
1.5.3 1.5.2 to 1.5.7 1.5.2 to 1.5.4
1.5.2 1.5.2, 1.5.3 1.5.2 to 1.5.4
1.5.0 1.5.0 N/A
1.4.3 1.4.3 1.4.3
1.4.2 1.4.2 1.2.0
1.4.1 1.4.0 1.2.0
1.4.0 1.4.0 1.2.0
1.3.0 1.3.0 1.2.0

[tab:Grakn Core]

<repositories>
    <repository>
        <id>repo.grakn.ai</id>
        <url>https://repo.grakn.ai/repository/maven/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        &lt;groupId&gt;io.grakn.client&lt;/groupId&gt;
        &lt;artifactId&gt;api&lt;/artifactId&gt;
        <version>1.5.2</version>
    </dependency>
    <dependency>
        &lt;groupId&gt;io.grakn.core&lt;/groupId&gt;
        &lt;artifactId&gt;concept&lt;/artifactId&gt;
        <version>1.5.3</version>
    </dependency>
    <dependency>
        &lt;groupId&gt;io.graql&lt;/groupId&gt;
        &lt;artifactId&gt;lang&lt;/artifactId&gt;
        <version>1.0.1</version>
    </dependency>
</dependencies>

[tab:end]

[tab:Grakn KGMS]

<repositories>
    <repository>
        <id>mavencentral</id>
        <url>https://oss.sonatype.org/content/repositories/releases</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        &lt;groupId&gt;ai.grakn.kgms&lt;/groupId&gt;
        &lt;artifactId&gt;client&lt;/artifactId&gt;
        <version>1.4.3</version>
    </dependency>
</dependencies>

[tab:end]

Quickstart

First make sure, the Grakn Server is running.

Import grakn.client.GraknClient, instantiate a client and open a session to a keyspace.

package grakn.examples;

import grakn.client.GraknClient;

public class GraknQuickstartA {
    public static void main(String[] args) {
        GraknClient client = new GraknClient("localhost:48555");
        // client is open
        GraknClient.Session session = client.session("social_network");
        // session is open
        session.close();
        // session is closed
        client.close();
        // client is closed
    }
}

[KGMS ONLY] Using Client Java 1.4.3, we can also pass the credentials, as specified when configuring authentication via Grakn Console.

SimpleURI localGrakn = new SimpleURI("localhost", 48555);
Grakn grakn = new ClientFactory(localGrakn, "<username>", "<password>").client();

Create transactions to use for reading and writing data.

package grakn.examples;

import grakn.client.GraknClient;

public class GraknQuickstartB {
    public static void main(String[] args) {
        GraknClient client = new GraknClient("localhost:48555");
        GraknClient.Session session = client.session("social_network");

        // creating a write transaction
        GraknClient.Transaction writeTransaction = session.transaction().write();
        // write transaction is open
        // write transaction must always be committed (closed)
        writeTransaction.commit();

        // creating a read transaction
        GraknClient.Transaction readTransaction = session.transaction().read();
        // read transaction is open
        // read transaction must always be closed
        readTransaction.close();

        session.close();
        client.close();
    }
}

Running basic retrieval and insertion queries.

package grakn.examples;

import grakn.client.GraknClient;
import graql.lang.Graql;
import static graql.lang.Graql.*;
import graql.lang.query.GraqlGet;
import graql.lang.query.GraqlInsert;
import grakn.core.concept.answer.ConceptMap;

import java.util.List;
import java.util.stream.Stream;

public class GraknQuickstartC {
  public static void main(String[] args) {
    GraknClient client = new GraknClient("localhost:48555");
    GraknClient.Session session = client.session("social_network");

    // Insert a person using a WRITE transaction
    GraknClient.Transaction writeTransaction = session.transaction().write();
    GraqlInsert insertQuery = Graql.insert(var("x").isa("person").has("email", "x@email.com"));
    List<ConceptMap> insertedId = writeTransaction.execute(insertQuery);
    System.out.println("Inserted a person with ID: " + insertedId.get(0).get("x").id());
    // to persist changes, a write transaction must always be committed (closed)
    writeTransaction.commit();

    // Read the person using a READ only transaction
    GraknClient.Transaction readTransaction = session.transaction().read();
    GraqlGet getQuery = Graql.match(var("p").isa("person")).get().limit(10);
    Stream<ConceptMap> answers = readTransaction.stream(getQuery);
    answers.forEach(answer -> System.out.println(answer.get("p").id()));

    // transactions, sessions and clients must always be closed
    readTransaction.close();
    session.close();
    client.close();
  }
}
[Important] Remember that transactions always need to be closed. Commiting a write transaction closes it. A read transaction, however, must be explicitly closed by calling the `close()` method on it.

Check out the Concept API to learn about the available methods on the concepts retrieved as the answers to Graql queries.

To view examples of running various Graql queries using the Grakn Client Java, head over to their dedicated documentation pages as listed below.


API Reference

{% include api/generic.html data=site.data.03_client_api.references.grakn language="java" %}

{% include api/generic.html data=site.data.03_client_api.references.client language="java" %}

{% include api/generic.html data=site.data.03_client_api.references.session language="java" %}

{% include api/generic.html data=site.data.03_client_api.references.transaction language="java" %}

{% include api/generic.html data=site.data.03_client_api.references.graql language="java" %}

{% include api/answers.html data=site.data.03_client_api.references.answer language="java" %}

You can’t perform that action at this time.