Search in sources :

Example 1 with CosmosQueryRequestOptions

use of com.azure.cosmos.models.CosmosQueryRequestOptions in project cas by apereo.

the class CosmosDbServiceRegistry method deleteAll.

@Override
public void deleteAll() {
    LOGGER.debug("Deleting registered services from container [{}]", container.getId());
    val queryOptions = new CosmosQueryRequestOptions();
    val items = container.queryItems("SELECT * FROM " + container.getId(), queryOptions, CosmosDbDocument.class);
    items.iterableByPage().forEach(response -> response.getResults().forEach(doc -> container.deleteItem(doc, new CosmosItemRequestOptions())));
}
Also used : lombok.val(lombok.val) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) CosmosDbDocument(org.apereo.cas.cosmosdb.CosmosDbDocument) MinimalPrettyPrinter(com.fasterxml.jackson.core.util.MinimalPrettyPrinter) Collection(java.util.Collection) PartitionKey(com.azure.cosmos.models.PartitionKey) lombok.val(lombok.val) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) RegisteredServiceJsonSerializer(org.apereo.cas.services.util.RegisteredServiceJsonSerializer) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HttpStatus(org.springframework.http.HttpStatus) LoggingUtils(org.apereo.cas.util.LoggingUtils) Slf4j(lombok.extern.slf4j.Slf4j) StringSerializer(org.apereo.cas.util.serialization.StringSerializer) CosmosContainer(com.azure.cosmos.CosmosContainer) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) CosmosException(com.azure.cosmos.CosmosException) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions)

Example 2 with CosmosQueryRequestOptions

use of com.azure.cosmos.models.CosmosQueryRequestOptions in project cas by apereo.

the class CosmosDbServiceRegistry method load.

@Override
public Collection<RegisteredService> load() {
    val services = new ArrayList<RegisteredService>();
    val queryOptions = new CosmosQueryRequestOptions();
    LOGGER.trace("Loading registered services from container [{}]", container.getId());
    val items = container.queryItems("SELECT * FROM " + container.getId(), queryOptions, CosmosDbDocument.class);
    items.iterableByPage().forEach(response -> services.addAll(response.getResults().stream().map(this::getRegisteredServiceFromDocumentBody).peek(this::invokeServiceRegistryListenerPostLoad).collect(Collectors.toList())));
    return services;
}
Also used : lombok.val(lombok.val) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) ArrayList(java.util.ArrayList)

Example 3 with CosmosQueryRequestOptions

use of com.azure.cosmos.models.CosmosQueryRequestOptions in project cas by apereo.

the class CosmosDbServiceRegistry method findServiceBy.

@Override
public RegisteredService findServiceBy(final String id) {
    val services = new ArrayList<RegisteredService>();
    val queryOptions = new CosmosQueryRequestOptions();
    LOGGER.trace("Loading registered service by [{}] from container [{}]", id, container.getId());
    val query = String.format("SELECT * FROM %s r WHERE r.serviceId LIKE '%s'", container.getId(), '%' + id + '%');
    val items = container.queryItems(query, queryOptions, CosmosDbDocument.class);
    items.iterableByPage().forEach(response -> services.addAll(response.getResults().stream().map(this::getRegisteredServiceFromDocumentBody).sorted().filter(r -> r.matches(id)).peek(this::invokeServiceRegistryListenerPostLoad).collect(Collectors.toList())));
    return services.isEmpty() ? null : services.get(0);
}
Also used : lombok.val(lombok.val) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) ArrayList(java.util.ArrayList)

Example 4 with CosmosQueryRequestOptions

use of com.azure.cosmos.models.CosmosQueryRequestOptions in project YCSB by brianfrankcooper.

the class AzureCosmosClient method scan.

/**
 * Perform a range scan for a set of records in the database. Each field/value
 * pair from the result will be stored in a HashMap.
 *
 * @param table       The name of the table
 * @param startkey    The record key of the first record to read.
 * @param recordcount The number of records to read
 * @param fields      The list of fields to read, or null for all of them
 * @param result      A Vector of HashMaps, where each HashMap is a set
 *                    field/value pairs for one record
 * @return Zero on success, a non-zero error code on error
 */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    try {
        CosmosQueryRequestOptions queryOptions = new CosmosQueryRequestOptions();
        queryOptions.setMaxDegreeOfParallelism(AzureCosmosClient.maxDegreeOfParallelism);
        queryOptions.setMaxBufferedItemCount(AzureCosmosClient.maxBufferedItemCount);
        CosmosContainer container = AzureCosmosClient.containerCache.get(table);
        if (container == null) {
            container = AzureCosmosClient.database.getContainer(table);
            AzureCosmosClient.containerCache.put(table, container);
        }
        List<SqlParameter> paramList = new ArrayList<>();
        paramList.add(new SqlParameter("@startkey", startkey));
        SqlQuerySpec querySpec = new SqlQuerySpec(this.createSelectTop(fields, recordcount) + " FROM root r WHERE r.id >= @startkey", paramList);
        CosmosPagedIterable<ObjectNode> pagedIterable = container.queryItems(querySpec, queryOptions, ObjectNode.class);
        Iterator<FeedResponse<ObjectNode>> pageIterator = pagedIterable.iterableByPage(AzureCosmosClient.preferredPageSize).iterator();
        while (pageIterator.hasNext()) {
            List<ObjectNode> pageDocs = pageIterator.next().getResults();
            for (ObjectNode doc : pageDocs) {
                Map<String, String> stringResults = new HashMap<>(doc.size());
                Iterator<Map.Entry<String, JsonNode>> nodeIterator = doc.fields();
                while (nodeIterator.hasNext()) {
                    Entry<String, JsonNode> pair = nodeIterator.next();
                    stringResults.put(pair.getKey().toString(), pair.getValue().toString());
                }
                HashMap<String, ByteIterator> byteResults = new HashMap<>(doc.size());
                StringByteIterator.putAllAsByteIterators(byteResults, stringResults);
                result.add(byteResults);
            }
        }
        return Status.OK;
    } catch (CosmosException e) {
        if (!AzureCosmosClient.includeExceptionStackInLog) {
            e = null;
        }
        LOGGER.error("Failed to query key {} from collection {} in database {}", startkey, table, AzureCosmosClient.databaseName, e);
    }
    return Status.ERROR;
}
Also used : SqlParameter(com.azure.cosmos.models.SqlParameter) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CosmosContainer(com.azure.cosmos.CosmosContainer) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) FeedResponse(com.azure.cosmos.models.FeedResponse) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) Entry(java.util.Map.Entry) SqlQuerySpec(com.azure.cosmos.models.SqlQuerySpec) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) CosmosException(com.azure.cosmos.CosmosException)

Aggregations

CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)4 ArrayList (java.util.ArrayList)4 lombok.val (lombok.val)3 CosmosContainer (com.azure.cosmos.CosmosContainer)2 CosmosException (com.azure.cosmos.CosmosException)2 CosmosItemRequestOptions (com.azure.cosmos.models.CosmosItemRequestOptions)1 FeedResponse (com.azure.cosmos.models.FeedResponse)1 PartitionKey (com.azure.cosmos.models.PartitionKey)1 SqlParameter (com.azure.cosmos.models.SqlParameter)1 SqlQuerySpec (com.azure.cosmos.models.SqlQuerySpec)1 MinimalPrettyPrinter (com.fasterxml.jackson.core.util.MinimalPrettyPrinter)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Collectors (java.util.stream.Collectors)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CosmosDbDocument (org.apereo.cas.cosmosdb.CosmosDbDocument)1