Search in sources :

Example 1 with Cell

use of com.google.bigtable.v2.Cell in project simple-bigtable by spotify.

the class CellReadImplTest method testParentDataTypeToDataType.

@Test
public void testParentDataTypeToDataType() throws Exception {
    assertEquals(Optional.empty(), cellRead.parentTypeToCurrentType().apply(Collections.emptyList()));
    final Cell cell = Cell.getDefaultInstance();
    assertEquals(Optional.of(cell), cellRead.parentTypeToCurrentType().apply(Collections.singletonList(cell)));
}
Also used : Cell(com.google.bigtable.v2.Cell) Test(org.junit.Test)

Example 2 with Cell

use of com.google.bigtable.v2.Cell in project beam by apache.

the class BigtableTableTestUtils method setFixedTimestamp.

// There is no possibility to insert a value with fixed timestamp so we have to replace it
// for the testing purpose.
static com.google.bigtable.v2.Row setFixedTimestamp(com.google.bigtable.v2.Row row) {
    Family family = row.getFamilies(0);
    List<Column> columnsReplaced = family.getColumnsList().stream().map(column -> {
        Cell cell = column.getCells(0);
        return column(column.getQualifier().toStringUtf8(), cell.getValue().toByteArray());
    }).collect(toList());
    Family familyReplaced = Family.newBuilder().setName(family.getName()).addAllColumns(columnsReplaced).build();
    return com.google.bigtable.v2.Row.newBuilder().setKey(row.getKey()).addFamilies(familyReplaced).build();
}
Also used : BigtableUtils.byteStringUtf8(org.apache.beam.sdk.io.gcp.testing.BigtableUtils.byteStringUtf8) BigtableUtils.doubleToByteArray(org.apache.beam.sdk.io.gcp.testing.BigtableUtils.doubleToByteArray) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Cell(com.google.bigtable.v2.Cell) Longs(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.primitives.Longs) Family(com.google.bigtable.v2.Family) Column(com.google.bigtable.v2.Column) LABELS(org.apache.beam.sdk.io.gcp.bigtable.RowUtils.LABELS) Schema(org.apache.beam.sdk.schemas.Schema) BigtableUtils.booleanToByteArray(org.apache.beam.sdk.io.gcp.testing.BigtableUtils.booleanToByteArray) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) KEY(org.apache.beam.sdk.io.gcp.bigtable.RowUtils.KEY) BigtableUtils.longToByteArray(org.apache.beam.sdk.io.gcp.testing.BigtableUtils.longToByteArray) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Assert.fail(org.junit.Assert.fail) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Row(org.apache.beam.sdk.values.Row) Matchers.containsString(org.hamcrest.Matchers.containsString) TIMESTAMP_MICROS(org.apache.beam.sdk.io.gcp.bigtable.RowUtils.TIMESTAMP_MICROS) VALUE(org.apache.beam.sdk.io.gcp.bigtable.RowUtils.VALUE) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BigtableUtils.byteString(org.apache.beam.sdk.io.gcp.testing.BigtableUtils.byteString) Column(com.google.bigtable.v2.Column) Family(com.google.bigtable.v2.Family) Cell(com.google.bigtable.v2.Cell)

Example 3 with Cell

use of com.google.bigtable.v2.Cell 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 4 with Cell

use of com.google.bigtable.v2.Cell in project simple-bigtable by spotify.

the class CellsReadImplTest method testParentDataTypeToDataType.

@Test
public void testParentDataTypeToDataType() throws Exception {
    assertEquals(Lists.newArrayList(), cellsRead.parentTypeToCurrentType().apply(Optional.empty()));
    assertEquals(Lists.newArrayList(), cellsRead.parentTypeToCurrentType().apply(Optional.of(Column.getDefaultInstance())));
    final Cell cell = Cell.getDefaultInstance();
    final Column column = Column.newBuilder().addCells(cell).build();
    assertEquals(ImmutableList.of(cell), cellsRead.parentTypeToCurrentType().apply(Optional.of(column)));
}
Also used : Column(com.google.bigtable.v2.Column) Cell(com.google.bigtable.v2.Cell) Test(org.junit.Test)

Example 5 with Cell

use of com.google.bigtable.v2.Cell 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

Column (com.google.bigtable.v2.Column)4 Cell (com.google.bigtable.v2.Cell)3 Family (com.google.bigtable.v2.Family)3 DeleteFromRow (com.google.bigtable.v2.Mutation.DeleteFromRow)2 ReadRowsRequest (com.google.bigtable.v2.ReadRowsRequest)2 Row (com.google.bigtable.v2.Row)2 RowFilter (com.google.bigtable.v2.RowFilter)2 Builder (com.google.bigtable.v2.RowFilter.Chain.Builder)2 ByteString (com.google.protobuf.ByteString)2 ExecutionException (java.util.concurrent.ExecutionException)2 Test (org.junit.Test)2 ByteArrayByteIterator (site.ycsb.ByteArrayByteIterator)2 RowRange (com.google.bigtable.v2.RowRange)1 RowSet (com.google.bigtable.v2.RowSet)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 KEY (org.apache.beam.sdk.io.gcp.bigtable.RowUtils.KEY)1 LABELS (org.apache.beam.sdk.io.gcp.bigtable.RowUtils.LABELS)1