|
| 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 io.dapr.client.DaprClient; |
| 17 | +import io.dapr.client.DaprClientBuilder; |
| 18 | +import io.dapr.client.DaprPreviewClient; |
| 19 | +import io.dapr.client.domain.QueryStateItem; |
| 20 | +import io.dapr.client.domain.QueryStateRequest; |
| 21 | +import io.dapr.client.domain.QueryStateResponse; |
| 22 | +import io.dapr.client.domain.State; |
| 23 | +import io.dapr.client.domain.TransactionalStateOperation; |
| 24 | +import io.dapr.client.domain.query.Query; |
| 25 | +import io.dapr.client.domain.query.Sorting; |
| 26 | +import io.dapr.client.domain.query.filters.EqFilter; |
| 27 | +import io.dapr.exceptions.DaprException; |
| 28 | +import io.grpc.Status; |
| 29 | +import reactor.core.publisher.Mono; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Objects; |
| 35 | + |
| 36 | +/** |
| 37 | + * 1. Build and install jars: |
| 38 | + * mvn clean install |
| 39 | + * 2. cd [repo root]/examples |
| 40 | + * 3. send a message to be saved as state: |
| 41 | + * dapr run --components-path ./components/state -- \ |
| 42 | + * java -Ddapr.api.protocol=HTTP -jar target/dapr-java-sdk-examples-exec.jar \ |
| 43 | + * io.dapr.examples.querystate.QuerySavedState 'my message' |
| 44 | + */ |
| 45 | +public class QuerySavedState { |
| 46 | + |
| 47 | + public static class MyData { |
| 48 | + |
| 49 | + /// Gets or sets the value for PropertyA. |
| 50 | + private String propertyA; |
| 51 | + |
| 52 | + /// Gets or sets the value for PropertyB. |
| 53 | + private String propertyB; |
| 54 | + |
| 55 | + public String getPropertyB() { |
| 56 | + return propertyB; |
| 57 | + } |
| 58 | + |
| 59 | + public void setPropertyB(String propertyB) { |
| 60 | + this.propertyB = propertyB; |
| 61 | + } |
| 62 | + |
| 63 | + public String getPropertyA() { |
| 64 | + return propertyA; |
| 65 | + } |
| 66 | + |
| 67 | + public void setPropertyA(String propertyA) { |
| 68 | + this.propertyA = propertyA; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public String toString() { |
| 73 | + return "MyData{" |
| 74 | + + "propertyA='" + propertyA + '\'' |
| 75 | + + ", propertyB='" + propertyB + '\'' + '}'; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean equals(Object o) { |
| 80 | + if (this == o) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + if (o == null || getClass() != o.getClass()) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + MyData myData = (MyData) o; |
| 87 | + return Objects.equals(propertyA, myData.propertyA) |
| 88 | + && Objects.equals(propertyB, myData.propertyB); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public int hashCode() { |
| 93 | + return Objects.hash(propertyA, propertyB); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static final String STATE_STORE_NAME = "mongo-statestore"; |
| 98 | + |
| 99 | + private static final String FIRST_KEY_NAME = "key1"; |
| 100 | + |
| 101 | + private static final String SECOND_KEY_NAME = "key2"; |
| 102 | + |
| 103 | + private static final String THIRD_KEY_NAME = "key3"; |
| 104 | + |
| 105 | + /** |
| 106 | + * Executes the sate actions. |
| 107 | + * @param args messages to be sent as state value. |
| 108 | + */ |
| 109 | + public static void main(String[] args) throws Exception { |
| 110 | + DaprClientBuilder builder = new DaprClientBuilder(); |
| 111 | + try (DaprClient client = builder.build(); DaprPreviewClient previewClient = builder.buildPreviewClient()) { |
| 112 | + System.out.println("Waiting for Dapr sidecar ..."); |
| 113 | + client.waitForSidecar(10000).block(); |
| 114 | + System.out.println("Dapr sidecar is ready."); |
| 115 | + |
| 116 | + String searchVal = args.length == 0 ? "searchValue" : args[0]; |
| 117 | + |
| 118 | + MyData dataQueried = new MyData(); |
| 119 | + dataQueried.setPropertyA(searchVal); |
| 120 | + dataQueried.setPropertyB("query"); |
| 121 | + MyData dataNotQueried = new MyData(); |
| 122 | + dataNotQueried.setPropertyA("no query"); |
| 123 | + dataNotQueried.setPropertyB("no query"); |
| 124 | + |
| 125 | + |
| 126 | + System.out.println("Insert key: " + FIRST_KEY_NAME + ", data: " + dataQueried); |
| 127 | + client.saveState(STATE_STORE_NAME, FIRST_KEY_NAME, dataQueried).block(); |
| 128 | + System.out.println("Insert key: " + SECOND_KEY_NAME + ", data: " + dataQueried); |
| 129 | + client.saveState(STATE_STORE_NAME, SECOND_KEY_NAME, dataQueried).block(); |
| 130 | + System.out.println("Insert key: " + THIRD_KEY_NAME + ", data: " + dataNotQueried); |
| 131 | + client.saveState(STATE_STORE_NAME, THIRD_KEY_NAME, dataNotQueried).block(); |
| 132 | + Query query = new Query().setFilter(new EqFilter<>("propertyA", searchVal)) |
| 133 | + .setSort(Arrays.asList(new Sorting("propertyB", Sorting.Order.ASC))); |
| 134 | + |
| 135 | + QueryStateRequest request = new QueryStateRequest(STATE_STORE_NAME) |
| 136 | + .setQuery(query); |
| 137 | + |
| 138 | + |
| 139 | + QueryStateResponse<MyData> result = previewClient.queryState(request, MyData.class).block(); |
| 140 | + |
| 141 | + System.out.println("Found " + result.getResults().size() + " items."); |
| 142 | + for (QueryStateItem<MyData> item : result.getResults()) { |
| 143 | + System.out.println("Key: " + item.getKey()); |
| 144 | + System.out.println("Data: " + item.getValue()); |
| 145 | + } |
| 146 | + |
| 147 | + // This is an example, so for simplicity we are just exiting here. |
| 148 | + // Normally a dapr app would be a web service and not exit main. |
| 149 | + System.out.println("Done"); |
| 150 | + System.exit(0); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments