@@ -116,64 +116,48 @@ var result = await client.GetContainer("database", "container").Scripts.ExecuteS
116
116
The following example shows how to register a stored procedure by using the Java SDK:
117
117
118
118
``` java
119
- String containerLink = String . format(" /dbs/%s/colls/%s" , " myDatabase" , " myContainer" );
120
- StoredProcedure newStoredProcedure = new StoredProcedure (
121
- " {" +
122
- " 'id':'spCreateToDoItems'," +
123
- " 'body':" + new String (Files . readAllBytes(Paths . get(" ..\\ js\\ spCreateToDoItems.js" ))) +
124
- " }" );
125
- // toBlocking() blocks the thread until the operation is complete and is used only for demo.
126
- StoredProcedure createdStoredProcedure = asyncClient. createStoredProcedure(containerLink, newStoredProcedure, null )
127
- .toBlocking(). single(). getResource();
119
+ CosmosStoredProcedureProperties definition = new CosmosStoredProcedureProperties (
120
+ " spCreateToDoItems" ,
121
+ Files . readString(Paths . get(" createToDoItems.js" ))
122
+ );
123
+
124
+ CosmosStoredProcedureResponse response = container
125
+ .getScripts()
126
+ .createStoredProcedure(definition);
128
127
```
129
128
130
129
The following code shows how to call a stored procedure by using the Java SDK:
131
130
132
131
``` java
133
- String containerLink = String . format(" /dbs/%s/colls/%s" , " myDatabase" , " myContainer" );
134
- String sprocLink = String . format(" %s/sprocs/%s" , containerLink, " spCreateToDoItems" );
135
- final CountDownLatch successfulCompletionLatch = new CountDownLatch (1 );
136
-
137
- List<ToDoItem > ToDoItems = new ArrayList<ToDoItem > ();
138
-
139
- class ToDoItem {
140
- public String category;
141
- public String name;
142
- public String description;
143
- public boolean isComplete;
144
- }
145
-
146
- ToDoItem newItem = new ToDoItem ();
147
- newItem. category = " Personal" ;
148
- newItem. name = " Groceries" ;
149
- newItem. description = " Pick up strawberries" ;
150
- newItem. isComplete = false ;
151
-
152
- ToDoItems . add(newItem)
132
+ CosmosStoredProcedure sproc = container
133
+ .getScripts()
134
+ .getStoredProcedure(" spCreateToDoItems" );
153
135
154
- newItem. category = " Personal" ;
155
- newItem. name = " Doctor" ;
156
- newItem. description = " Make appointment for check up" ;
157
- newItem. isComplete = false ;
136
+ List<Object > items = new ArrayList<Object > ();
158
137
159
- ToDoItems . add(newItem)
138
+ ToDoItem firstItem = new ToDoItem ();
139
+ firstItem. category = " Personal" ;
140
+ firstItem. name = " Groceries" ;
141
+ firstItem. description = " Pick up strawberries" ;
142
+ firstItem. isComplete = false ;
143
+ items. add(firstItem);
160
144
161
- RequestOptions requestOptions = new RequestOptions ();
162
- requestOptions. setPartitionKey(new PartitionKey (" Personal" ));
145
+ ToDoItem secondItem = new ToDoItem ();
146
+ secondItem. category = " Personal" ;
147
+ secondItem. name = " Doctor" ;
148
+ secondItem. description = " Make appointment for check up" ;
149
+ secondItem. isComplete = true ;
150
+ items. add(secondItem);
163
151
164
- Object [] storedProcedureArgs = new Object [] { ToDoItems };
165
- asyncClient. executeStoredProcedure(sprocLink, requestOptions, storedProcedureArgs)
166
- .subscribe(storedProcedureResponse - > {
167
- String storedProcResultAsString = storedProcedureResponse. getResponseAsString();
168
- successfulCompletionLatch. countDown();
169
- System . out. println(storedProcedureResponse. getActivityId());
170
- }, error - > {
171
- successfulCompletionLatch. countDown();
172
- System . err. println(" an error occurred while executing the stored procedure: actual cause: "
173
- + error. getMessage());
174
- });
152
+ CosmosStoredProcedureRequestOptions options = new CosmosStoredProcedureRequestOptions ();
153
+ options. setPartitionKey(
154
+ new PartitionKey (" Personal" )
155
+ );
175
156
176
- successfulCompletionLatch. await();
157
+ CosmosStoredProcedureResponse response = sproc. execute(
158
+ items,
159
+ options
160
+ );
177
161
```
178
162
179
163
### [ JavaScript SDK] ( #tab/javascript-sdk )
@@ -324,32 +308,33 @@ await client.GetContainer("database", "container").CreateItemAsync(newItem, null
324
308
The following code shows how to register a pre-trigger using the Java SDK:
325
309
326
310
``` java
327
- String containerLink = String . format(" /dbs/%s/colls/%s" , " myDatabase" , " myContainer" );
328
- String triggerId = " trgPreValidateToDoItemTimestamp" ;
329
- Trigger trigger = new Trigger ();
330
- trigger. setId(triggerId);
331
- trigger. setBody(new String (Files . readAllBytes(Paths . get(String . format(" ..\\ js\\ %s.js" , triggerId)));
332
- trigger. setTriggerOperation(TriggerOperation . Create );
333
- trigger. setTriggerType(TriggerType . Pre );
334
- // toBlocking() blocks the thread until the operation is complete and is used only for demo.
335
- Trigger createdTrigger = asyncClient. createTrigger(containerLink, trigger, new RequestOptions ()). toBlocking(). single(). getResource();
311
+ CosmosTriggerProperties definition = new CosmosTriggerProperties (
312
+ " preValidateToDoItemTimestamp" ,
313
+ Files . readString(Paths . get(" validateToDoItemTimestamp.js" ))
314
+ );
315
+ definition. setTriggerOperation(TriggerOperation . CREATE );
316
+ definition. setTriggerType(TriggerType . PRE );
317
+
318
+ CosmosTriggerResponse response = container
319
+ .getScripts()
320
+ .createTrigger(definition);
336
321
```
337
322
338
323
The following code shows how to call a pre-trigger using the Java SDK:
339
324
340
325
``` java
341
- String containerLink = String . format( " /dbs/%s/colls/%s " , " myDatabase " , " myContainer " );
342
- Document item = new Document ( " { "
343
- + " \" category \" : \" Personal \" , "
344
- + " \" name \" : \" Groceries \" , "
345
- + " \" description \" : \" Pick up strawberries \" , "
346
- + " \" isComplete \" : false, "
347
- + " } "
348
- );
349
- RequestOptions requestOptions = new RequestOptions ();
350
- requestOptions . setPreTriggerInclude( Arrays . asList( " trgPreValidateToDoItemTimestamp " ) );
351
- // toBlocking() blocks the thread until the operation is complete and is used only for demo.
352
- asyncClient . createDocument(containerLink, item, requestOptions, false ) . toBlocking( );
326
+ ToDoItem item = new ToDoItem ( );
327
+ item. category = " Personal " ;
328
+ item . name = " Groceries " ;
329
+ item . description = " Pick up strawberries " ;
330
+ item . isComplete = false ;
331
+
332
+ CosmosItemRequestOptions options = new CosmosItemRequestOptions ();
333
+ options . setPreTriggerInclude(
334
+ Arrays . asList( " preValidateToDoItemTimestamp " )
335
+ );
336
+
337
+ CosmosItemResponse< ToDoItem > response = container . createItem(item, options );
353
338
```
354
339
355
340
### [ JavaScript SDK] ( #tab/javascript-sdk )
@@ -484,30 +469,33 @@ await client.GetContainer("database", "container").CreateItemAsync(newItem, null
484
469
The following code shows how to register a post-trigger using the Java SDK:
485
470
486
471
``` java
487
- String containerLink = String.format ("/dbs /%s /colls /%s ", "myDatabase ", "myContainer ");
488
- String triggerId = " trgPostUpdateMetadata" ;
489
- Trigger trigger = new Trigger ();
490
- trigger.setId (triggerId );
491
- trigger.setBody (new String (Files .readAllBytes (Paths .get (String .format ("..\\js \\%s .js ", triggerId )))));
492
- trigger.setTriggerOperation (TriggerOperation .Create );
493
- trigger.setTriggerType (TriggerType .Post );
494
- Trigger createdTrigger = asyncClient. createTrigger(containerLink, trigger, new RequestOptions ()). toBlocking(). single(). getResource();
472
+ CosmosTriggerProperties definition = new CosmosTriggerProperties (
473
+ " postUpdateMetadata" ,
474
+ Files . readString(Paths . get(" updateMetadata.js" ))
475
+ );
476
+ definition. setTriggerOperation(TriggerOperation . CREATE );
477
+ definition. setTriggerType(TriggerType . POST );
478
+
479
+ CosmosTriggerResponse response = container
480
+ .getScripts()
481
+ .createTrigger(definition);
495
482
```
496
483
497
484
The following code shows how to call a post-trigger using the Java SDK:
498
485
499
486
``` java
500
- String containerLink = String . format(" /dbs/%s/colls/%s" , " myDatabase" , " myContainer" );
501
- Document item = new Document (String . format(" { "
502
- + " \" name\" : \" artist_profile_1023\" , "
503
- + " \" artist\" : \" The Band\" , "
504
- + " \" albums\" : [\" Hellujah\" , \" Rotators\" , \" Spinning Top\" ]"
505
- + " }"
506
- ));
507
- RequestOptions requestOptions = new RequestOptions ();
508
- requestOptions.setPostTriggerInclude (Arrays .asList ("trgPostUpdateMetadata "));
509
- // toBlocking() blocks the thread until the operation is complete, and is used only for demo.
510
- asyncClient.createDocument (containerLink , item , requestOptions , false ).toBlocking ();
487
+ ToDoItem item = new ToDoItem ();
488
+ item. category = " Personal" ;
489
+ item. name = " Doctor" ;
490
+ item. description = " Make appointment for check up" ;
491
+ item. isComplete = true ;
492
+
493
+ CosmosItemRequestOptions options = new CosmosItemRequestOptions ();
494
+ options. setPostTriggerInclude(
495
+ Arrays . asList(" postUpdateMetadata" )
496
+ );
497
+
498
+ CosmosItemResponse<ToDoItem > response = container. createItem(item, options);
511
499
```
512
500
513
501
### [ JavaScript SDK] ( #tab/javascript-sdk )
@@ -640,37 +628,25 @@ while (iterator.HasMoreResults)
640
628
The following code shows how to register a user-defined function using the Java SDK:
641
629
642
630
``` java
643
- String containerLink = String.format ("/dbs /%s /colls /%s ", "myDatabase ", "myContainer ");
644
- String udfId = " Tax" ;
645
- UserDefinedFunction udf = new UserDefinedFunction ();
646
- udf.setId (udfId );
647
- udf.setBody (new String (Files .readAllBytes (Paths .get (String .format ("..\\js \\%s .js ", udfId )))));
648
- // toBlocking() blocks the thread until the operation is complete and is used only for demo.
649
- UserDefinedFunction createdUDF = client. createUserDefinedFunction(containerLink, udf, new RequestOptions ()). toBlocking(). single(). getResource();
631
+ CosmosUserDefinedFunctionProperties definition = new CosmosUserDefinedFunctionProperties (
632
+ " udfTax" ,
633
+ Files . readString(Paths . get(" tax.js" ))
634
+ );
635
+
636
+ CosmosUserDefinedFunctionResponse response = container
637
+ .getScripts()
638
+ .createUserDefinedFunction(definition);
650
639
```
651
640
652
641
The following code shows how to call a user-defined function using the Java SDK:
653
642
654
643
``` java
655
- String containerLink = String . format(" /dbs/%s/colls/%s" , " myDatabase" , " myContainer" );
656
- Observable<FeedResponse<Document > > queryObservable = client.queryDocuments (containerLink , "SELECT * FROM Incomes t WHERE udf. Tax (t .income ) > 20000", new FeedOptions ());
657
- final CountDownLatch completionLatch = new CountDownLatch (1 );
658
- queryObservable.subscribe (
659
- queryResultPage -> {
660
- System .out .println ("Got a page of query result with " +
661
- queryResultPage .getResults ().size ());
662
- },
663
- // terminal error signal
664
- e - > {
665
- e. printStackTrace();
666
- completionLatch. countDown();
667
- },
668
-
669
- // terminal completion signal
670
- () - > {
671
- completionLatch. countDown();
672
- });
673
- completionLatch.await ();
644
+ CosmosQueryRequestOptions options = new CosmosQueryRequestOptions ();
645
+
646
+ CosmosPagedIterable<ToDoItem > iterable = container. queryItems(
647
+ " SELECT t.cost, udf.udfTax(t.cost) AS costWithTax FROM t" ,
648
+ options,
649
+ ToDoItem . class);
674
650
```
675
651
676
652
### [ JavaScript SDK] ( #tab/javascript-sdk )
0 commit comments