-
Notifications
You must be signed in to change notification settings - Fork 16
/
Ex09View.java
159 lines (116 loc) · 5.72 KB
/
Ex09View.java
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.couchbase.devday;
import com.couchbase.client.CouchbaseClient;
import com.couchbase.client.protocol.views.*;
import com.google.gson.Gson;
import net.spy.memcached.PersistTo;
import net.spy.memcached.ReplicateTo;
import net.spy.memcached.internal.OperationFuture;
import java.net.URI;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class Ex09View {
public static void main(String[] args) {
System.out.println("--------------------------------------------------------------------------");
System.out.println("\tCouchbase - Views");
System.out.println("--------------------------------------------------------------------------");
List<URI> uris = new LinkedList<URI>();
uris.add(URI.create("http://127.0.0.1:8091/pools"));
uris.add(URI.create("http://127.0.0.1:8091/pools"));
CouchbaseClient cb = null;
try {
cb = new CouchbaseClient(uris, "beer-sample", "");
Gson json = new Gson();
DesignDocument designDocument = cb.getDesignDoc("beer");
List<ViewDesign> views = designDocument.getViews();
for (ViewDesign view : views) {
System.out.println(view.getName());
System.out.println("\t" + view.getMap());
}
// create a new Document and view
DesignDocument breweryView = new DesignDocument("brewery");
String mapFunction = "function (doc, meta) {\n" +
" if (doc.type == \"brewery\") {\n" +
" \temit(doc.name);\n" +
" }\n" +
"}";
ViewDesign viewDesign = new ViewDesign("by_name", mapFunction);
breweryView.setView(viewDesign);
cb.createDesignDoc(breweryView);
{
System.out.println("--------------------------------------------------------------------------");
System.out.println("Breweries (by_name)");
View view = cb.getView("brewery", "by_name");
Query query = new Query();
query.setLimit(10);
ViewResponse viewResponse = cb.query(view, query);
for (ViewRow row : viewResponse) {
System.out.println(row.getKey());
}
System.out.println("");
}
{
System.out.println("--------------------------------------------------------------------------");
System.out.println("Breweries (by_name) with docs");
View view = cb.getView("brewery", "by_name");
Query query = new Query();
query.setLimit(10);
query.setIncludeDocs(true);
ViewResponse viewResponse = cb.query(view, query);
for (ViewRow row : viewResponse) {
System.out.println(row.getKey() + " | "+ row.getDocument());
}
System.out.println("");
}
{
System.out.println("--------------------------------------------------------------------------");
System.out.println("Breweries (by_name) with docs & JSON parsing");
View view = cb.getView("brewery", "by_name");
Query query = new Query();
query.setLimit(10);
query.setIncludeDocs(true);
ViewResponse viewResponse = cb.query(view, query);
for (ViewRow row : viewResponse) {
HashMap<String, String> parsedDoc = json.fromJson((String)row.getDocument(), HashMap.class);
System.out.println(row.getKey() + " : "+ parsedDoc.get("country") );
}
System.out.println("");
}
{
System.out.println("--------------------------------------------------------------------------");
System.out.println("Breweries (by_name) starting with letter y or Y");
View view = cb.getView("brewery", "by_name");
Query query = new Query();
query.setRangeStart("y");
query.setRangeEnd("z");
query.setIncludeDocs(true);
ViewResponse viewResponse = cb.query(view, query);
for (ViewRow row : viewResponse) {
HashMap<String, String> parsedDoc = json.fromJson((String)row.getDocument(), HashMap.class);
System.out.println(row.getKey() + " : "+ parsedDoc.get("country") );
}
System.out.println("");
}
{
System.out.println("--------------------------------------------------------------------------");
System.out.println("Breweries (by_name) starting with letter y only, no Y (results should be empty)");
View view = cb.getView("brewery", "by_name");
Query query = new Query();
query.setRangeStart("y");
query.setRangeEnd("Y");
query.setIncludeDocs(true);
ViewResponse viewResponse = cb.query(view, query);
for (ViewRow row : viewResponse) {
HashMap<String, String> parsedDoc = json.fromJson((String)row.getDocument(), HashMap.class);
System.out.println(row.getKey() + " : "+ parsedDoc.get("country") );
}
System.out.println("");
}
System.out.println("\n\n");
cb.shutdown(10, TimeUnit.SECONDS);
} catch (Exception e) {
System.err.println("Error connecting to Couchbase: " + e.getMessage());
}
}
}