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