Search in sources :

Example 1 with CosmosStoredProcedureProperties

use of com.azure.cosmos.models.CosmosStoredProcedureProperties in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SampleStoredProcedureAsync method readAllSprocs.

private void readAllSprocs() throws Exception {
    CosmosPagedFlux<CosmosStoredProcedureProperties> fluxResponse = container.getScripts().readAllStoredProcedures();
    final CountDownLatch completionLatch = new CountDownLatch(1);
    fluxResponse.flatMap(storedProcedureProperties -> {
        logger.info(String.format("Stored Procedure: %s\n", storedProcedureProperties.getId()));
        return Mono.empty();
    }).subscribe(s -> {
    }, err -> {
        if (err instanceof CosmosException) {
            // Client-specific errors
            CosmosException cerr = (CosmosException) err;
            cerr.printStackTrace();
            logger.info(String.format("Read Item failed with %s\n", cerr));
        } else {
            // General errors
            err.printStackTrace();
        }
        completionLatch.countDown();
    }, () -> {
        completionLatch.countDown();
    });
    completionLatch.await();
}
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) CosmosException(com.azure.cosmos.CosmosException) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with CosmosStoredProcedureProperties

use of com.azure.cosmos.models.CosmosStoredProcedureProperties 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 3 with CosmosStoredProcedureProperties

use of com.azure.cosmos.models.CosmosStoredProcedureProperties 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 CosmosStoredProcedureProperties

use of com.azure.cosmos.models.CosmosStoredProcedureProperties in project DataSpaceConnector by eclipse-dataspaceconnector.

the class CosmosDbApiImpl method uploadStoredProcedure.

@Override
public void uploadStoredProcedure(String name) {
    // upload stored procedure
    var is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name + ".js");
    if (is == null) {
        throw new AssertionError("The input stream referring to the " + name + " file cannot be null!");
    }
    var s = new Scanner(is).useDelimiter("\\A");
    if (!s.hasNext()) {
        throw new IllegalArgumentException("Error loading resource with name " + ".js");
    }
    var body = s.next();
    var props = new CosmosStoredProcedureProperties(name, body);
    var scripts = container.getScripts();
    if (scripts.readAllStoredProcedures().stream().noneMatch(sp -> sp.getId().equals(name))) {
        scripts.createStoredProcedure(props);
    }
}
Also used : Scanner(java.util.Scanner) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties)

Example 5 with CosmosStoredProcedureProperties

use of com.azure.cosmos.models.CosmosStoredProcedureProperties in project scalardb by scalar-labs.

the class CosmosAdmin method createContainer.

private void createContainer(String database, String table, TableMetadata metadata) throws ExecutionException {
    if (!databaseExists(database)) {
        throw new ExecutionException("the database does not exists");
    }
    CosmosDatabase cosmosDatabase = client.getDatabase(database);
    CosmosContainerProperties properties = computeContainerProperties(table, metadata);
    cosmosDatabase.createContainer(properties);
    CosmosStoredProcedureProperties storedProcedureProperties = computeContainerStoredProcedureProperties();
    cosmosDatabase.getContainer(table).getScripts().createStoredProcedure(storedProcedureProperties);
}
Also used : CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

Aggregations

CosmosStoredProcedureProperties (com.azure.cosmos.models.CosmosStoredProcedureProperties)10 CosmosStoredProcedureRequestOptions (com.azure.cosmos.models.CosmosStoredProcedureRequestOptions)5 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)3 Scanner (java.util.Scanner)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 CustomPOJO (com.azure.cosmos.examples.common.CustomPOJO)2 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)2 PartitionKey (com.azure.cosmos.models.PartitionKey)2 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)2 ArrayList (java.util.ArrayList)2 Logger (org.slf4j.Logger)2 ProxyOptions (com.azure.core.http.ProxyOptions)1 ChangeFeedProcessor (com.azure.cosmos.ChangeFeedProcessor)1 ChangeFeedProcessorBuilder (com.azure.cosmos.ChangeFeedProcessorBuilder)1 CosmosDatabase (com.azure.cosmos.CosmosDatabase)1 CosmosException (com.azure.cosmos.CosmosException)1