SDK
SDK Jvm v1.x
2

Getting Started #

In this tutorial you will learn how to install and use the JVM SDK for Kuzzle. This page shows examples of scripts that store documents in Kuzzle, and of scripts that subcribe to real-time notifications for each new document created.

Before proceeding, please make sure your system meets the following requirements:

Having trouble? Get in touch with us on Discord!

Installation #

You can find the SDK JARs directly on bintray. Download and add them to your classpath.

The following examples are made to be executed without any IDE. If you're using Eclipse, IntelliJ or another Java IDE, you need to add the SDK as a project dependency in your classpath.

Installing for Android projects using gradle #

To build the project, add the following lines:

Maven #

Copied to clipboard!
<dependency>
  <groupId>io.kuzzle</groupId>
  <artifactId>sdk-jvm</artifactId>
  <version>1.0.1</version>
  <type>pom</type>
</dependency>

Gradle #

Copied to clipboard!
repositories {
    maven() {
        url  "https://dl.bintray.com/kuzzle/maven" 
    }
}
dependencies {
  compile 'io.kuzzle:sdk-jvm:1.0.1'
}

Ivy #

Copied to clipboard!
<dependency org='io.kuzzle' name='sdk-jvm' rev='1.0.1'>
  <artifact name='sdk-jvm' ext='pom' ></artifact>
</dependency>

First connection #

Initialize a new Java project, create a GettingStartedFirstConnection.java file and start by adding the code below:

Copied to clipboard!
import io.kuzzle.sdk.*;
import io.kuzzle.sdk.protocol.WebSocket;
import java.util.concurrent.ConcurrentHashMap;
public class SnippetTest {
  private static Kuzzle kuzzle;
  public static void main(String[] args) {
    try {
      // Creates a WebSocket connection.
      // Replace "kuzzle" with
      // your Kuzzle hostname like "localhost"
      WebSocket ws = new WebSocket("kuzzle");
      // Instantiates a Kuzzle client
      kuzzle = new Kuzzle(ws);
      // Connects to the server.
      kuzzle.connect();
      System.out.println("Connected!");
    } catch(Exception e){
      e.printStackTrace();
    }
    // Freshly installed Kuzzle servers are empty: we need to create
    // a new index.
    try {
      kuzzle.getIndexController().create("nyc-open-data").get();
      System.out.println("Index nyc-open-data created!");
    } catch(Exception e){
      e.printStackTrace();
    }
    // Creates a collection
    try {
      kuzzle.getCollectionController().create("nyc-open-data", "yellow-taxi").get();
      System.out.println("Collection yellow-taxi created!");
    } catch(Exception e){
      e.printStackTrace();
    }
    // Disconnects the SDK
    kuzzle.disconnect();
  }
}

If you're not yet familiar with how Kuzzle structures its storage, check our detailed guide

This program initializes the Kuzzle Server storage by creating an index and a collection. Run the program with the following command:

Copied to clipboard!
$ javac -classpath ./path/to/the/sdk.jar GettingStartedFirstConnection.java
$ java -classpath .:./path/to/the/sdk.jar GettingStartedFirstConnection
Connected!
Index nyc-open-data created!
Collection yellow-taxi created!

You now know how to:

  • Instantiate the Kuzzle SDK and connect to a Kuzzle Server using a specific network protocol (here websocket)
  • Create an index
  • Create a collection within an existing index

Create your first document #

Now that you successfully connected to your Kuzzle Server instance, and created an index and a collection, it's time to manipulate documents.

Create a GettingStartedStorage.java file in the playground and add this code:

Copied to clipboard!
import io.kuzzle.sdk.*;
import io.kuzzle.sdk.protocol.WebSocket;
import java.util.concurrent.ConcurrentHashMap;
public class SnippetTest {
  private static Kuzzle kuzzle;
  public static void main(String[] args) {
    try {
      // Creates a WebSocket connection.
      // Replace "kuzzle" with
      // your Kuzzle host name (e.g. "localhost")
      WebSocket ws = new WebSocket("kuzzle");
      // Instantiates a Kuzzle client
      kuzzle = new Kuzzle(ws);
      // Connects to the server.
      kuzzle.connect();
      System.out.println("Connected!");
    } catch(Exception e){
      e.printStackTrace();
    }
    // New document content
    ConcurrentHashMap<String, Object> content = new ConcurrentHashMap<>();
    content.put("name", "John");
    content.put("birthday", "1995-11-27");
    content.put("license", "B");
    // Stores the document in the "yellow-taxi" collection.
    try {
      kuzzle.getDocumentController()
        .create( "nyc-open-data", "yellow-taxi", content).get();
      System.out.println("New document added to the yellow-taxi collection!");
    } catch(Exception e){
      e.printStackTrace();
    }
    // Disconnects the SDK.
    kuzzle.disconnect();
  }
}

As you did before, build and run your program:

Copied to clipboard!
$ javac -classpath ./path/to/the/sdk.jar  GettingStartedStorage.java
$ java -classpath .:./path/to/the/sdk.jar GettingStartedStorage
Connected!
New document added to the yellow-taxi collection!

Many other actions are available to manipulate stored documents. You can check the exhaustive list in the Document Controller documentation.

Now you know how to:

  • Store documents in a Kuzzle Server, and access to those

Subscribe to realtime document notifications (pub/sub) #

Time to use Kuzzle's realtime capabilities. Create a new file GettingStartedRealtime.java with the following code:

Copied to clipboard!
import io.kuzzle.sdk.*;
import io.kuzzle.sdk.protocol.WebSocket;
import java.util.concurrent.ConcurrentHashMap;
public class SnippetTest {
  private static Kuzzle kuzzle;
  public static void main(String[] args) {
    try {
      // Creates a WebSocket connection.
      // Replace "kuzzle" with
      // your Kuzzle hostname like "localhost"
      WebSocket ws = new WebSocket("kuzzle");
      // Instantiates a Kuzzle client
      kuzzle = new Kuzzle(ws);
      // Connects to the server.
      kuzzle.connect();
      System.out.println("Connected!");
    } catch (Exception e) {
      e.printStackTrace();
    }
    // Subscribes to notifications for drivers having a "B" driver license.
    ConcurrentHashMap<String, Object> filters = new ConcurrentHashMap<>();
    ConcurrentHashMap<String, Object> equals = new ConcurrentHashMap<>();
    equals.put("license", "B");
    filters.put("equals", equals);
    try {
      // Sends the subscription
      kuzzle.getRealtimeController()
        .subscribe("nyc-open-data", "yellow-taxi", filters, notification -> {
          ConcurrentHashMap<String, Object> content = ((ConcurrentHashMap<String, Object>)(notification.getResult()));
          System.out.println("New created document notification: " + content);
          /*
          {
          _source={
            birthday=1995-11-27,
            license=B,
            name=John,
            _kuzzle_info={
              createdAt=1605694059151,author=-1
              }
            },
            _id=9PDS2nUBeGNr7nwl8j2Q,
            _version=1
          }
          */
        }).get();
      System.out.println("Successfully subscribed!");
    } catch (Exception e) {
      e.printStackTrace();
    }
    // Writes a new document. This triggers a notification
    // sent to our subscription.
    ConcurrentHashMap<String, Object> content = new ConcurrentHashMap<>();
    content.put("name", "John");
    content.put("birthday", "1995-11-27");
    content.put("license", "B");
    try {
      kuzzle.getDocumentController()
        .create("nyc-open-data", "yellow-taxi", content).get();
      System.out.println("New document added to the yellow-taxi collection!");
    } catch (Exception e) {
      e.printStackTrace();
    }
    // Disconnects the SDK.
    kuzzle.disconnect();
  }
}

This program subscribes to changes made to documents with a license field set to B, within the yellow-taxi collection. Whenever a document matching the provided filters changes, a new notification is received from Kuzzle.

Build and run your program:

Copied to clipboard!
$ javac -classpath ./path/to/the/sdk.jar GettingStartedRealtime.java
$ java -classpath .:./path/to/the/sdk.jar GettingStartedRealtime
Connected!
Successfully subscribing!
New document added to yellow-taxi collection!
New created document notification: 

{
  _source={
    birthday=1995-11-27,
    license=B,
    name=John,
    _kuzzle_info={
      createdAt=1605694059151,author=-1
      }
    },
    _id=9PDS2nUBeGNr7nwl8j2Q,
    _version=1
  }

You should see the document content as a ConcurrentHashMap.

Now, you know how to:

  • Create realtime filters
  • Subscribe to notifications

Where do we go from here? #

Now that you're more familiar with the JVM SDK, you can dive even deeper to learn how to leverage its full capabilities: