Search in sources :

Example 1 with SqlParameter

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

CosmosContainer (com.azure.cosmos.CosmosContainer)1 CosmosException (com.azure.cosmos.CosmosException)1 CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)1 FeedResponse (com.azure.cosmos.models.FeedResponse)1 SqlParameter (com.azure.cosmos.models.SqlParameter)1 SqlQuerySpec (com.azure.cosmos.models.SqlQuerySpec)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ByteIterator (site.ycsb.ByteIterator)1 StringByteIterator (site.ycsb.StringByteIterator)1