Search in sources :

Example 1 with CosmosStoredProcedureRequestOptions

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();
}
Also used : CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties)

Example 2 with CosmosStoredProcedureRequestOptions

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();
}
Also used : CustomPOJO(com.azure.cosmos.examples.common.CustomPOJO) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) Logger(org.slf4j.Logger) PartitionKey(com.azure.cosmos.models.PartitionKey) LoggerFactory(org.slf4j.LoggerFactory) Mono(reactor.core.publisher.Mono) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) CosmosException(com.azure.cosmos.CosmosException) CosmosPagedFlux(com.azure.cosmos.util.CosmosPagedFlux) AccountSettings(com.azure.cosmos.examples.common.AccountSettings) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) PartitionKey(com.azure.cosmos.models.PartitionKey)

Example 3 with CosmosStoredProcedureRequestOptions

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());
}
Also used : CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties)

Example 4 with 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");
    });
}
Also used : BadRequestException(com.azure.cosmos.implementation.BadRequestException) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) CosmosTestClient(org.eclipse.dataspaceconnector.azure.testfixtures.CosmosTestClient) TypeManager(org.eclipse.dataspaceconnector.spi.types.TypeManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CosmosDatabase(com.azure.cosmos.CosmosDatabase) PartitionKey(com.azure.cosmos.models.PartitionKey) Scanner(java.util.Scanner) TransferProcess(org.eclipse.dataspaceconnector.spi.types.domain.transfer.TransferProcess) TestHelper.createTransferProcess(org.eclipse.dataspaceconnector.transfer.store.cosmos.TestHelper.createTransferProcess) AfterAll(org.junit.jupiter.api.AfterAll) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BeforeAll(org.junit.jupiter.api.BeforeAll) CosmosDbApiImpl(org.eclipse.dataspaceconnector.azure.cosmos.CosmosDbApiImpl) ResourceManifest(org.eclipse.dataspaceconnector.spi.types.domain.transfer.ResourceManifest) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) DataRequest(org.eclipse.dataspaceconnector.spi.types.domain.transfer.DataRequest) Set(java.util.Set) RetryPolicy(net.jodah.failsafe.RetryPolicy) EdcException(org.eclipse.dataspaceconnector.spi.EdcException) CosmosStoredProcedureResponse(com.azure.cosmos.models.CosmosStoredProcedureResponse) UUID(java.util.UUID) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) TransferProcessDocument(org.eclipse.dataspaceconnector.transfer.store.cosmos.model.TransferProcessDocument) Collectors(java.util.stream.Collectors) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) CosmosScripts(com.azure.cosmos.CosmosScripts) CosmosPagedIterable(com.azure.cosmos.util.CosmosPagedIterable) IntegrationTest(org.eclipse.dataspaceconnector.common.annotations.IntegrationTest) CosmosContainer(com.azure.cosmos.CosmosContainer) TransferProcessStates(org.eclipse.dataspaceconnector.spi.types.domain.transfer.TransferProcessStates) CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) TransferProcessDocument(org.eclipse.dataspaceconnector.transfer.store.cosmos.model.TransferProcessDocument) List(java.util.List) CosmosStoredProcedureResponse(com.azure.cosmos.models.CosmosStoredProcedureResponse) Test(org.junit.jupiter.api.Test) IntegrationTest(org.eclipse.dataspaceconnector.common.annotations.IntegrationTest)

Example 5 with CosmosStoredProcedureRequestOptions

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();
}
Also used : CosmosStoredProcedureRequestOptions(com.azure.cosmos.models.CosmosStoredProcedureRequestOptions) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosStoredProcedureResponse(com.azure.cosmos.models.CosmosStoredProcedureResponse)

Aggregations

CosmosStoredProcedureRequestOptions (com.azure.cosmos.models.CosmosStoredProcedureRequestOptions)10 PartitionKey (com.azure.cosmos.models.PartitionKey)7 CosmosStoredProcedureProperties (com.azure.cosmos.models.CosmosStoredProcedureProperties)6 CosmosStoredProcedureResponse (com.azure.cosmos.models.CosmosStoredProcedureResponse)5 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)4 List (java.util.List)3 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)2 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)2 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)2 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)2 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)2 CosmosContainer (com.azure.cosmos.CosmosContainer)2 CosmosDatabase (com.azure.cosmos.CosmosDatabase)2 CustomPOJO (com.azure.cosmos.examples.common.CustomPOJO)2 BadRequestException (com.azure.cosmos.implementation.BadRequestException)2 CosmosPagedIterable (com.azure.cosmos.util.CosmosPagedIterable)2 ChronoUnit (java.time.temporal.ChronoUnit)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 UUID (java.util.UUID)2