Search in sources :

Example 16 with ByteArrayByteIterator

use of site.ycsb.ByteArrayByteIterator 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 17 with ByteArrayByteIterator

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

the class AccumuloClient method read.

@Override
public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) {
    Scanner scanner = null;
    try {
        scanner = getRow(table, new Text(key), null);
        // Pick out the results we care about.
        final Text cq = new Text();
        for (Entry<Key, Value> entry : scanner) {
            entry.getKey().getColumnQualifier(cq);
            Value v = entry.getValue();
            byte[] buf = v.get();
            result.put(cq.toString(), new ByteArrayByteIterator(buf));
        }
    } catch (Exception e) {
        System.err.println("Error trying to reading Accumulo table " + table + " " + key);
        e.printStackTrace();
        return Status.ERROR;
    } finally {
        if (null != scanner) {
            scanner.close();
        }
    }
    return Status.OK;
}
Also used : Scanner(org.apache.accumulo.core.client.Scanner) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) Value(org.apache.accumulo.core.data.Value) Text(org.apache.hadoop.io.Text) Key(org.apache.accumulo.core.data.Key) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) IOException(java.io.IOException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) DBException(site.ycsb.DBException)

Example 18 with ByteArrayByteIterator

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

the class GridDBClient method read.

public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) {
    try {
        Object rowKey = makeRowKey(key);
        String containerKey = makeContainerKey(key);
        final Container<Object, Row> container = store.getContainer(containerKey);
        if (container == null) {
            LOGGER.severe("[ERROR]getCollection " + containerKey + " in read()");
            return Status.ERROR;
        }
        Row targetRow = container.get(rowKey);
        if (targetRow == null) {
            LOGGER.severe("[ERROR]get(rowKey) in read()");
            return Status.ERROR;
        }
        for (int i = 1; i < containerInfo.getColumnCount(); i++) {
            result.put(containerInfo.getColumnInfo(i).getName(), new ByteArrayByteIterator(targetRow.getValue(i).toString().getBytes()));
        }
        return Status.OK;
    } catch (GSException e) {
        LOGGER.severe("Exception: " + e.getMessage());
        return Status.ERROR;
    }
}
Also used : ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) Row(com.toshiba.mwcloud.gs.Row) GSException(com.toshiba.mwcloud.gs.GSException)

Example 19 with ByteArrayByteIterator

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

the class GeodeClient method read.

@Override
public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) {
    Region<String, PdxInstance> r = getRegion(table);
    PdxInstance val = r.get(key);
    if (val != null) {
        if (fields == null) {
            for (String fieldName : val.getFieldNames()) {
                result.put(fieldName, new ByteArrayByteIterator((byte[]) val.getField(fieldName)));
            }
        } else {
            for (String field : fields) {
                result.put(field, new ByteArrayByteIterator((byte[]) val.getField(field)));
            }
        }
        return Status.OK;
    }
    return Status.ERROR;
}
Also used : ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) PdxInstance(org.apache.geode.pdx.PdxInstance)

Example 20 with ByteArrayByteIterator

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

the class GoogleBigtableClient method read.

@Override
public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) {
    if (debug) {
        System.out.println("Doing read from Bigtable columnfamily " + new String(columnFamilyBytes));
        System.out.println("Doing read for key: " + key);
    }
    setTable(table);
    RowFilter filter = RowFilter.newBuilder().setFamilyNameRegexFilterBytes(ByteStringer.wrap(columnFamilyBytes)).build();
    if (fields != null && fields.size() > 0) {
        Builder filterChain = RowFilter.Chain.newBuilder();
        filterChain.addFilters(filter);
        filterChain.addFilters(RowFilter.newBuilder().setCellsPerColumnLimitFilter(1).build());
        int count = 0;
        // usually "field#" so pre-alloc
        final StringBuilder regex = new StringBuilder(fields.size() * 6);
        for (final String field : fields) {
            if (count++ > 0) {
                regex.append("|");
            }
            regex.append(field);
        }
        filterChain.addFilters(RowFilter.newBuilder().setColumnQualifierRegexFilter(ByteStringer.wrap(regex.toString().getBytes()))).build();
        filter = RowFilter.newBuilder().setChain(filterChain.build()).build();
    }
    final ReadRowsRequest.Builder rrr = ReadRowsRequest.newBuilder().setTableNameBytes(ByteStringer.wrap(lastTableBytes)).setFilter(filter).setRows(RowSet.newBuilder().addRowKeys(ByteStringer.wrap(key.getBytes())));
    List<Row> rows;
    try {
        rows = client.readRowsAsync(rrr.build()).get();
        if (rows == null || rows.isEmpty()) {
            return Status.NOT_FOUND;
        }
        for (final Row row : rows) {
            for (final Family family : row.getFamiliesList()) {
                if (Arrays.equals(family.getNameBytes().toByteArray(), columnFamilyBytes)) {
                    for (final Column column : family.getColumnsList()) {
                        // we should only have a single cell per column
                        result.put(column.getQualifier().toString(UTF8_CHARSET), new ByteArrayByteIterator(column.getCells(0).getValue().toByteArray()));
                        if (debug) {
                            System.out.println("Result for field: " + column.getQualifier().toString(UTF8_CHARSET) + " is: " + column.getCells(0).getValue().toString(UTF8_CHARSET));
                        }
                    }
                }
            }
        }
        return Status.OK;
    } catch (InterruptedException e) {
        System.err.println("Interrupted during get: " + e);
        Thread.currentThread().interrupt();
        return Status.ERROR;
    } catch (ExecutionException e) {
        System.err.println("Exception during get: " + e);
        return Status.ERROR;
    }
}
Also used : Builder(com.google.bigtable.v2.RowFilter.Chain.Builder) ReadRowsRequest(com.google.bigtable.v2.ReadRowsRequest) ByteString(com.google.protobuf.ByteString) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) RowFilter(com.google.bigtable.v2.RowFilter) Column(com.google.bigtable.v2.Column) Family(com.google.bigtable.v2.Family) Row(com.google.bigtable.v2.Row) DeleteFromRow(com.google.bigtable.v2.Mutation.DeleteFromRow) ExecutionException(java.util.concurrent.ExecutionException)

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