Skip to content

Commit 06d92da

Browse files
Query State Preview API implementation. (dapr#697)
* Query State Preview API implementation. Signed-off-by: Mukundan Sundararajan <[email protected]> * Use latest dapr ref and fix grpc query state api Signed-off-by: Mukundan Sundararajan <[email protected]> * fix service invocation automatic unesacpe Signed-off-by: Mukundan Sundararajan <[email protected]> * add more unit tests Signed-off-by: Mukundan Sundararajan <[email protected]> * Add query state API docs Signed-off-by: Mukundan Sundararajan <[email protected]> * Fix example to be user friendly Signed-off-by: Mukundan Sundararajan <[email protected]> * Fix example in docs Signed-off-by: Mukundan Sundararajan <[email protected]> * make pagination immutable Signed-off-by: Mukundan Sundararajan <[email protected]> Co-authored-by: Artur Souza <[email protected]>
1 parent e3fa24f commit 06d92da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2559
-125
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
matrix:
2121
java: [ 11, 13, 15, 16 ]
2222
env:
23-
GOVER: 1.15.0
23+
GOVER: 1.17.7
2424
GOOS: linux
2525
GOARCH: amd64
2626
GOPROXY: https://proxy.golang.org
@@ -29,7 +29,7 @@ jobs:
2929
DAPR_RUNTIME_VER: 1.6.0-rc.2
3030
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.6.0-rc.1/install/install.sh
3131
DAPR_CLI_REF:
32-
DAPR_REF:
32+
DAPR_REF: 5a307f3deaa1b322f7945179adad0403de80eb7e
3333
steps:
3434
- uses: actions/checkout@v3
3535
- name: Set up OpenJDK ${{ env.JDK_VER }}
@@ -91,6 +91,10 @@ jobs:
9191
run: |
9292
docker-compose -f ./sdk-tests/deploy/local-test-vault.yml up -d
9393
docker ps
94+
- name: Install Local mongo database using docker-compose
95+
run: |
96+
docker-compose -f ./sdk-tests/deploy/local-test-mongo.yml up -d
97+
docker ps
9498
- name: Clean up files
9599
run: mvn clean
96100
- name: Build sdk

.github/workflows/validate.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
matrix:
3232
java: [ 11, 13, 15, 16 ]
3333
env:
34-
GOVER: 1.15.0
34+
GOVER: 1.17.7
3535
GOOS: linux
3636
GOARCH: amd64
3737
GOPROXY: https://proxy.golang.org
@@ -40,7 +40,7 @@ jobs:
4040
DAPR_RUNTIME_VER: 1.6.0-rc.2
4141
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/v1.6.0-rc.1/install/install.sh
4242
DAPR_CLI_REF:
43-
DAPR_REF:
43+
DAPR_REF: 5a307f3deaa1b322f7945179adad0403de80eb7e
4444
steps:
4545
- uses: actions/checkout@v3
4646
- name: Set up OpenJDK ${{ env.JDK_VER }}
@@ -108,6 +108,10 @@ jobs:
108108
sudo apt-get install vault
109109
# Verify vault is installed
110110
vault -h
111+
- name: Install Local mongo database using docker-compose
112+
run: |
113+
docker-compose -f ./sdk-tests/deploy/local-test-mongo.yml up -d
114+
docker ps
111115
- name: Clean up files
112116
run: mvn clean
113117
- name: Build sdk
@@ -153,4 +157,8 @@ jobs:
153157
- name: Validate Configuration API example
154158
working-directory: ./examples
155159
run: |
156-
mm.py ./src/main/java/io/dapr/examples/configuration/grpc/README.md
160+
mm.py ./src/main/java/io/dapr/examples/configuration/grpc/README.md
161+
- name: Validate query state HTTP example
162+
working-directory: ./examples
163+
run: |
164+
mm.py ./src/main/java/io/dapr/examples/querystate/README.md

daprdocs/content/en/java-sdk-docs/_index.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ public interface DemoActor {
241241

242242
### Get & Subscribe to application configurations
243243

244+
> Note this is a preview API and thus will only be accessible via the DaprPreviewClient interface and not the normal DaprClient interface
245+
244246
```java
245247
import io.dapr.client.DaprClientBuilder;
246248
import io.dapr.client.DaprPreviewClient;
@@ -267,5 +269,76 @@ try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient())
267269
- For a full list of configuration operations visit [How-To: Manage configuration from a store]({{< ref howto-manage-configuration.md >}}).
268270
- Visit [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/configuration) for code samples and instructions to try out different configuration operations.
269271

272+
### Query saved state
273+
274+
> Note this is a preview API and thus will only be accessible via the DaprPreviewClient interface and not the normal DaprClient interface
275+
276+
```java
277+
import io.dapr.client.DaprClient;
278+
import io.dapr.client.DaprClientBuilder;
279+
import io.dapr.client.DaprPreviewClient;
280+
import io.dapr.client.domain.QueryStateItem;
281+
import io.dapr.client.domain.QueryStateRequest;
282+
import io.dapr.client.domain.QueryStateResponse;
283+
import io.dapr.client.domain.query.Query;
284+
import io.dapr.client.domain.query.Sorting;
285+
import io.dapr.client.domain.query.filters.EqFilter;
286+
287+
try (DaprClient client = builder.build(); DaprPreviewClient previewClient = builder.buildPreviewClient()) {
288+
String searchVal = args.length == 0 ? "searchValue" : args[0];
289+
290+
// Create JSON data
291+
Listing first = new Listing();
292+
first.setPropertyType("apartment");
293+
first.setId("1000");
294+
...
295+
Listing second = new Listing();
296+
second.setPropertyType("row-house");
297+
second.setId("1002");
298+
...
299+
Listing third = new Listing();
300+
third.setPropertyType("apartment");
301+
third.setId("1003");
302+
...
303+
Listing fourth = new Listing();
304+
fourth.setPropertyType("apartment");
305+
fourth.setId("1001");
306+
...
307+
Map<String, String> meta = new HashMap<>();
308+
meta.put("contentType", "application/json");
309+
310+
// Save state
311+
SaveStateRequest request = new SaveStateRequest(STATE_STORE_NAME).setStates(
312+
new State<>("1", first, null, meta, null),
313+
new State<>("2", second, null, meta, null),
314+
new State<>("3", third, null, meta, null),
315+
new State<>("4", fourth, null, meta, null)
316+
);
317+
client.saveBulkState(request).block();
318+
319+
320+
// Create query and query state request
321+
322+
Query query = new Query()
323+
.setFilter(new EqFilter<>("propertyType", "apartment"))
324+
.setSort(Arrays.asList(new Sorting("id", Sorting.Order.DESC)));
325+
QueryStateRequest request = new QueryStateRequest(STATE_STORE_NAME)
326+
.setQuery(query);
327+
328+
// Use preview client to call query state API
329+
QueryStateResponse<MyData> result = previewClient.queryState(request, MyData.class).block();
330+
331+
// View Query state response
332+
System.out.println("Found " + result.getResults().size() + " items.");
333+
for (QueryStateItem<Listing> item : result.getResults()) {
334+
System.out.println("Key: " + item.getKey());
335+
System.out.println("Data: " + item.getValue());
336+
}
337+
}
338+
339+
```
340+
- For a full list of configuration operations visit [How-To: Query state]({{< ref howto-state-query-api.md >}}).
341+
- Visit [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/querystate) for complete code sample.
342+
270343
## Related links
271344
- [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: mongo-statestore
5+
spec:
6+
type: state.mongodb
7+
version: v1
8+
metadata:
9+
- name: host
10+
value: localhost:27017
11+
- name: databaseName
12+
value: local
13+
- name: collectionName
14+
value: propertyCollection

examples/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<dependency>
6262
<groupId>com.github.os72</groupId>
6363
<artifactId>protoc-jar-maven-plugin</artifactId>
64-
<version>3.10.1</version>
64+
<version>3.11.4</version>
6565
</dependency>
6666
<dependency>
6767
<groupId>org.springframework.boot</groupId>
@@ -130,7 +130,7 @@
130130
<plugin>
131131
<groupId>com.github.os72</groupId>
132132
<artifactId>protoc-jar-maven-plugin</artifactId>
133-
<version>3.10.1</version>
133+
<version>3.11.4</version>
134134
<executions>
135135
<execution>
136136
<phase>generate-sources</phase>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2021 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.querystate;
15+
16+
import com.fasterxml.jackson.annotation.JsonProperty;
17+
18+
import java.util.Objects;
19+
20+
public class Listing {
21+
22+
@JsonProperty
23+
private String propertyType;
24+
25+
@JsonProperty
26+
private String id;
27+
28+
@JsonProperty
29+
private String city;
30+
31+
@JsonProperty
32+
private String state;
33+
34+
public Listing() {
35+
}
36+
37+
public String getPropertyType() {
38+
return propertyType;
39+
}
40+
41+
public void setPropertyType(String propertyType) {
42+
this.propertyType = propertyType;
43+
}
44+
45+
public String getId() {
46+
return id;
47+
}
48+
49+
public void setId(String id) {
50+
this.id = id;
51+
}
52+
53+
public String getCity() {
54+
return city;
55+
}
56+
57+
public void setCity(String city) {
58+
this.city = city;
59+
}
60+
61+
public String getState() {
62+
return state;
63+
}
64+
65+
public void setState(String state) {
66+
this.state = state;
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return "Listing{"
72+
+ "propertyType='" + propertyType + '\''
73+
+ ", id=" + id
74+
+ ", city='" + city + '\''
75+
+ ", state='" + state + '\''
76+
+ '}';
77+
}
78+
79+
@Override
80+
public boolean equals(Object o) {
81+
if (this == o) {
82+
return true;
83+
}
84+
if (o == null || getClass() != o.getClass()) {
85+
return false;
86+
}
87+
Listing listing = (Listing) o;
88+
return id == listing.id
89+
&& propertyType.equals(listing.propertyType)
90+
&& Objects.equals(city, listing.city)
91+
&& Objects.equals(state, listing.state);
92+
}
93+
94+
@Override
95+
public int hashCode() {
96+
return Objects.hash(propertyType, id, city, state);
97+
}
98+
}

0 commit comments

Comments
 (0)