Connecting to Crate with Java

Java Client

Deprecation Notice

The Java client is deprecated in versions of Crate 0.57.0 and will be removed by 0.58.0 and later. Instead the recommended way to access Crate from Java is via our JDBC driver.

Add Crate to you Project

A simple way to configure your project to connect to a Crate cluster is to visit the Java client page on Bintray and click the Set Me Up button. This will give you a Maven or Gradle configuration that can be pasted into your current project or downloaded for a new project.

For manual configuration follow these steps depending on your build system of choice.

Maven

Add the following to your pom.xml file:

...
<repositories>
  ...
  <repository>
      <snapshots>
          <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>bintray</name>
      <url>http://dl.bintray.com/crate/crate</url>
  </repository>
</repositories>
...
<dependencies>
  ...
  <dependency>
      <groupId>io.crate</groupId>
      <artifactId>crate-client</artifactId>
      <version>VERSION</version>
  </dependency>
</dependencies>
...

Gradle

Add Crate as a dependency to your Gradle file:

repositories {
  ...
  jcenter()
}

dependencies {
  compile 'io.crate:crate-client:VERSION
  ...
}

Connect to Crate

Import the Crate client and create a new client object:

import io.crate.client.CrateClient;
...
CrateClient client = new CrateClient(
    "NODE1_IP:4300",
    "NODE2_IP:4300"
    ...
);

Whilst it's possible to just connect to one node in a cluster, we recommend specifying all nodes to allow for automatic routing to another node if one becomes available.

Want to Know More?

  • More detailed documentation on the Crate Java client is available here.

Next Steps