Search in sources :

Example 1 with CosmosContainer

use of com.azure.cosmos.CosmosContainer in project YCSB by brianfrankcooper.

the class AzureCosmosClient method delete.

@Override
public Status delete(String table, String key) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Delete key {} from table {}", key, table);
    }
    try {
        CosmosContainer container = AzureCosmosClient.containerCache.get(table);
        if (container == null) {
            container = AzureCosmosClient.database.getContainer(table);
            AzureCosmosClient.containerCache.put(table, container);
        }
        container.deleteItem(key, new PartitionKey(key), new CosmosItemRequestOptions());
        return Status.OK;
    } catch (Exception e) {
        if (!AzureCosmosClient.includeExceptionStackInLog) {
            e = null;
        }
        LOGGER.error("Failed to delete key {} in collection {}", key, table, e);
    }
    return Status.ERROR;
}
Also used : CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosContainer(com.azure.cosmos.CosmosContainer) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosException(com.azure.cosmos.CosmosException) DBException(site.ycsb.DBException)

Example 2 with CosmosContainer

use of com.azure.cosmos.CosmosContainer in project YCSB by brianfrankcooper.

the class AzureCosmosClient method update.

/**
 * Update a record in the database. Any field/value pairs in the specified
 * values HashMap will be written into the record with the specified record key,
 * overwriting any existing values with the same field name.
 *
 * @param table  The name of the table
 * @param key    The record key of the record to write.
 * @param values A HashMap of field/value pairs to update in the record
 * @return Zero on success, a non-zero error code on error
 */
@Override
public Status update(String table, String key, Map<String, ByteIterator> values) {
    String readEtag = "";
    // that will be a future improvement.
    for (int attempt = 0; attempt < NUM_UPDATE_ATTEMPTS; attempt++) {
        try {
            CosmosContainer container = AzureCosmosClient.containerCache.get(table);
            if (container == null) {
                container = AzureCosmosClient.database.getContainer(table);
                AzureCosmosClient.containerCache.put(table, container);
            }
            CosmosItemResponse<ObjectNode> response = container.readItem(key, new PartitionKey(key), ObjectNode.class);
            readEtag = response.getETag();
            ObjectNode node = response.getItem();
            for (Entry<String, ByteIterator> pair : values.entrySet()) {
                node.put(pair.getKey(), pair.getValue().toString());
            }
            CosmosItemRequestOptions requestOptions = new CosmosItemRequestOptions();
            requestOptions.setIfMatchETag(readEtag);
            PartitionKey pk = new PartitionKey(key);
            container.replaceItem(node, key, pk, requestOptions);
            return Status.OK;
        } catch (CosmosException e) {
            if (!AzureCosmosClient.includeExceptionStackInLog) {
                e = null;
            }
            LOGGER.error("Failed to update key {} to collection {} in database {} on attempt {}", key, table, AzureCosmosClient.databaseName, attempt, e);
        }
    }
    return Status.ERROR;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosContainer(com.azure.cosmos.CosmosContainer) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosException(com.azure.cosmos.CosmosException)

Example 3 with CosmosContainer

use of com.azure.cosmos.CosmosContainer in project YCSB by brianfrankcooper.

the class AzureCosmosClient method insert.

/**
 * Insert a record in the database. Any field/value pairs in the specified
 * values HashMap will be written into the record with the specified record key.
 *
 * @param table  The name of the table
 * @param key    The record key of the record to insert.
 * @param values A HashMap of field/value pairs to insert in the record
 * @return Zero on success, a non-zero error code on error
 */
@Override
public Status insert(String table, String key, Map<String, ByteIterator> values) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Insert key: {} into table: {}", key, table);
    }
    try {
        CosmosContainer container = AzureCosmosClient.containerCache.get(table);
        if (container == null) {
            container = AzureCosmosClient.database.getContainer(table);
            AzureCosmosClient.containerCache.put(table, container);
        }
        PartitionKey pk = new PartitionKey(key);
        ObjectNode node = OBJECT_MAPPER.createObjectNode();
        node.put("id", key);
        for (Map.Entry<String, ByteIterator> pair : values.entrySet()) {
            node.put(pair.getKey(), pair.getValue().toString());
        }
        if (AzureCosmosClient.useUpsert) {
            container.upsertItem(node, pk, new CosmosItemRequestOptions());
        } else {
            container.createItem(node, pk, new CosmosItemRequestOptions());
        }
        return Status.OK;
    } catch (CosmosException e) {
        if (!AzureCosmosClient.includeExceptionStackInLog) {
            e = null;
        }
        LOGGER.error("Failed to insert key {} to collection {} in database {}", key, table, AzureCosmosClient.databaseName, e);
    }
    return Status.ERROR;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) CosmosItemRequestOptions(com.azure.cosmos.models.CosmosItemRequestOptions) CosmosContainer(com.azure.cosmos.CosmosContainer) PartitionKey(com.azure.cosmos.models.PartitionKey) CosmosException(com.azure.cosmos.CosmosException) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with CosmosContainer

use of com.azure.cosmos.CosmosContainer in project YCSB by brianfrankcooper.

the class AzureCosmosClient method read.

/**
 * Read a record from the database. Each field/value pair from the result will
 * be stored in a HashMap.
 *
 * @param table  The name of the table
 * @param key    The record key of the record to read.
 * @param fields The list of fields to read, or null for all of them
 * @param result A HashMap of field/value pairs for the result
 * @return Zero on success, a non-zero error code on error
 */
@Override
public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) {
    try {
        CosmosContainer container = AzureCosmosClient.containerCache.get(table);
        if (container == null) {
            container = AzureCosmosClient.database.getContainer(table);
            AzureCosmosClient.containerCache.put(table, container);
        }
        CosmosItemResponse<ObjectNode> response = container.readItem(key, new PartitionKey(key), ObjectNode.class);
        ObjectNode node = response.getItem();
        Map<String, String> stringResults = new HashMap<>(node.size());
        if (fields == null) {
            Iterator<Map.Entry<String, JsonNode>> iter = node.fields();
            while (iter.hasNext()) {
                Entry<String, JsonNode> pair = iter.next();
                stringResults.put(pair.getKey().toString(), pair.getValue().toString());
            }
            StringByteIterator.putAllAsByteIterators(result, stringResults);
        } else {
            Iterator<Map.Entry<String, JsonNode>> iter = node.fields();
            while (iter.hasNext()) {
                Entry<String, JsonNode> pair = iter.next();
                if (fields.contains(pair.getKey())) {
                    stringResults.put(pair.getKey().toString(), pair.getValue().toString());
                }
            }
            StringByteIterator.putAllAsByteIterators(result, stringResults);
        }
        return Status.OK;
    } catch (CosmosException e) {
        LOGGER.error("Failed to read key {} in collection {} in database {}", key, table, AzureCosmosClient.databaseName, e);
        return Status.NOT_FOUND;
    }
}
Also used : Entry(java.util.Map.Entry) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CosmosContainer(com.azure.cosmos.CosmosContainer) PartitionKey(com.azure.cosmos.models.PartitionKey) JsonNode(com.fasterxml.jackson.databind.JsonNode) CosmosException(com.azure.cosmos.CosmosException)

Example 5 with CosmosContainer

use of com.azure.cosmos.CosmosContainer 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)5 CosmosException (com.azure.cosmos.CosmosException)5 PartitionKey (com.azure.cosmos.models.PartitionKey)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 CosmosItemRequestOptions (com.azure.cosmos.models.CosmosItemRequestOptions)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ByteIterator (site.ycsb.ByteIterator)3 StringByteIterator (site.ycsb.StringByteIterator)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Entry (java.util.Map.Entry)2 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 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 DBException (site.ycsb.DBException)1