Search in sources :

Example 1 with ByteArrayByteIterator

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

the class HBaseClient1 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) {
    // 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;
        }
    }
    Scan s = new Scan(Bytes.toBytes(startkey));
    // HBase has no record limit. Here, assume recordcount is small enough to
    // bring back in one call.
    // We get back recordcount records
    s.setCaching(recordcount);
    if (this.usePageFilter) {
        s.setFilter(new PageFilter(recordcount));
    }
    // add specified fields or else all fields
    if (fields == null) {
        s.addFamily(columnFamilyBytes);
    } else {
        for (String field : fields) {
            s.addColumn(columnFamilyBytes, Bytes.toBytes(field));
        }
    }
    // get results
    ResultScanner scanner = null;
    try {
        scanner = currentTable.getScanner(s);
        int numResults = 0;
        for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
            // get row key
            String key = Bytes.toString(rr.getRow());
            if (debug) {
                System.out.println("Got scan result for key: " + key);
            }
            HashMap<String, ByteIterator> rowResult = new HashMap<String, ByteIterator>();
            while (rr.advance()) {
                final Cell cell = rr.current();
                rowResult.put(Bytes.toString(CellUtil.cloneQualifier(cell)), new ByteArrayByteIterator(CellUtil.cloneValue(cell)));
            }
            // add rowResult to result vector
            result.add(rowResult);
            numResults++;
            // break is required.
            if (numResults >= recordcount) {
                // if hit recordcount, bail out
                break;
            }
        }
    // done with row
    } catch (IOException e) {
        if (debug) {
            System.out.println("Error in getting/parsing scan result: " + e);
        }
        return Status.ERROR;
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
    return Status.OK;
}
Also used : ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) HashMap(java.util.HashMap) Scan(org.apache.hadoop.hbase.client.Scan) PageFilter(org.apache.hadoop.hbase.filter.PageFilter) IOException(java.io.IOException) Cell(org.apache.hadoop.hbase.Cell) Result(org.apache.hadoop.hbase.client.Result)

Example 2 with ByteArrayByteIterator

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

the class GoogleBigtableClient method scan.

@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    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 RowRange range = RowRange.newBuilder().setStartKeyClosed(ByteStringer.wrap(startkey.getBytes())).build();
    final RowSet rowSet = RowSet.newBuilder().addRowRanges(range).build();
    final ReadRowsRequest.Builder rrr = ReadRowsRequest.newBuilder().setTableNameBytes(ByteStringer.wrap(lastTableBytes)).setFilter(filter).setRows(rowSet);
    List<Row> rows;
    try {
        rows = client.readRowsAsync(rrr.build()).get();
        if (rows == null || rows.isEmpty()) {
            return Status.NOT_FOUND;
        }
        int numResults = 0;
        for (final Row row : rows) {
            final HashMap<String, ByteIterator> rowResult = new HashMap<String, ByteIterator>(fields != null ? fields.size() : 10);
            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
                        rowResult.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));
                        }
                    }
                }
            }
            result.add(rowResult);
            numResults++;
            if (numResults >= recordcount) {
                // if hit recordcount, bail out
                break;
            }
        }
        return Status.OK;
    } catch (InterruptedException e) {
        System.err.println("Interrupted during scan: " + e);
        Thread.currentThread().interrupt();
        return Status.ERROR;
    } catch (ExecutionException e) {
        System.err.println("Exception during scan: " + e);
        return Status.ERROR;
    }
}
Also used : HashMap(java.util.HashMap) Builder(com.google.bigtable.v2.RowFilter.Chain.Builder) RowSet(com.google.bigtable.v2.RowSet) ReadRowsRequest(com.google.bigtable.v2.ReadRowsRequest) ByteString(com.google.protobuf.ByteString) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) RowFilter(com.google.bigtable.v2.RowFilter) ByteIterator(site.ycsb.ByteIterator) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) RowRange(com.google.bigtable.v2.RowRange) 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)

Example 3 with ByteArrayByteIterator

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

the class CrailClient method read.

@Override
public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) {
    try {
        String path = table + "/" + key;
        CrailKeyValue file = client.lookup(path).get().asKeyValue();
        CrailBufferedInputStream stream = file.getBufferedInputStream(1024);
        while (stream.available() < Integer.BYTES) {
            assert true;
        }
        int fieldKeyLength = stream.readInt();
        while (stream.available() < fieldKeyLength) {
            assert true;
        }
        byte[] fieldKey = new byte[fieldKeyLength];
        int res = stream.read(fieldKey);
        if (res != fieldKey.length) {
            stream.close();
            return Status.ERROR;
        }
        while (stream.available() < Integer.BYTES) {
            assert true;
        }
        int fieldValueLength = stream.readInt();
        while (stream.available() < fieldValueLength) {
            assert true;
        }
        byte[] fieldValue = new byte[fieldValueLength];
        res = stream.read(fieldValue);
        if (res != fieldValue.length) {
            stream.close();
            return Status.ERROR;
        }
        result.put(new String(fieldKey), new ByteArrayByteIterator(fieldValue));
        stream.close();
        return Status.OK;
    } catch (Exception e) {
        LOG.error("Error during read, table " + table + ", key " + key + ", exception " + e.getMessage());
        return new Status("read error", "reading exception");
    }
}
Also used : Status(site.ycsb.Status) ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) CrailKeyValue(org.apache.crail.CrailKeyValue) CrailBufferedInputStream(org.apache.crail.CrailBufferedInputStream) DBException(site.ycsb.DBException)

Example 4 with ByteArrayByteIterator

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

the class HBaseClient1 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 5 with ByteArrayByteIterator

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

the class S3Client method readFromStorage.

/**
 * Download an object from S3.
 *
 * @param bucket
 *            The name of the bucket
 * @param key
 *            The file key of the object to upload/update.
 * @param result
 *            The Hash map where data from the object are written
 */
protected Status readFromStorage(String bucket, String key, Map<String, ByteIterator> result, SSECustomerKey ssecLocal) {
    try {
        S3Object object = getS3ObjectAndMetadata(bucket, key, ssecLocal);
        // consuming the stream
        InputStream objectData = object.getObjectContent();
        // writing the stream to bytes and to results
        result.put(key, new ByteArrayByteIterator(IOUtils.toByteArray(objectData)));
        objectData.close();
        object.close();
    } catch (Exception e) {
        System.err.println("Not possible to get the object " + key);
        e.printStackTrace();
        return Status.ERROR;
    }
    return Status.OK;
}
Also used : ByteArrayByteIterator(site.ycsb.ByteArrayByteIterator) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) S3Object(com.amazonaws.services.s3.model.S3Object) DBException(site.ycsb.DBException)

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