Skip to content

Commit 085740e

Browse files
Merge pull request #368 from couchbase/DOC-10839
Fixed DOC-10839
2 parents da00b7c + d5c344a commit 085740e

File tree

6 files changed

+123
-29
lines changed

6 files changed

+123
-29
lines changed

modules/howtos/examples/Queries.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,6 @@ public static void main(String[] args) throws Exception {
5151
Scope scope = bucket.scope("inventory");
5252
// end::connect-bucket-and-scope[]
5353

54-
{
55-
System.out.println("Example: [simple]");
56-
// tag::simple[]
57-
try {
58-
final QueryResult result = cluster.query("select * from `travel-sample`.inventory.airline limit 100",
59-
queryOptions().metrics(true));
60-
61-
for (JsonObject row : result.rowsAsObject()) {
62-
System.out.println("Found row: " + row);
63-
}
64-
65-
System.out.println("Reported execution time: " + result.metaData().metrics().get().executionTime());
66-
} catch (CouchbaseException ex) {
67-
ex.printStackTrace();
68-
}
69-
// end::simple[]
70-
}
71-
7254
{
7355
System.out.println("\nExample: [named]");
7456
// tag::named[]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import static com.couchbase.client.java.query.QueryOptions.queryOptions;
2+
3+
import com.couchbase.client.core.error.CouchbaseException;
4+
import com.couchbase.client.java.Cluster;
5+
import com.couchbase.client.java.json.JsonObject;
6+
import com.couchbase.client.java.query.QueryResult;
7+
8+
// tag::class[]
9+
public class SimpleQuery {
10+
// Update these variables to point to your Couchbase instance and credentials.
11+
static String connectionString = "localhost";
12+
static String username = "Administrator";
13+
static String password = "password";
14+
15+
public static void main(String[] args) throws Exception {
16+
Cluster cluster = Cluster.connect(connectionString, username, password);
17+
18+
{
19+
try {
20+
final QueryResult result = cluster.query("select * from `travel-sample`.inventory.airline limit 100",
21+
queryOptions().metrics(true));
22+
23+
for (JsonObject row : result.rowsAsObject()) {
24+
System.out.println("Found row: " + row);
25+
}
26+
27+
System.out.println("Reported execution time: " + result.metaData().metrics().get().executionTime());
28+
} catch (CouchbaseException ex) {
29+
ex.printStackTrace();
30+
}
31+
}
32+
}
33+
}
34+
// end::class[]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import static com.couchbase.client.java.query.QueryOptions.queryOptions;
2+
3+
import com.couchbase.client.core.error.CouchbaseException;
4+
import com.couchbase.client.java.*;
5+
import com.couchbase.client.java.json.*;
6+
import com.couchbase.client.java.query.*;
7+
8+
// tag::class[]
9+
public class SimpleQueryCloud {
10+
// Update these variables to point to your Couchbase Capella instance and credentials.
11+
static String connectionString = "couchbases://cb.<your-endpoint-here>.cloud.couchbase.com";
12+
static String username = "Administrator";
13+
static String password = "password";
14+
15+
public static void main(String[] args) throws Exception {
16+
Cluster cluster = Cluster.connect(
17+
connectionString,
18+
ClusterOptions.clusterOptions(username, password).environment(env -> {
19+
env.applyProfile("wan-development");
20+
})
21+
);
22+
23+
{
24+
try {
25+
final QueryResult result = cluster.query("select * from `travel-sample`.inventory.airline limit 100",
26+
queryOptions().metrics(true));
27+
28+
for (JsonObject row : result.rowsAsObject()) {
29+
System.out.println("Found row: " + row);
30+
}
31+
32+
System.out.println("Reported execution time: " + result.metaData().metrics().get().executionTime());
33+
34+
} catch (CouchbaseException ex) {
35+
ex.printStackTrace();
36+
}
37+
}
38+
}
39+
}
40+
// end::class[]

modules/howtos/pages/n1ql-queries-with-sdk.adoc

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
= Query
2-
:description: You can query for documents in Couchbase using the {sqlpp_url}[{sqlpp} (formerly N1QL)] query language, a language based on SQL, but designed for structured and flexible JSON documents.
2+
:description: You can query for documents in Couchbase using the {sqlpp_url}[{sqlpp}] (formerly N1QL) query language, a language based on SQL, but designed for structured and flexible JSON documents.
33
:page-topic-type: howto
44
:page-aliases: n1ql-query,ROOT:querying-n1ql
55
:page-pagination: full
@@ -23,10 +23,33 @@ include::example$Queries.java[tag=imports,indent=0]
2323

2424
Here's a complete example of doing a query and handling the results:
2525

26+
[{tabs}]
27+
====
28+
Couchbase Capella Sample::
29+
+
30+
--
31+
These examples requires the Travel Sample Bucket.
32+
The Couchbase Capella free trial version comes with this bucket, and its Query indexes, loaded and ready.
33+
34+
[source,java]
35+
----
36+
include::example$SimpleQueryCloud.java[tag=class,indent=0]
37+
----
38+
--
39+
40+
Local Couchbase Server::
41+
+
42+
--
43+
To run these examples, you will need to install the Travel Sample Bucket
44+
using either the xref:{version-server}@server:manage:manage-settings/install-sample-buckets.adoc#install-sample-buckets-with-the-ui[Web interface]
45+
or the xref:{version-server}@server:manage:manage-settings/install-sample-buckets.adoc#install-sample-buckets-with-the-cli[command line].
46+
2647
[source,java]
2748
----
28-
include::example$Queries.java[tag=simple,indent=0]
49+
include::example$SimpleQuery.java[tag=class,indent=0]
2950
----
51+
--
52+
====
3053

3154
Let's break it down. A query is always performed at the `Cluster` level, using the `query` method. It takes the statement as a required argument and then allows to provide additional options if needed (in the example above, no options are specified).
3255

@@ -156,7 +179,7 @@ include::example$Queries.java[tag=scanconsistency,indent=0]
156179
//If you need "global" scan consistency, use `QueryScanConsistency.REQUEST_PLUS` on `scanConsistency(QueryScanConsistency)`.
157180
158181
159-
=== Client Context Id
182+
=== Client Context ID
160183
161184
The SDK will always send a client context ID with each query, even if none is provided by the user.
162185
By default a UUID will be generated that is mirrored back from the query engine and can be used for debugging purposes.

modules/student/examples/AddEnrollments.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.couchbase.client.java.json.JsonObject;
88
import com.couchbase.client.java.query.QueryOptions;
99
import com.couchbase.client.java.query.QueryResult;
10+
import com.couchbase.client.java.query.QueryScanConsistency;
1011

1112
import java.time.Duration;
1213
import java.time.LocalDate;
@@ -54,25 +55,27 @@ public static void main(String[] args) {
5455

5556
private static JsonObject retrieveStudent(Cluster cluster, String name) throws CouchbaseException {
5657

58+
QueryOptions studentQueryOptions = QueryOptions.queryOptions();
59+
studentQueryOptions.parameters(JsonObject.create().put("name", name));
60+
studentQueryOptions.scanConsistency(QueryScanConsistency.REQUEST_PLUS);
61+
5762
final QueryResult result = cluster.query("select META().id, src.* " +
5863
"from `student-bucket`.`art-school-scope`.`student-record-collection` src " +
59-
"where src.`name` = $name",
60-
QueryOptions.queryOptions()
61-
.parameters(JsonObject.create()
62-
.put("name", name)));
64+
"where src.`name` = $name", studentQueryOptions);
6365

6466
return result.rowsAsObject().get(0);
6567

6668
}
6769

6870
private static JsonObject retrieveCourse(Cluster cluster, String course) throws CouchbaseException {
6971

72+
QueryOptions courseQueryOptions = QueryOptions.queryOptions();
73+
courseQueryOptions.parameters(JsonObject.create().put("courseName", course));
74+
courseQueryOptions.scanConsistency(QueryScanConsistency.REQUEST_PLUS);
75+
7076
final QueryResult result = cluster.query("select META().id, crc.* " +
7177
"from `student-bucket`.`art-school-scope`.`course-record-collection` crc " +
72-
"where crc.`course-name` = $courseName",
73-
QueryOptions.queryOptions()
74-
.parameters(JsonObject.create()
75-
.put("courseName", course)));
78+
"where crc.`course-name` = $courseName", courseQueryOptions);
7679

7780
return result.rowsAsObject().get(0);
7881

modules/test/test-howtos.bats

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ load 'test_helper'
112112
assert_output --partial "id='hotel_26223'"
113113
}
114114

115+
@test "[howtos] - SimpleQuery.java" {
116+
runExample SimpleQuery
117+
assert_success
118+
}
119+
120+
@test "[howtos] - SimpleQueryCloud.java" {
121+
skip "Example requires a cloud endpoint. Unable to run."
122+
123+
runExample SimpleQueryCloud
124+
assert_success
125+
}
126+
115127
@test "[howtos] - SubDocument.java" {
116128
runExample SubDocument
117129
assert_success

0 commit comments

Comments
 (0)