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())));
}
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;
}
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);
}
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;
}
Aggregations