use of com.azure.cosmos.models.CosmosStoredProcedureRequestOptions in project DataSpaceConnector by eclipse-dataspaceconnector.
the class CosmosTransferProcessStoreIntegrationTest method invokeStoredProcedure.
@Test
void invokeStoredProcedure() {
// 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 azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SalesOrder method MigrateJavaSDKv4SprocAsync.
/**
* https://docs.microsoft.com/en-us/azure/cosmos-db/migrate-java-v4-sdk
* Stored procedure
*/
/**
* Stored procedure
*/
public static void MigrateJavaSDKv4SprocAsync() {
String containerName = "family_container";
String partition_key = "/pk";
CosmosAsyncContainer container = null;
// <MigrateSprocAsync>
logger.info("Creating stored procedure...\n");
String 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();
// ...
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();
// </MigrateSprocAsync>
}
use of com.azure.cosmos.models.CosmosStoredProcedureRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleStoredProcedure method executeStoredProcedureArrayArg.
public void executeStoredProcedureArrayArg() throws Exception {
logger.info(String.format("Executing stored procedure %s...\n\n", sprocId + "ArrayArg"));
String partitionValue = "Seattle";
CosmosStoredProcedureRequestOptions options = new CosmosStoredProcedureRequestOptions();
options.setPartitionKey(new PartitionKey(partitionValue));
List<Object> pojos = new ArrayList<>();
pojos.add(new CustomPOJO("idA", partitionValue));
pojos.add(new CustomPOJO("idB", partitionValue));
pojos.add(new CustomPOJO("idC", partitionValue));
List<Object> sproc_args = new ArrayList<>();
sproc_args.add(OBJECT_MAPPER.writeValueAsString(pojos));
CosmosStoredProcedureResponse executeResponse = container.getScripts().getStoredProcedure(sprocId + "ArrayArg").execute(sproc_args, options);
logger.info(String.format("Stored procedure %s returned %s (HTTP %d), at cost %.3f RU.\n", sprocId + "ArrayArg", executeResponse.getResponseAsString(), executeResponse.getStatusCode(), executeResponse.getRequestCharge()));
}
use of com.azure.cosmos.models.CosmosStoredProcedureRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleStoredProcedure 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("Seattle"));
CosmosStoredProcedureResponse executeResponse = container.getScripts().getStoredProcedure(sprocId).execute(null, options);
logger.info(String.format("Stored procedure %s returned %s (HTTP %d), at cost %.3f RU.\n", sprocId, executeResponse.getResponseAsString(), executeResponse.getStatusCode(), executeResponse.getRequestCharge()));
}
use of com.azure.cosmos.models.CosmosStoredProcedureRequestOptions in project azure-cosmos-java-sql-api-samples by Azure-Samples.
the class SampleStoredProcedure method createStoredProcedureArrayArg.
public void createStoredProcedureArrayArg() throws Exception {
logger.info("Creating stored procedure...");
sprocId = "createMyDocument";
String sprocBody = "function " + sprocId + "ArrayArg(jsonArray) {\n" + " var context = getContext();\n" + " var container = context.getCollection();\n" + " //validate if input is valid json\n" + " if (typeof jsonArray === \"string\") {\n" + " try {\n" + " jsonArray = JSON.parse(jsonArray);\n" + " } catch (e) {\n" + " throw \"Bad input to store procedure should be array of json string.\";\n" + " }\n" + " } else {\n" + " throw \"Bad input to store procedure should be array of json string.\";\n" + " }\n" + " var resultDocuments = [];\n" + " jsonArray.forEach(function(jsonDoc) {\n" + " if (jsonDoc.isUpdate != undefined && jsonDoc.isUpdate === true) {\n" + " var accepted = container.replaceDocument(jsonDoc._self, jsonDoc, { etag: jsonDoc._etag },\n" + " function (err, docReplaced) {\n" + " if (err) throw new Error('Error' + err.message);\n" + " resultDocuments.push(docReplaced);\n" + " });\n" + " if (!accepted) throw \"Unable to update document, abort \";\n" + " } else {\n" + " var accepted = container.createDocument(container.getSelfLink(), jsonDoc,\n" + " function (err, itemCreated) {\n" + " if (err) throw new Error('Error' + err.message);\n" + " resultDocuments.push(itemCreated);\n" + " });\n" + " if (!accepted) throw \"Unable to create document, abort \";\n" + " }\n" + " });\n" + " context.getResponse().setBody(resultDocuments);\n" + "}\n";
CosmosStoredProcedureProperties storedProcedureDef = new CosmosStoredProcedureProperties(sprocId + "ArrayArg", sprocBody);
container.getScripts().createStoredProcedure(storedProcedureDef, new CosmosStoredProcedureRequestOptions());
}
Aggregations