Develop a Java Query

This section of the crash course covers Deephaven Enterprise's client APIs. This guide discusses the Java client, which allows you to:

  • Create new workers
  • Interact with tables and objects on the server
  • Connect to existing Persistent Queries (PQs)
  • Create PQs
  • Run queries server-side
  • And more!

Prerequisites

To access the examples, you will need to connect to Deephaven's Artifactory using an API key. Please ensure you have the necessary username (provided by your administrator) and API key to download the required files.

Add the following entries to your gradle.properties file, which is located at ${HOME}/.gradle/gradle.properties on a Linux-based system:

Alternatively, if you already have a development environment with access to the Deephaven code, you can use that instead of the provided Gradle files.

Setup

Persistent Query setup

This example demonstrates how to connect to a Persistent Query to retrieve data. First, log in to your Deephaven server's web UI, then use the Query Monitor to create a Persistent Query.

  1. On the Settings tab:
  • Call the PQ MyTestJavaClientPQ.
  • Select Live Query (Script) for the type.
  • Select a DB Server of AutoQuery.
  • Use the Core+ engine.
  • Give it a heap size of 1 GB.
  1. On the Scheduling tab, select Disabled.

  2. On the Script tab, select a Runtime of Groovy. The script consists of one line to retrieve the ProcessEventLog table and view the LogEntry column in the order it was written. This table contains events written out by Deephaven workers and can help diagnose issues.

  1. Save the PQ.

Create your project

This crash course assumes you're on a Linux-based operating system. While the Gradle files are compatible with Windows, the commands will differ.

The Java client is available in two packages: client-barrage and client-flight.

  • The client-barrage package requires at least Java 11 and includes Deephaven's Barrage extension. This allows you to use the full power of the ticking Deephaven engine.
  • The client-flight package is a Java 8 compatible minimal client that lets you access static snapshots of Deephaven tables using the Arrow Flight protocol.

First, create a directory to hold the examples. The rest of the example assumes you're running from this directory. For example:

Gradle setup

To use the Java client, you'll need either the Barrage or Flight Deephaven application JAR files. A convenient way to obtain them is through Gradle.

If you are already familiar with Gradle, you can follow the Gradle instructions to set it up in this directory.

Alternatively, you can download the example Gradle wrapper zip file, copy it to the directory you created earlier, and then unzip it in your example client directory.

Run the Gradle wrapper command to set up the environment.

Application build files

Two Gradle files are used to compile and run the examples. Based on their contents, Gradle automatically determines and downloads the required dependencies.

  • One file is for the Barrage client, and one is for the Flight client.
  • You must update the dhcVersion and dheVersion variables within the Gradle files to reflect the version of Deephaven that you are connecting to.
  • The application and run sections are used with the self-contained Java examples you'll create soon.
    • The Barrage example relies on a class called io.deephaven.enterprise.dnd.client.example.BarrageExample.
    • The Flight example relies on a class called io.deephaven.enterprise.dnd.client.example.FlightExample.

To use these examples with the suggested directories, edit settings.gradle in your project directory. Depending on whether you want to use Barrage or Flight, add one or both of these lines:

Then follow these instructions for Barrage, Flight, or both.

Gradle build details.

Create a directory and create a new build.gradle file in it.

Add the following contents to the file. When adding the text make a couple of changes.

  • Update dhcVersion and dheVersion to the versions on your server. If you start your test PQ, you can see these in the Summary tab.
  • Update /path/to/keyfile with the path to your keyfile.
  • Alternatively, you can use a username and password for testing, although this is insecure.

Note

This example uses port 8000. Your server may use a different port. Typically, the port is 8123 for servers without Envoy and 8000 for servers with Envoy.

Create a directory and create a new build.gradle file in it.

Add the following contents to the file. When adding the text make a couple of changes.

  • Update dheVersion to the version on your server. You can see this by clicking on the User Settings button in the web UI.
  • Update /path/to/keyfile with the path to your keyfile.
  • Alternatively, you can use a username and password for testing, although this is insecure.

Note

This example uses port 8000. Your server may use a different port. Typically, the port is 8123 for servers without Envoy and 8000 for servers with Envoy.

Update the Gradle file's args line (at the bottom of the file) with an appropriate URL, username, password, Persistent Query name, and table name. The PQ name should be the one created earlier (MyTestJavaClientPQ), and the table name should be the one from that PQ's script (pel).

Alternatively, you can use a keyfile that's been set up instead of the username and password. This is the recommended authentication method.

Create the Java client code

Depending on whether you're creating a Barrage or a Flight application, follow the appropriate instructions below to create the Java application. The code contains comments explaining what it's doing.

Java client code
  1. Create the Barrage Java client code.
  1. Copy the following code into the file.
  1. Create the Flight Java client code.
  1. Copy the following code into the file.

Compile and run the code

  1. Compile the code to make sure you've got access to the required libraries.

  2. Use the Gradle file to compile the client.

  1. Run the application.

Before running the application, ensure your PQ isn't running so you can see it start up and guarantee some data in the internal tables that the example retrieves.