use of com.azure.cosmos.models.CosmosStoredProcedureRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleStoredProcedureAsync method createStoredProcedure.
public void createStoredProcedure() throws Exception {
logger.info("Creating stored procedure...\n");
sprocId = "createMyDocument";
String sprocBody = "function createMyDocument() {\n" + "var documentToCreate = {\"id\":\"test_doc\"}\n" + "var context = getContext();\n" + "var collection = context.getCollection();\n" + "var accepted = collection.createDocument(collection.getSelfLink(), documentToCreate,\n" + " function (err, documentCreated) {\n" + "if (err) throw new Error('Error' + err.message);\n" + "context.getResponse().setBody(documentCreated.id)\n" + "});\n" + "if (!accepted) return;\n" + "}";
CosmosStoredProcedureProperties storedProcedureDef = new CosmosStoredProcedureProperties(sprocId, sprocBody);
container.getScripts().createStoredProcedure(storedProcedureDef, new CosmosStoredProcedureRequestOptions()).block();
}
use of com.azure.cosmos.models.CosmosStoredProcedureRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleStoredProcedureAsync method executeStoredProcedure.
public void executeStoredProcedure() throws Exception {
logger.info(String.format("Executing stored procedure %s...\n\n", sprocId));
CosmosStoredProcedureRequestOptions options = new CosmosStoredProcedureRequestOptions();
options.setPartitionKey(new PartitionKey("test_doc"));
container.getScripts().getStoredProcedure(sprocId).execute(null, options).flatMap(executeResponse -> {
logger.info(String.format("Stored procedure %s returned %s (HTTP %d), at cost %.3f RU.\n", sprocId, executeResponse.getResponseAsString(), executeResponse.getStatusCode(), executeResponse.getRequestCharge()));
return Mono.empty();
}).block();
}
use of com.azure.cosmos.models.CosmosStoredProcedureRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleStoredProcedure method createStoredProcedure.
public void createStoredProcedure() throws Exception {
logger.info("Creating stored procedure...");
sprocId = "createMyDocument";
String sprocBody = "function createMyDocument() {\n" + "var documentToCreate = {\"id\":\"test_doc\", \"city\":\"Seattle\"}\n" + "var context = getContext();\n" + "var collection = context.getCollection();\n" + "var accepted = collection.createDocument(collection.getSelfLink(), documentToCreate,\n" + " function (err, documentCreated) {\n" + "if (err) throw new Error('Error' + err.message);\n" + "context.getResponse().setBody(documentCreated.id)\n" + "});\n" + "if (!accepted) return;\n" + "}";
CosmosStoredProcedureProperties storedProcedureDef = new CosmosStoredProcedureProperties(sprocId, sprocBody);
container.getScripts().createStoredProcedure(storedProcedureDef, new CosmosStoredProcedureRequestOptions());
}
use of com.azure.cosmos.models.CosmosStoredProcedureRequestOptions in project DataSpaceConnector by eclipse-dataspaceconnector.
the class CosmosTransferProcessStoreIntegrationTest method invokeStoreProcedure.
@Test
void invokeStoreProcedure() {
// create one item
var tp = createTransferProcess("proc1");
store.create(tp);
List<Object> procedureParams = Arrays.asList(100, 5, connectorId);
CosmosStoredProcedureRequestOptions options = new CosmosStoredProcedureRequestOptions();
options.setPartitionKey(PartitionKey.NONE);
CosmosStoredProcedureResponse sprocResponse = container.getScripts().getStoredProcedure("nextForState").execute(procedureParams, options);
var result = sprocResponse.getResponseAsString();
var l = typeManager.readValue(result, List.class);
List<TransferProcessDocument> documents = (List<TransferProcessDocument>) l.stream().map(o -> typeManager.writeValueAsString(o)).map(json -> typeManager.readValue(json.toString(), TransferProcessDocument.class)).collect(Collectors.toList());
assertThat(documents).allSatisfy(document -> {
assertThat(document.getLease()).isNotNull().hasFieldOrPropertyWithValue("leasedBy", connectorId).hasFieldOrProperty("leasedAt");
});
}
use of com.azure.cosmos.models.CosmosStoredProcedureRequestOptions in project DataSpaceConnector by eclipse-dataspaceconnector.
the class CosmosDbApiImpl method invokeStoredProcedure.
@Override
public String invokeStoredProcedure(String procedureName, String partitionKey, Object... args) {
var sproc = getStoredProcedure(procedureName);
List<Object> params = Arrays.asList(args);
var options = new CosmosStoredProcedureRequestOptions();
options.setPartitionKey(new PartitionKey(partitionKey));
CosmosStoredProcedureResponse response = sproc.execute(params, options);
handleResponse(response.getStatusCode(), "Failed to invoke stored procedure: " + procedureName);
return response.getResponseAsString();
}
Aggregations