Querying with SQL++

      +
      You can query for documents in Couchbase using the SQL++ query language, a language based on SQL, but designed for structured and flexible JSON documents.

      On this page we dive straight into using the Query Service API from the Java Analytics SDK. For a deeper look at the concepts, to help you better understand the Query Service, and the SQL++ language, see the links in the Further Information section at the end of this page.

      Here we show queries against the Travel Sample collection, at cluster and scope level, and give links to information on adding other collections to your data.

      Before You Start

      This page assumes that you have installed the Java Analytics SDK, and created an Enterprise Analytics cluster.

      Create a collection to work upon by importing the travel-sample dataset into your cluster.

      Querying Your Dataset

      Execute a query and buffer all result rows in client memory:

      Scope Level
      QueryResult result = scope.executeQuery("select 1");
      result.rows().forEach(row -> System.out.println("Got row: " + row));
      Cluster Level
      QueryResult result = cluster.executeQuery("select 1");
      result.rows().forEach(row -> System.out.println("Got row: " + row));

      Positional and Named Parameters

      Supplying parameters as individual arguments to the query allows the query engine to optimize the parsing and planning of the query. You can either supply these parameters by name or by position.

      Execute a streaming query with positional arguments:

      Positional Parameters
      cluster.executeStreamingQuery(
        "select ?=1",
        row -> System.out.println("Got row: " + row),
        options -> options
          .parameters(List.of(1))
      );

      Execute a streaming query with named arguments:

      Named Parameters
      cluster.executeStreamingQuery(
        "select $foo=1",
        row -> System.out.println("Got row: " + row),
        options -> options
          .parameters(Map.of("foo", 1))
      );

      Query Options

      The query service accepts various options to customize your query. The following table lists them all:

      Table 1. Available Query Options
      Name Description

      clientContextId(@Nullable String clientContextId)

      An optional identifier for the query.

      parameters(@Nullable Map<String,?> namedParameters)

      Specifies the values to use for named placeholders in the query statement.

      parameters(@Nullable List<?> positionalParameters)

      Allows setting positional arguments for a parameterized query.

      deserializer(@Nullable Deserializer deserializer)

      Sets the deserializer used by Row.as(java.lang.Class<T>) to convert query result rows into Java objects. If not specified, defaults to the cluster’s default deserializer.

      scanWait(@Nullable Duration scanWait)

      Allows customizing how long the query engine is willing to wait until the index catches up to whatever scan consistency is asked for in this query.

      scanConsistency(@Nullable ScanConsistency scanConsistency)

      Sets a different scan consistency for this query.

      readOnly(@Nullable Boolean readOnly)

      Specifies that this query should be executed in read-only mode, disabling the ability for the query to make any changes to the data.

      Further Information

      The SQL++ for Analytics Reference offers a complete guide to the SQL++ language for both of our analytics services, including all of the latest additions.