forked from apache/geode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
develop_rest_apps.html.md.erb
683 lines (514 loc) · 21.6 KB
/
develop_rest_apps.html.md.erb
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
---
title: Developing REST Applications
---
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.
-->
<a id="topic_lvp_cd5_m4"></a>
This section provides guidelines on writing REST client applications for <%=vars.product_name%>.
You can browse, query, update and delete data stored in your <%=vars.product_name%> deployment. You can also manage and execute pre-deployed functions on <%=vars.product_name%> members.
- **[Working with Regions](#topic_qhs_f25_m4)**
The <%=vars.product_name%> REST APIs provide basic CRUD (create, read, update and delete) operations for data entries stored in your regions.
- **[Working with Queries](#topic_fcn_g25_m4)**
<%=vars.product_name%> supports the use of queries to extract data from its regions. Using REST APIs, you can create and execute either prepared or ad-hoc queries on <%=vars.product_name%> regions. You can also update and delete prepared queries.
- **[Working with Functions](#topic_rbc_h25_m4)**
<%=vars.product_name%> REST APIs support the discovery and execution of predefined <%=vars.product_name%> functions on your cluster deployments.
## <a id="topic_qhs_f25_m4" class="no-quick-link"></a>Working with Regions
The <%=vars.product_name%> REST APIs provide basic CRUD (create, read, update and delete) operations for data entries stored in your regions.
Regions are the resources of the <%=vars.product_name%> REST API. Each region represents a resource or a collection of resources.
You cannot create or delete the regions themselves with the REST APIs, but you can work with the data stored within predefined <%=vars.product_name%> regions. Use the [gfsh](../tools_modules/gfsh/chapter_overview.html) command utility to add, configure or delete regions in your <%=vars.product_name%> deployment. Any additions or modifications to regions made through `gfsh` are then accessible by the REST APIs.
## Listing Available Regions
The main resource endpoint to the <%=vars.product_name%> API is [GET /gemfire-api/v1](get_regions.html#topic_itv_mg5_m4). Use this endpoint to discover which regions are available in your cluster.
Example call:
``` pre
curl -i http://localhost:7070/gemfire-api/v1
```
Example response:
``` pre
Accept: application/json
Response Payload: application/json
200 OK
Server: Apache-Coyote/1.1
Location: http://localhost:7070/gemfire-api/v1
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sat, 18 Jan 2014 20:05:47 GMT
{
"regions": [
{
"name": "customers",
"type": "REPLICATE",
"key-constraint": "java.lang.String",
"value-constraint": "org.apache.geode.pdx.PdxInstance"
},
{
"name": "items",
"type": "REPLICATE",
"key-constraint": null,
"value-constraint": null
},
{
"name": "orders",
"type": "PARTITION",
"key-constraint": null,
"value-constraint": null
},
{
"name": "primitiveKVStore",
"type": "PARTITION",
"key-constraint": null,
"value-constraint": null
},
{
"name": "empty_region",
"type": "EMPTY",
"key-constraint": "java.lang.String",
"value-constraint": "org.apache.geode.pdx.PdxInstance"
}
]
}
```
Each region listed in the response includes the following region attributes:
- **name**. Name of the region.
- **type**. Type of region. For example, REPLICATE, PARTITION, or EMPTY. See [Region Types](../developing/region_options/region_types.html#region_types) for more information.
- **key-constraint**. If defined, the fully qualified class name of the key's type. Otherwise, null.
- **value-constraint**. If defined, the fully qualified class name of the value's type. Otherwise, null.
If no resources (regions) are available in the cluster, the call returns a 404 NOT FOUND error.
## Reading Region Data
You can read data from a region by using any of the following REST-enabled mechanisms:
- **GET /gemfire-api/v1/{region}?limit=ALL** - Read all entries in a region
- **GET /gemfire-api/v1/{region}?limit=*N*** - Read a limited number of entries in a region
- **GET /gemfire-api/v1/{region}/keys** - List all keys in a region
- **GET /gemfire-api/v1/{region}/{keys}** - Read data for specific key or keys in a region
**Reading Entries**
To read entries in a region, use the following REST endpoint:
``` pre
GET /gemfire-api/v1/{region}?[limit={<number>|ALL}]
```
For example:
``` pre
http://localhost:7070/gemfire-api/v1/items
```
To read all entries in the region, specify instead:
``` pre
http://localhost:7070/gemfire-api/v1/items?limit=ALL
```
To read 80 entries from the region, specify:
``` pre
http://localhost:7070/gemfire-api/v1/items?limit=80
```
Setting the limit parameter is optional. If you do not specify a limit, the request will return 50 results by default. The returned order of the results is not guaranteed; however, the response values (JSON) are specified in the same order as the list of comma-separated keys returned in the "Content-location" header.
**Reading Keys**
To list all keys in a region, use the following endpoint:
``` pre
/gemfire-api/v1/{region}/keys
```
For example:
``` pre
http://localhost:7070/gemfire-api/v1/items/keys
```
You can use the returned key to perform additional operations on the keys such as read, update or delete their values.
**Reading Entries By Key**
To obtain data for a specific key or set of keys, use the following endpoints:
``` pre
GET /gemfire-api/v1/{region}/{key1}
```
or
``` pre
GET /gemfire-api/v1/{region}/{key1},{key2},...,{keyN}
```
where you specify keys in a comma-delimited list.
For example:
``` pre
http://localhost:7070/gemfire-api/v1/items/1
http://localhost:7070/gemfire-api/v1/items/1,3,5
```
If one or more of the keys you provide in the list of keys is missing from the region, you will receive a 400 BAD STATUS error response.
If you are providing multiple keys, you can also use the `ignoreMissingKey=true` parameter to prevent 400 errors. Any non-existing keys will instead return a null response. For example:
``` pre
http://localhost:7070/gemfire-api/v1/items/1,3,5?ignoreMissingKey=true
```
## Adding or Modifying Region Data
To add data to a region, you have several options:
- Create new a brand new entry (both key and value) in a region
- Update or insert a value for a key (if the key does not exist, it will be created)
- Update (replace) data for a key if and only if the key already exists in region
- Compare existing value for a key before replacing value
- Update or insert multiple values in the region for a set of provided keys
**Adding entries**
To add a new key and value to a region, you can use the following endpoint:
``` pre
POST /gemfire-api/v1/{region}?key=<key>
```
This endpoint only puts the entry into the region **if the specified key does not already exist**. If the key already exists, the request will fail with a 409 CONFLICT error.
**Note:**
If you do not specify a key for this request, a String representation of a numerical key will be generated automatically for you.
Specify the value for the new entry in the request body. For example:
``` pre
http://localhost:7070/gemfire-api/v1/orders?key=2
```
``` pre
Request Payload: application/json
POST /gemfire-api/v1/orders?key=2
Accept: application/json
Content-Type: application/json
{
"@type": "org.apache.geode.web.rest.domain.Order",
"purchaseOrderNo": 112,
"customerId": 1012,
"description": "Purchase Order for myCompany",
"orderDate": "02/10/2014",
"deliveryDate": "02/20/2014",
"contact": "John Doe",
"email": "[email protected]",
"phone": "01-2048096",
"totalPrice": 225,
"items": [
{
"itemNo": 1,
"description": "Product2, PartA",
"quantity": 10,
"unitPrice": 5,
"totalPrice": 50
},
{
"itemNo": 2,
"description": "Product2, PartB",
"quantity": 20,
"unitPrice": 20,
"totalPrice": 400
}
]
}
```
Note that in the example above, `@type` is used to declare the value-constraint for the new entry. This declaration is required to provide interoperability between Java cache clients and REST clients.
Alternately, you can also use the following endpoint to create a new entry:
``` pre
PUT /gemfire/v1/{region}/{key}
```
This endpoint will add the entry if the key does not exist. If the key already exists, the operation will update the entry.
``` pre
http://localhost:7070/gemfire-api/v1/orders/2
```
``` pre
Request Payload: application/json
PUT /gemfire-api/v1/orders/2
Request Payload: application/json
Content-Type: application/json
Accept: application/json
{
"@type": "org.apache.geode.web.rest.domain.Order",
"purchaseOrderNo": 1121,
"customerId": 1012,
"description": "Order for XYZ Corp",
"orderDate": "02/10/2014",
"deliveryDate": "02/20/2014",
"contact": "Pie Doe",
"email": "[email protected]",
"phone": "01-2048096",
"totalPrice": 225,
"items": [
{
"itemNo": 1,
"description": "Product-100",
"quantity": 10,
"unitPrice": 5,
"totalPrice": 50
}
]
}
```
**Modifying existing entries**
<%=vars.product_name%> provides three different options for this type of operation. To update a value for the key, you can use:
``` pre
PUT /gemfire/v1/{region}/{key}
PUT /gemfire/v1{region}/{key}?op=REPLACE
PUT /gemfire/v1{region}/{key}?op=CAS
```
If you do not specify a parameter to the PUT operation, the request which will add or update the entry regardless of whether the key already exists in the region. See the example above.
``` pre
http://localhost:7070/gemfire-api/v1/orders/2
```
If you specify the op=REPLACE parameter, the request which will explicitly perform a Cache replace operation and will verify that the key exists before replacing the value. If the key does not exist in the specified region, you will receive a 404 NOT FOUND error. This operation is idempotent, meaning multiple identical requests will have the same effect as the initial request.
``` pre
http://localhost:7070/gemfire-api/v1/orders/2?op=REPLACE
```
If you specify the op=CAS parameter, the value will only be replaced with the `@new` value only if the specified `@old` value matches the current value of the key in the region. If the `@old` value does not match the current value, then a 409 CONFLICT error is thrown. If you receive a 409 CONFLICT error, you can call the `GET /gemfire-api/v1/{region}/{key}` endpoint to get an updated copy of the value. This operation is not idempotent and multiple identical requests will not have the same effect as the initial request. You can use this type of REST call to achieve a similar effect as optimistic locking.
``` pre
http://localhost:7070/gemfire-api/v1/orders/2?op=REPLACE
```
``` pre
Request Payload: application/json
PUT /gemfire-api/v1/orders/2?op=CAS
Accept: application/json
Content-Type: application/json
{
"@old": {
"@type": "org.apache.geode.web.rest.domain.Order",
"purchaseOrderNo": 1121,
"customerId": 1012,
"description": "Order for XYZ Corp",
"orderDate": "02/10/2014",
"deliveryDate": "02/20/2014",
"contact": "Jelly Bean",
"email": "[email protected]",
"phone": "01-2048096",
"items": [
{
"itemNo": 1,
"description": "Product-100",
"quantity": 12,
"unitPrice": 5,
"totalPrice": 60
}
],
"totalPrice": 225
},
"@new ": {
"@type": "org.apache.geode.web.rest.domain.Order",
"purchaseOrderNo": 1121,
"customerId": 1013,
"description": "Order for New Corp",
"orderDate": "02/10/2014",
"deliveryDate": "02/25/2014",
"contact": "Vanilla Bean",
"email": "[email protected]",
"phone": "01-2048096",
"items": [
{
"itemNo": 12345,
"description": "part 123",
"quantity": 12,
"unitPrice": 29.99,
"totalPrice": 149.95
}
],
"totalPrice": 149.95
}
}
```
**Adding or updating multiple values for a set of keys**
To update multiple values for keys, use:
``` pre
PUT /gemfire-api/v1/{region}/{key1},{key2},...,{keyN}
```
This REST call will update any keys that already exist and insert values for any keys that do not exist in the region.
## Deleting Region Data
There are three options for deleting data in a region using REST APIs:
- Delete all the data in the region
- Delete the data associated with a particular key
- Delete the data associated with a set of keys
**Deleting all data**
To delete all data in the region, use the following endpoint:
``` pre
DELETE /gemfire-api/v1/{region}
```
For example:
``` pre
http://localhost:7070/gemfire-api/v1/items
```
Note that this does not delete the region itself, but instead all the entries in the region.
**Deleting data based on key**
Use:
``` pre
DELETE /gemfire-api/v1/{region}/{key}
```
or
``` pre
DELETE /gemfire-api/v1/{region}/{key}{key1},{key2},...{keyN}
```
If any of the supplied keys are not found in the region, the request will fail and return a 404 NOT FOUND ERROR.
## <a id="topic_fcn_g25_m4" class="no-quick-link"></a>Working with Queries
<%=vars.product_name%> supports the use of queries to extract data from its regions. Using REST APIs, you can create and execute either prepared or ad-hoc queries on <%=vars.product_name%> regions. You can also update and delete prepared queries.
## Listing Queries
To find out which predefined and named queries are available in your deployment, use the following endpoint:
``` pre
GET /gemfire-api/v1/queries
```
All queries that have been predefined and assigned IDs in <%=vars.product_name%> are listed.
## <a id="topic_fcn_g25_m4__section_t4h_wtp_y4" class="no-quick-link"></a>Creating a New Query
To create a query, use the following endpoint:
``` pre
POST /gemfire-api/v1/queries?id=<queryId>&q=<OQL-statement>
```
Here are some examples:
``` pre
http://localhost:7070/gemfire-api/v1/queries?id=selectOffers&q="SELECT DISTINCT c FROM /customers c, /orders o WHERE o.totalprice < $1 AND c.customerId = o.customerId"
```
**Note:**
The query must be provided as a URL parameter. You cannot specify OQL in the request body at this time.
You can specify query bind parameters ($1) in your predefined queries and then pass in values at runtime.
To update this query at a later time, use the [PUT operation](#topic_fcn_g25_m4__section_oj1_b5p_y4) described below.
## <a id="topic_fcn_g25_m4__section_b1p_wtp_y4" class="no-quick-link"></a>Executing a Prepared Query
To run a prepared query, use:
``` pre
POST /gemfire-api/v1/queries/{queryId}
```
Specify the queryId in the URL. All query argument must be passed in the request body. For example:
``` pre
http://localhost:7070/gemfire-api/v1/queries/selectOrders
```
``` pre
Request Payload: OQL bind parameter values HTTP message body of media type application/json
POST /gemfire-api/v1/queries/selectOrders
Accept: application/json
Content-Type: application/json
[
{
"@type": "int",
"@value": 2
},
{
"@type": "double",
"@value": 110.00
}
]
Response Payload: application/json
200 OK
Content-Length: <#-of-bytes>
Content-Type: application/json
[
{
"description": "Purchase order for company - B",
"totalPrice": 350,
"purchaseOrderNo": 1112,
"customerId": 102,
"deliveryDate": "Thu Feb 20 00:00:00 IST 2014",
"contact": "John Doe",
"email": "[email protected]",
"phone": "01-2048096",
"items": [
{
"description": "Product-AAAA",
"quantity": 10,
"itemNo": 1,
"unitPrice": 20,
"totalPrice": 200,
"type-class": "org.apache.geode.web.rest.domain.Item"
},
{
"description": "Product-BBB",
"quantity": 15,
"itemNo": 2,
"unitPrice": 10,
"totalPrice": 150,
"type-class": "org.apache.geode.web.rest.domain.Item"
}
],
"orderDate": "Mon Feb 10 00:00:00 IST 2014",
"type-class": "org.apache.geode.web.rest.domain.Order"
},
{...},
{...}
}
```
Another example:
``` pre
http://localhost:7070/gemfire-api/v1/queries/selectOrders
```
``` pre
Request Payload: OQL bind parameter values HTTP message body of media type application/json
POST /gemfire-api/v1/queries/selectCustomer
Accept: application/json
Content-Type: application/json
{
"args": [
{
"@type": "int",
"@value": 101
}
]
}
Response-Payload: application/json
200 Ok
Content-Length: 140
Content-Type: application/json
[
{
"firstName": "Jane",
"lastName": "Doe",
"customerId": 101,
"type-class": "org.apache.geode.web.rest.domain.Customer"
}
]
```
## <a id="topic_fcn_g25_m4__section_oj1_b5p_y4" class="no-quick-link"></a>Modifying a Prepared Query
To modify an existing prepared query, use the following endpoint:
``` pre
PUT /gemfire-api/v1/queries/{queryId}
```
Here are some examples:
``` pre
http://localhost:7070/gemfire-api/v1/queries/selectOffers&q="SELECT DISTINCT c FROM /customers c, /orders o WHERE o.totalprice < $1 AND c.customerId = o.customerId"
```
You can specify query bind parameters ($1) in your predefined queries, and then pass in values at runtime.
A PUT operation will only succeed if the specified queryId already exists (for example, created with the [POST operation](#topic_fcn_g25_m4__section_t4h_wtp_y4) above.) If the queryId does already exist, you will receive a 404 response - "Named query (selectKey456) does not exist!"
## <a id="topic_fcn_g25_m4__section_igg_b5p_y4" class="no-quick-link"></a>Deleting a Prepared Query
To delete an existing prepared query, use the following endpoint:
``` pre
DELETE /gemfire-api/v1/queries/{queryId}
```
## <a id="topic_fcn_g25_m4__section_wbk_b5p_y4" class="no-quick-link"></a>Executing an Ad-Hoc Query
To run an unnamed query, use the following endpoint:
``` pre
GET /gemfire-api/v1/queries/adhoc?q=<OQL-statement>
```
Provide the OQL query string directly in the URL enclosed in single quotes.
For example:
``` pre
http://localhost:7070/gemfire-api/v1/queries/adhoc?q="SELECT * FROM /customers"
```
## <a id="topic_rbc_h25_m4" class="no-quick-link"></a>Working with Functions
<%=vars.product_name%> REST APIs support the discovery and execution of predefined <%=vars.product_name%> functions on your cluster deployments.
Before you can access functions using REST APIs, you must have already defined and registered the functions in your <%=vars.product_name%> deployment. Additionally, any domain objects that are being accessed by the functions must be available on the CLASSPATH of the server running the REST endpoint service.
You can do the following with functions:
- List all functions available in the <%=vars.product_name%> cluster.
- Execute a function, optionally specifying the region and members and/or member groups that are targeted by the function
## Listing Functions
To list all functions that are currently registered and deployed in the <%=vars.product_name%> cluster, use the following endpoint:
``` pre
GET /gemfire-api/v1/functions
```
The list of returned functions includes the functionId, which you can use to execute the function as described in the next section.
## Executing Functions
To execute a function on a <%=vars.product_name%> cluster, use the following endpoint:
``` pre
POST /gemfire-api/v1/functions/{functionId}?[&onRegion=regionname|&onMembers=member1,member2,...,memberN|&onGroups=group1,group2,...,groupN]
```
You have the option to target a specific region and members or member groups when executing your function. If you do not specify these parameters, the function will execute on all members that are hosting data in the entire cluster by default. Function arguments are passed in the request body.
For example:
``` pre
http://localhost:7070/gemfire-api/v1/functions/AddFreeItemToOrders
```
``` pre
Request Payload: application/json
POST /gemfire-api/v1/functions/AddFreeItemToOrders
Accept: application/json
Content-Type: application/json
[
{
"@type": "double",
"@value": 210
},
{
"@type": "org.apache.geode.web.rest.domain.Item",
"itemNo": "599",
"description": "Part X Free on Bumper Offer",
"quantity": "2",
"unitprice": "5",
"totalprice": "10.00"
}
]
```
In the above example, the `Item` domain object must be in the CLASSPATH of all members receiving the function. If the object is not defined, the function will fail with an `Internal Server` error. Look for `ClassNotFoundException`s in the stack trace.