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 Columnar 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 Columnar SDK, added your IP address to the allowlist, and created a Columnar 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))
      );

      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.