Hello Columnar — Java SDK Quickstart Guide

      +
      Install, connect, try. A quick start guide to get you up and running with Columnar and the Java Columnar SDK.

      Before You Start

      Sign up for a Capella Free Tier, and choose a Columnar cluster.

      After creating the cluster, add your IP address to the list of allowed IP addresses.

      Minimum Java Version

      The Java Columnar SDK requires Java 17 or later. We recommend using the most recent long-term support (LTS) version of OpenJDK.

      Remember to keep your Java installation up to date with the latest patches.

      Maven Project Template

      The SDK’s source code repository includes an example Maven project you can copy to get started quickly.

      Adding the SDK to an Existing Project

      Declare a dependency on the SDK using its Maven Coordinates.

      To see log messages from the SDK, include an SLF4J binding in your project.

      Connecting and Executing a Query

      import com.couchbase.columnar.client.java.Cluster;
      import com.couchbase.columnar.client.java.Credential;
      import com.couchbase.columnar.client.java.QueryResult;
      
      import java.util.List;
      
      public class Example {
        public static void main(String[] args) {
          var connectionString = "couchbases://...";
          var username = "...";
          var password = "...";
      
          try (Cluster cluster = Cluster.newInstance(
            connectionString,
            Credential.of(username, password),
            // The third parameter is optional.
            // This example sets the default query timeout to 2 minutes.
            clusterOptions -> clusterOptions
              .timeout(it -> it.queryTimeout(Duration.ofMinutes(2)))
          )) {
      
            // Execute a query and buffer all result rows in client memory.
            QueryResult result = cluster.executeQuery("select 1");
            result.rows().forEach(row -> System.out.println("Got row: " + row));
      
            // Execute a query and process rows as they arrive from server.
            cluster.executeStreamingQuery(
                "select 1",
                row -> System.out.println("Got row: " + row)
            );
      
            // Execute a streaming query with positional arguments.
            cluster.executeStreamingQuery(
              "select ?=1",
              row -> System.out.println("Got row: " + row),
              options -> options
                .parameters(List.of(1))
            );
      
            // Execute a streaming query with named arguments.
            cluster.executeStreamingQuery(
              "select $foo=1",
              row -> System.out.println("Got row: " + row),
              options -> options
                .parameters(Map.of("foo", 1))
            );
          }
        }
      }