SDK
SDK Java v3.x
2

This SDK is deprecated. We recommend to use the Kuzzle SDK-JVM.
A migration guide is available here

Getting Started #

In this tutorial you will learn how to install the Kuzzle Java SDK. 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 it 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 on Android using gradle #

To build the project, add the following lines:

Maven #

<dependency>
  <groupId>io.kuzzle</groupId>
  <artifactId>kuzzle-sdk-java</artifactId>
  <version>3.0.0</version>
  <type>pom</type>
</dependency>

Gradle #

repositories {
    maven() {
        url  "https://dl.bintray.com/kuzzle/maven" 
    }
}
dependencies {
  compile 'io.kuzzle:kuzzle-sdk-java:3.0.0'
}

Ivy #

<dependency org='io.kuzzle' name='kuzzle-sdk-java' rev='3.0.0'>
  <artifact name='kuzzle-sdk-java' ext='pom' ></artifact>
</dependency>

First connection #

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

import io.kuzzle.sdk.*;
import io.kuzzle.sdk.Options.KuzzleOptions;
import io.kuzzle.sdk.Options.Protocol.WebSocketOptions;
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"
      WebSocketOptions opts = new WebSocketOptions();
      opts.setAutoReconnect(true).setConnectionTimeout(42000);
      WebSocket ws = new WebSocket("kuzzle", opts);
      // Instantiates a Kuzzle client
      kuzzle = new Kuzzle(ws, (KuzzleOptions) null);
      // 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();
  }
}

This program initializes the Kuzzle Server storage by creating a index, and a collection inside it Run the program with the following command:

$ 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!

Congratulations, you performed your first connection to Kuzzle Server via a Java program. You now know how to:

  • Instantiate Kuzzle SDK and connect to Kuzzle Server using a specific protocol (here websocket)
  • Create a 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 some data.

Here is how Kuzzle structures its storage space:

  • indexes contain collections
  • collections contain documents Create a GettingStartedStorage.java file in the playground and add this code:
import io.kuzzle.sdk.*;
import io.kuzzle.sdk.Options.KuzzleOptions;
import io.kuzzle.sdk.Options.Protocol.WebSocketOptions;
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"
      WebSocketOptions opts = new WebSocketOptions();
      opts.setAutoReconnect(true).setConnectionTimeout(42000);
      WebSocket ws = new WebSocket("kuzzle", opts);
      // Instantiates a Kuzzle client
      kuzzle = new Kuzzle(ws, (KuzzleOptions) null);
      // 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:

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

You can perform other actions such as delete, replace or search documents. There are also other ways to interact with Kuzzle like our Admin Console, the Kuzzle HTTP API or by using your own protocol.

Now you know how to:

  • Store documents in a Kuzzle Server, and access those

Subscribe to realtime document notifications (pub/sub) #

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

import io.kuzzle.sdk.*;
import io.kuzzle.sdk.Options.KuzzleOptions;
import io.kuzzle.sdk.Options.Protocol.WebSocketOptions;
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"
      WebSocketOptions opts = new WebSocketOptions();
      opts.setAutoReconnect(true).setConnectionTimeout(42000);
      WebSocket ws = new WebSocket("kuzzle", opts);
      // Instantiates a Kuzzle client
      kuzzle = new Kuzzle(ws, (KuzzleOptions) null);
      // 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.result));
          System.out.println("New created document notification: " + content);
        }).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:

$ 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: [Document content as ConcurrentHashMap]

You should see 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 Java SDK, you can dive even deeper to learn how to leverage its full capabilities: