Search in sources :

Example 56 with ByteIterator

use of site.ycsb.ByteIterator 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 57 with ByteIterator

use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.

the class AzureClient method readEntity.

private Status readEntity(String key, Map<String, ByteIterator> result) {
    try {
        // firstly, retrieve the entity to be deleted
        TableOperation retrieveOp = TableOperation.retrieve(partitionKey, key, DynamicTableEntity.class);
        DynamicTableEntity entity = cloudTable.execute(retrieveOp).getResultAsType();
        HashMap<String, EntityProperty> properties = entity.getProperties();
        for (Entry<String, EntityProperty> entry : properties.entrySet()) {
            String fieldName = entry.getKey();
            ByteIterator fieldVal = new ByteArrayByteIterator(entry.getValue().getValueAsByteArray());
            result.put(fieldName, fieldVal);
        }
        return Status.OK;
    } catch (Exception e) {
        return Status.ERROR;
    }
}
Also used : DynamicTableEntity(com.microsoft.azure.storage.table.DynamicTableEntity) EntityProperty(com.microsoft.azure.storage.table.EntityProperty) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) TableOperation(com.microsoft.azure.storage.table.TableOperation) DBException(site.ycsb.DBException)

Example 58 with ByteIterator

use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.

the class CassandraCQLClient 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.
 *
 * Cassandra CQL uses "token" method for range scan which doesn't always yield
 * intuitive results.
 *
 * @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 {
        PreparedStatement stmt = (fields == null) ? scanAllStmt.get() : scanStmts.get(fields);
        // Prepare statement on demand
        if (stmt == null) {
            Select.Builder selectBuilder;
            if (fields == null) {
                selectBuilder = QueryBuilder.select().all();
            } else {
                selectBuilder = QueryBuilder.select();
                for (String col : fields) {
                    ((Select.Selection) selectBuilder).column(col);
                }
            }
            Select selectStmt = selectBuilder.from(table);
            // The statement builder is not setup right for tokens.
            // So, we need to build it manually.
            String initialStmt = selectStmt.toString();
            StringBuilder scanStmt = new StringBuilder();
            scanStmt.append(initialStmt.substring(0, initialStmt.length() - 1));
            scanStmt.append(" WHERE ");
            scanStmt.append(QueryBuilder.token(YCSB_KEY));
            scanStmt.append(" >= ");
            scanStmt.append("token(");
            scanStmt.append(QueryBuilder.bindMarker());
            scanStmt.append(")");
            scanStmt.append(" LIMIT ");
            scanStmt.append(QueryBuilder.bindMarker());
            stmt = session.prepare(scanStmt.toString());
            stmt.setConsistencyLevel(readConsistencyLevel);
            if (trace) {
                stmt.enableTracing();
            }
            PreparedStatement prevStmt = (fields == null) ? scanAllStmt.getAndSet(stmt) : scanStmts.putIfAbsent(new HashSet(fields), stmt);
            if (prevStmt != null) {
                stmt = prevStmt;
            }
        }
        logger.debug(stmt.getQueryString());
        logger.debug("startKey = {}, recordcount = {}", startkey, recordcount);
        ResultSet rs = session.execute(stmt.bind(startkey, Integer.valueOf(recordcount)));
        HashMap<String, ByteIterator> tuple;
        while (!rs.isExhausted()) {
            Row row = rs.one();
            tuple = new HashMap<String, ByteIterator>();
            ColumnDefinitions cd = row.getColumnDefinitions();
            for (ColumnDefinitions.Definition def : cd) {
                ByteBuffer val = row.getBytesUnsafe(def.getName());
                if (val != null) {
                    tuple.put(def.getName(), new ByteArrayByteIterator(val.array()));
                } else {
                    tuple.put(def.getName(), null);
                }
            }
            result.add(tuple);
        }
        return Status.OK;
    } catch (Exception e) {
        logger.error(MessageFormatter.format("Error scanning with startkey: {}", startkey).getMessage(), e);
        return Status.ERROR;
    }
}
Also used : ColumnDefinitions(com.datastax.driver.core.ColumnDefinitions) PreparedStatement(com.datastax.driver.core.PreparedStatement) ByteBuffer(java.nio.ByteBuffer) DBException(site.ycsb.DBException) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) Select(com.datastax.driver.core.querybuilder.Select) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) HashSet(java.util.HashSet)

Example 59 with ByteIterator

use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.

the class CassandraCQLClient 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) {
    try {
        Set<String> fields = values.keySet();
        PreparedStatement stmt = updateStmts.get(fields);
        // Prepare statement on demand
        if (stmt == null) {
            Update updateStmt = QueryBuilder.update(table);
            // Add fields
            for (String field : fields) {
                updateStmt.with(QueryBuilder.set(field, QueryBuilder.bindMarker()));
            }
            // Add key
            updateStmt.where(QueryBuilder.eq(YCSB_KEY, QueryBuilder.bindMarker()));
            stmt = session.prepare(updateStmt);
            stmt.setConsistencyLevel(writeConsistencyLevel);
            if (trace) {
                stmt.enableTracing();
            }
            PreparedStatement prevStmt = updateStmts.putIfAbsent(new HashSet(fields), stmt);
            if (prevStmt != null) {
                stmt = prevStmt;
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug(stmt.getQueryString());
            logger.debug("key = {}", key);
            for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
                logger.debug("{} = {}", entry.getKey(), entry.getValue());
            }
        }
        // Add fields
        ColumnDefinitions vars = stmt.getVariables();
        BoundStatement boundStmt = stmt.bind();
        for (int i = 0; i < vars.size() - 1; i++) {
            boundStmt.setString(i, values.get(vars.getName(i)).toString());
        }
        // Add key
        boundStmt.setString(vars.size() - 1, key);
        session.execute(boundStmt);
        return Status.OK;
    } catch (Exception e) {
        logger.error(MessageFormatter.format("Error updating key: {}", key).getMessage(), e);
    }
    return Status.ERROR;
}
Also used : ColumnDefinitions(com.datastax.driver.core.ColumnDefinitions) ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) PreparedStatement(com.datastax.driver.core.PreparedStatement) Update(com.datastax.driver.core.querybuilder.Update) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BoundStatement(com.datastax.driver.core.BoundStatement) DBException(site.ycsb.DBException) HashSet(java.util.HashSet)

Example 60 with ByteIterator

use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.

the class CassandraCQLClientTest method testReadSingleColumn.

@Test
public void testReadSingleColumn() throws Exception {
    insertRow();
    final HashMap<String, ByteIterator> result = new HashMap<String, ByteIterator>();
    final Set<String> fields = Sets.newHashSet("field1");
    final Status status = client.read(TABLE, DEFAULT_ROW_KEY, fields, result);
    assertThat(status, is(Status.OK));
    assertThat(result.entrySet(), hasSize(1));
    final Map<String, String> strResult = StringByteIterator.getStringMap(result);
    assertThat(strResult, hasEntry("field1", "value2"));
}
Also used : Status(site.ycsb.Status) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

ByteIterator (site.ycsb.ByteIterator)131 HashMap (java.util.HashMap)98 StringByteIterator (site.ycsb.StringByteIterator)92 Status (site.ycsb.Status)62 Test (org.junit.Test)53 ByteArrayByteIterator (site.ycsb.ByteArrayByteIterator)34 DBException (site.ycsb.DBException)30 Map (java.util.Map)20 IOException (java.io.IOException)10 Put (org.apache.hadoop.hbase.client.Put)8 ArrayList (java.util.ArrayList)7 Vector (java.util.Vector)7 ByteBuffer (java.nio.ByteBuffer)6 HashSet (java.util.HashSet)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 NumericByteIterator (site.ycsb.NumericByteIterator)5 EntityProperty (com.microsoft.azure.storage.table.EntityProperty)4 Properties (java.util.Properties)4 Assume.assumeNoException (org.junit.Assume.assumeNoException)4 DB (site.ycsb.DB)4