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:
QueryResult result = scope.executeQuery("select 1");
result.rows().forEach(row -> System.out.println("Got row: " + row));
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:
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))
);
Query Options
The query service accepts various options to customize your query. The following table lists them all:
| Name | Description |
|---|---|
|
An optional identifier for the query. |
|
Specifies the values to use for named placeholders in the query statement. |
|
Allows setting positional arguments for a parameterized query. |
|
Sets the deserializer used by |
|
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. |
|
Sets a different scan consistency for this query. |
|
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.