Search in sources :

Example 21 with ByteArrayByteIterator

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

the class HBaseClient2 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
 */
public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) {
    // if this is a "new" table, init HTable object. Else, use existing one
    if (!tableName.equals(table)) {
        currentTable = null;
        try {
            getHTable(table);
            tableName = table;
        } catch (IOException e) {
            System.err.println("Error accessing HBase table: " + e);
            return Status.ERROR;
        }
    }
    Result r = null;
    try {
        if (debug) {
            System.out.println("Doing read from HBase columnfamily " + columnFamily);
            System.out.println("Doing read for key: " + key);
        }
        Get g = new Get(Bytes.toBytes(key));
        if (fields == null) {
            g.addFamily(columnFamilyBytes);
        } else {
            for (String field : fields) {
                g.addColumn(columnFamilyBytes, Bytes.toBytes(field));
            }
        }
        r = currentTable.get(g);
    } catch (IOException e) {
        if (debug) {
            System.err.println("Error doing get: " + e);
        }
        return Status.ERROR;
    } catch (ConcurrentModificationException e) {
        // do nothing for now...need to understand HBase concurrency model better
        return Status.ERROR;
    }
    if (r.isEmpty()) {
        return Status.NOT_FOUND;
    }
    while (r.advance()) {
        final Cell c = r.current();
        result.put(Bytes.toString(CellUtil.cloneQualifier(c)), new ByteArrayByteIterator(CellUtil.cloneValue(c)));
        if (debug) {
            System.out.println("Result for field: " + Bytes.toString(CellUtil.cloneQualifier(c)) + " is: " + Bytes.toString(CellUtil.cloneValue(c)));
        }
    }
    return Status.OK;
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) Get(org.apache.hadoop.hbase.client.Get) IOException(java.io.IOException) Cell(org.apache.hadoop.hbase.Cell) Result(org.apache.hadoop.hbase.client.Result)

Example 22 with ByteArrayByteIterator

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

the class VoltClient4 method unpackRowData.

private Map<String, ByteIterator> unpackRowData(byte[] rowData, ByteBuffer buf, int nFields, Set<String> fields, Map<String, ByteIterator> result) {
    for (int i = 0; i < nFields; i++) {
        int len = buf.getInt();
        int off = buf.position();
        String key = new String(rowData, off, len, UTF8);
        buf.position(off + len);
        len = buf.getInt();
        off = buf.position();
        if (fields == null || fields.contains(key)) {
            result.put(key, new ByteArrayByteIterator(rowData, off, len));
        }
        buf.position(off + len);
    }
    return result;
}
Also used : ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator)

Example 23 with ByteArrayByteIterator

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

the class AzureClient method readSubset.

/*
   * Read subset of properties instead of full fields with projection.
   */
public Status readSubset(String key, Set<String> fields, Map<String, ByteIterator> result) {
    String whereStr = String.format("RowKey eq '%s'", key);
    TableQuery<TableServiceEntity> projectionQuery = TableQuery.from(TableServiceEntity.class).where(whereStr).select(fields.toArray(new String[0]));
    EntityResolver<HashMap<String, ByteIterator>> resolver = new EntityResolver<HashMap<String, ByteIterator>>() {

        public HashMap<String, ByteIterator> resolve(String partitionkey, String rowKey, Date timeStamp, HashMap<String, EntityProperty> properties, String etag) {
            HashMap<String, ByteIterator> tmp = new HashMap<String, ByteIterator>();
            for (Entry<String, EntityProperty> entry : properties.entrySet()) {
                String key = entry.getKey();
                ByteIterator val = new ByteArrayByteIterator(entry.getValue().getValueAsByteArray());
                tmp.put(key, val);
            }
            return tmp;
        }
    };
    try {
        for (HashMap<String, ByteIterator> tmp : cloudTable.execute(projectionQuery, resolver)) {
            for (Entry<String, ByteIterator> entry : tmp.entrySet()) {
                String fieldName = entry.getKey();
                ByteIterator fieldVal = entry.getValue();
                result.put(fieldName, fieldVal);
            }
        }
        return Status.OK;
    } catch (Exception e) {
        return Status.ERROR;
    }
}
Also used : HashMap(java.util.HashMap) EntityResolver(com.microsoft.azure.storage.table.EntityResolver) TableServiceEntity(com.microsoft.azure.storage.table.TableServiceEntity) Date(java.util.Date) DBException(site.ycsb.DBException) EntityProperty(com.microsoft.azure.storage.table.EntityProperty) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator)

Example 24 with ByteArrayByteIterator

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

the class AzureClient method scan.

@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    try {
        String whereStr = String.format("(PartitionKey eq '%s') and (RowKey ge '%s')", partitionKey, startkey);
        TableQuery<DynamicTableEntity> scanQuery = new TableQuery<DynamicTableEntity>(DynamicTableEntity.class).where(whereStr).take(recordcount);
        int cnt = 0;
        for (DynamicTableEntity entity : cloudTable.execute(scanQuery)) {
            HashMap<String, EntityProperty> properties = entity.getProperties();
            HashMap<String, ByteIterator> cur = new HashMap<String, ByteIterator>();
            for (Entry<String, EntityProperty> entry : properties.entrySet()) {
                String fieldName = entry.getKey();
                ByteIterator fieldVal = new ByteArrayByteIterator(entry.getValue().getValueAsByteArray());
                if (fields == null || fields.contains(fieldName)) {
                    cur.put(fieldName, fieldVal);
                }
            }
            result.add(cur);
            if (++cnt == recordcount) {
                break;
            }
        }
        return Status.OK;
    } catch (Exception e) {
        return Status.ERROR;
    }
}
Also used : HashMap(java.util.HashMap) DBException(site.ycsb.DBException) 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) TableQuery(com.microsoft.azure.storage.table.TableQuery)

Example 25 with ByteArrayByteIterator

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

the class CassandraCQLClient 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 {
        PreparedStatement stmt = (fields == null) ? readAllStmt.get() : readStmts.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);
                }
            }
            stmt = session.prepare(selectBuilder.from(table).where(QueryBuilder.eq(YCSB_KEY, QueryBuilder.bindMarker())).limit(1));
            stmt.setConsistencyLevel(readConsistencyLevel);
            if (trace) {
                stmt.enableTracing();
            }
            PreparedStatement prevStmt = (fields == null) ? readAllStmt.getAndSet(stmt) : readStmts.putIfAbsent(new HashSet(fields), stmt);
            if (prevStmt != null) {
                stmt = prevStmt;
            }
        }
        logger.debug(stmt.getQueryString());
        logger.debug("key = {}", key);
        ResultSet rs = session.execute(stmt.bind(key));
        if (rs.isExhausted()) {
            return Status.NOT_FOUND;
        }
        // Should be only 1 row
        Row row = rs.one();
        ColumnDefinitions cd = row.getColumnDefinitions();
        for (ColumnDefinitions.Definition def : cd) {
            ByteBuffer val = row.getBytesUnsafe(def.getName());
            if (val != null) {
                result.put(def.getName(), new ByteArrayByteIterator(val.array()));
            } else {
                result.put(def.getName(), null);
            }
        }
        return Status.OK;
    } catch (Exception e) {
        logger.error(MessageFormatter.format("Error reading key: {}", key).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) Select(com.datastax.driver.core.querybuilder.Select) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) HashSet(java.util.HashSet)

Aggregations

ByteArrayByteIterator (site.ycsb.ByteArrayByteIterator)29 ByteIterator (site.ycsb.ByteIterator)14 DBException (site.ycsb.DBException)12 HashMap (java.util.HashMap)11 IOException (java.io.IOException)8 Status (site.ycsb.Status)5 ByteBuffer (java.nio.ByteBuffer)4 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 DB (site.ycsb.DB)4 EntityProperty (com.microsoft.azure.storage.table.EntityProperty)3 Cell (org.apache.hadoop.hbase.Cell)3 Result (org.apache.hadoop.hbase.client.Result)3 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)2 PreparedStatement (com.datastax.driver.core.PreparedStatement)2 ResultSet (com.datastax.driver.core.ResultSet)2 Row (com.datastax.driver.core.Row)2 Select (com.datastax.driver.core.querybuilder.Select)2 Column (com.google.bigtable.v2.Column)2 Family (com.google.bigtable.v2.Family)2