|
| 1 | +# Query state Example |
| 2 | +To run this example, the default local redis state store will not work as it does not support redis-json server. You will encounter the following error |
| 3 | +``` |
| 4 | + GrpcError(GrpcError { _status: Status { code: Internal, message: "failed query in state store statestore: redis-json server support is required for query capability", metadata: MetadataMap { headers: {"content-type": "application/grpc", "grpc-trace-bin": "AABniqIo9TrSF6TepfB0yzgNAZzAwpG45zK0AgE"} }, source: None } }) |
| 5 | +``` |
| 6 | + |
| 7 | +See [Querying JSON objects(optional)](https://docs.dapr.io/reference/components-reference/supported-state-stores/setup-redis/#querying-json-objects-optional) for creation of a redis instance that supports querying json objects. |
| 8 | + |
| 9 | +For this example, we will be following the query state example in the [Dapr docs](https://docs.dapr.io/developing-applications/building-blocks/state-management/howto-state-query-api/#example-data-and-query) and will be using mongo instead. |
| 10 | + |
| 11 | +To setup MongoDB, execute the following command: |
| 12 | +<!-- STEP |
| 13 | +name: Run mongodb instance |
| 14 | +background: false |
| 15 | +sleep: 10 |
| 16 | +timeout_seconds: 30 |
| 17 | +--> |
| 18 | +```bash |
| 19 | +docker run -d --rm -p 27017:27017 --name mongodb mongo:5 |
| 20 | +``` |
| 21 | +<!-- END_STEP --> |
| 22 | + |
| 23 | +You can then apply the statestore configuration using the `statestore/mongodb.yaml` file. |
| 24 | + |
| 25 | +Then, execute the following commands to populate the state data in the statestore: |
| 26 | + |
| 27 | +<!-- STEP |
| 28 | +name: Populate state store step 1/2 |
| 29 | +background: true |
| 30 | +sleep: 5 |
| 31 | +timeout_seconds: 10 |
| 32 | +--> |
| 33 | +```bash |
| 34 | +dapr run --app-id demo --dapr-http-port 3500 --resources-path statestore/ |
| 35 | +``` |
| 36 | +<!-- END_STEP --> |
| 37 | + |
| 38 | +In a new terminal, apply the test data: |
| 39 | + |
| 40 | +<!-- STEP |
| 41 | +name: Populate state store step 2/2 |
| 42 | +background: false |
| 43 | +sleep: 2 |
| 44 | +timeout_seconds: 5 |
| 45 | +--> |
| 46 | +```bash |
| 47 | +curl -X POST -H "Content-Type: application/json" http://localhost:3500/v1.0/state/statestore -d @./statestore/dataset.json |
| 48 | +`````` |
| 49 | +<!-- END_STEP --> |
| 50 | + |
| 51 | +1. To run the example we need to first build the examples using the following command: |
| 52 | + |
| 53 | +```bash |
| 54 | +cargo build --examples |
| 55 | +``` |
| 56 | + |
| 57 | +2. Executing the first query |
| 58 | +Query: |
| 59 | +```json |
| 60 | +{ |
| 61 | + "filter": { |
| 62 | + "EQ": { "state": "CA" } |
| 63 | + }, |
| 64 | + "sort": [ |
| 65 | + { |
| 66 | + "key": "person.id", |
| 67 | + "order": "DESC" |
| 68 | + } |
| 69 | + ] |
| 70 | +} |
| 71 | +
|
| 72 | +``` |
| 73 | +Execute the first state query using the following command: |
| 74 | +
|
| 75 | +<!-- STEP |
| 76 | +name: Run query_state_q1 example |
| 77 | +output_match_mode: substring |
| 78 | +match_order: none |
| 79 | +expected_stdout_lines: |
| 80 | + - 'San Francisco' |
| 81 | +background: false |
| 82 | +sleep: 15 |
| 83 | +timeout_seconds: 30 |
| 84 | +--> |
| 85 | +```bash |
| 86 | +dapr run --app-id=rustapp --dapr-grpc-port 3501 --resources-path statestore/ cargo run -- --example query_state_q1 |
| 87 | +``` |
| 88 | +<!-- END_STEP --> |
| 89 | +
|
| 90 | +Expected result: |
| 91 | +``` |
| 92 | +Query results: [Object {"id": String("3"), "value": String("{\"city\":\"Sacramento\",\"state\":\"CA\",\"person\":{\"org\":\"Finance\",\"id\":1071.0}}")}, |
| 93 | +Object {"id": String("7"), "value": String("{\"person\":{\"org\":\"Dev Ops\",\"id\":1015.0},\"city\":\"San Francisco\",\"state\":\"CA\"}")}, |
| 94 | +Object {"id": String("5"), "value": String("{\"person\":{\"org\":\"Hardware\",\"id\":1007.0},\"city\":\"Los Angeles\",\"state\":\"CA\"}")}, |
| 95 | + Object {"id": String("9"), "value": String("{\"person\":{\"org\":\"Finance\",\"id\":1002.0},\"city\":\"San Diego\",\"state\":\"CA\"}")}] |
| 96 | +``` |
| 97 | +
|
| 98 | +3. Executing the second query |
| 99 | +Query: |
| 100 | +```json |
| 101 | +{ |
| 102 | + "filter": { |
| 103 | + "IN": { "person.org": [ "Dev Ops", "Hardware" ] } |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | +Execute the second state query using the following command: |
| 108 | +
|
| 109 | +<!-- STEP |
| 110 | +name: Run query_state_q2 example |
| 111 | +output_match_mode: substring |
| 112 | +match_order: none |
| 113 | +expected_stdout_lines: |
| 114 | + - 'New York' |
| 115 | +background: false |
| 116 | +sleep: 15 |
| 117 | +timeout_seconds: 30 |
| 118 | +--> |
| 119 | +```bash |
| 120 | +dapr run --app-id=rustapp --dapr-grpc-port 3501 --resources-path statestore/ cargo run -- --example query_state_q2 |
| 121 | +``` |
| 122 | +<!-- END_STEP --> |
| 123 | +
|
| 124 | +Expected result: |
| 125 | +``` |
| 126 | +Query results: [Object {"id": String("5"), "value": String("{\"person\":{\"org\":\"Hardware\",\"id\":1007.0},\"city\":\"Los Angeles\",\"state\":\"CA\"}")}, |
| 127 | +Object {"id": String("2"), "value": String("{\"person\":{\"id\":1028.0,\"org\":\"Hardware\"},\"city\":\"Portland\",\"state\":\"OR\"}")}, |
| 128 | +Object {"id": String("4"), "value": String("{\"person\":{\"org\":\"Dev Ops\",\"id\":1042.0},\"city\":\"Spokane\",\"state\":\"WA\"}")}, |
| 129 | +Object {"id": String("7"), "value": String("{\"person\":{\"org\":\"Dev Ops\",\"id\":1015.0},\"city\":\"San Francisco\",\"state\":\"CA\"}")}, |
| 130 | +Object {"id": String("8"), "value": String("{\"city\":\"Redmond\",\"state\":\"WA\",\"person\":{\"id\":1077.0,\"org\":\"Hardware\"}}")}, |
| 131 | +Object {"id": String("10"), "value": String("{\"person\":{\"org\":\"Dev Ops\",\"id\":1054.0},\"city\":\"New York\",\"state\":\"NY\"}")}, |
| 132 | +Object {"id": String("1"), "value": String("{\"person\":{\"org\":\"Dev Ops\",\"id\":1036.0},\"city\":\"Seattle\",\"state\":\"WA\"}")}] |
| 133 | +``` |
0 commit comments