-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathQueries.java
More file actions
145 lines (125 loc) · 5.35 KB
/
Queries.java
File metadata and controls
145 lines (125 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (c) 2020 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// NB: This example requires the `travel-sample` bucket to be installed.
// tag::imports[]
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.Scope;
import com.couchbase.client.java.json.JsonArray;
import com.couchbase.client.java.json.JsonObject;
import com.couchbase.client.java.query.QueryResult;
import com.couchbase.client.java.query.QueryScanConsistency;
import com.couchbase.client.java.query.ReactiveQueryResult;
import reactor.core.publisher.Mono;
import java.util.UUID;
import static com.couchbase.client.java.query.QueryOptions.queryOptions;
// end::imports[]
public class Queries {
public static void main(String[] args) throws Exception {
Cluster cluster = Cluster.connect("localhost", "Administrator", "password");
// tag::connect-bucket-and-scope[]
Bucket bucket = cluster.bucket("travel-sample");
Scope scope = bucket.scope("inventory");
// end::connect-bucket-and-scope[]
{
System.out.println("\nExample: [named]");
// tag::named[]
QueryResult result = cluster.query(
"select count(*) from `travel-sample`.inventory.airline where country = $country",
queryOptions().parameters(JsonObject.create().put("country", "France")));
// end::named[]
}
{
System.out.println("\nExample: [positional]");
// tag::positional[]
QueryResult result = cluster.query(
"select count(*) from `travel-sample`.inventory.airline where country = ?",
queryOptions().parameters(JsonArray.from("France")));
// end::positional[]
}
{
System.out.println("\nExample: [scanconsistency]");
// tag::scanconsistency[]
QueryResult result = cluster.query(
"select count(*) from `travel-sample`.inventory.airline where country = 'France'",
queryOptions().scanConsistency(QueryScanConsistency.REQUEST_PLUS));
// end::scanconsistency[]
}
// NOTE: This currently fails with Couchbase Internal Server error.
// Server issue tracked here: https://issues.couchbase.com/browse/MB-46876
// Add back in once Couchbase Server 7.0.1 is available, which will fix this issue.
// {
// System.out.println("\nExample: [scanconsistency_with]");
// // tag::scanconsistency_with[]
// Collection collection = scope.collection("airport");
// MutationResult mr = collection.upsert("someDoc", JsonObject.create().put("name", "roi"));
// MutationState mutationState = MutationState.from(mr.mutationToken().get());
//
// QueryOptions qo = QueryOptions.queryOptions().consistentWith(mutationState);
// QueryResult result = cluster.query("select raw meta().id from `travel-sample`.inventory.airport limit 100", qo);
// // end::scanconsistency_with[]
// }
{
System.out.println("\nExample: [clientcontextid]");
// tag::clientcontextid[]
QueryResult result = cluster.query(
"select count(*) from `travel-sample`.inventory.airline where country = 'France'",
queryOptions().clientContextId("user-44" + UUID.randomUUID()));
// end::clientcontextid[]
}
{
System.out.println("\nExample: [readonly]");
// tag::readonly[]
QueryResult result = cluster.query(
"select count(*) from `travel-sample`.inventory.airline where country = 'France'",
queryOptions().readonly(true));
// end::readonly[]
}
{
System.out.println("\nExample: [printmetrics]");
// tag::printmetrics[]
QueryResult result = cluster.query("select 1=1", queryOptions().metrics(true));
System.err.println("Execution time: " + result.metaData().metrics().get().executionTime());
// end::printmetrics[]
}
{
System.out.println("\nExample: [rowsasobject]");
// tag::rowsasobject[]
QueryResult result = cluster.query("select * from `travel-sample`.inventory.airline limit 10");
for (JsonObject row : result.rowsAsObject()) {
System.out.println("Found row: " + row);
}
// end::rowsasobject[]
}
{
System.out.println("\nExample: [simplereactive]");
// tag::simplereactive[]
Mono<ReactiveQueryResult> result = cluster.reactive().query("select 1=1");
result.flatMapMany(ReactiveQueryResult::rowsAsObject).subscribe(row -> System.out.println("Found row: " + row));
// end::simplereactive[]
}
{
System.out.println("\nExample: [scope-level-query]");
// tag::scope-level-query[]
QueryResult result = scope.query("select * from `airline` where country = $country LIMIT 10",
queryOptions().parameters(JsonObject.create().put("country", "France")));
for (JsonObject row : result.rowsAsObject()) {
System.out.println("Found row: " + row);
}
// end::scope-level-query[]
}
}
}