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.
- 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
1GB.
-
On the Scheduling tab, select
Disabled. -
On the Script tab, select a Runtime of
Groovy. The script consists of one line to retrieve theProcessEventLogtable and view theLogEntrycolumn in the order it was written. This table contains events written out by Deephaven workers and can help diagnose issues.
- 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-barragepackage 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-flightpackage 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
dhcVersionanddheVersionvariables within the Gradle files to reflect the version of Deephaven that you are connecting to. - The
applicationandrunsections 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.
- The Barrage example relies on a class called
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
dhcVersionanddheVersionto the versions on your server. If you start your test PQ, you can see these in the Summary tab. - Update
/path/to/keyfilewith 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
dheVersionto the version on your server. You can see this by clicking on the User Settings button in the web UI. - Update
/path/to/keyfilewith 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
- Create the Barrage Java client code.
- Copy the following code into the file.
- Create the Flight Java client code.
- Copy the following code into the file.
Compile and run the code
-
Compile the code to make sure you've got access to the required libraries.
-
Use the Gradle file to compile the client.
- 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.