Search in sources :

Example 1 with Row

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

the class BigtableIOTest method testReadingPrimitiveDisplayData.

@Test
public void testReadingPrimitiveDisplayData() throws IOException, InterruptedException {
    final String table = "fooTable";
    service.createTable(table);
    RowFilter rowFilter = RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8("foo.*")).build();
    DisplayDataEvaluator evaluator = DisplayDataEvaluator.create();
    BigtableIO.Read read = BigtableIO.read().withBigtableOptions(BIGTABLE_OPTIONS).withTableId(table).withRowFilter(rowFilter).withBigtableService(service);
    Set<DisplayData> displayData = evaluator.displayDataForPrimitiveSourceTransforms(read);
    assertThat("BigtableIO.Read should include the table id in its primitive display data", displayData, Matchers.hasItem(hasDisplayItem("tableId")));
    assertThat("BigtableIO.Read should include the row filter, if it exists, in its primitive " + "display data", displayData, Matchers.hasItem(hasDisplayItem("rowFilter")));
}
Also used : RowFilter(com.google.bigtable.v2.RowFilter) ByteString(com.google.protobuf.ByteString) DisplayDataEvaluator(org.apache.beam.sdk.transforms.display.DisplayDataEvaluator) DisplayData(org.apache.beam.sdk.transforms.display.DisplayData) Test(org.junit.Test)

Example 2 with Row

use of com.google.bigtable.v2.Row in project YCSB by brianfrankcooper.

the class GoogleBigtableClient method read.

@Override
public Status read(String table, String key, Set<String> fields, HashMap<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).setRowKey(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.v1.RowFilter.Chain.Builder) ReadRowsRequest(com.google.bigtable.v1.ReadRowsRequest) ByteString(com.google.bigtable.repackaged.com.google.protobuf.ByteString) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) RowFilter(com.google.bigtable.v1.RowFilter) Column(com.google.bigtable.v1.Column) Family(com.google.bigtable.v1.Family) DeleteFromRow(com.google.bigtable.v1.Mutation.DeleteFromRow) Row(com.google.bigtable.v1.Row) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with Row

use of com.google.bigtable.v2.Row 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().setStartKey(ByteStringer.wrap(startkey.getBytes())).build();
    final ReadRowsRequest.Builder rrr = ReadRowsRequest.newBuilder().setTableNameBytes(ByteStringer.wrap(lastTableBytes)).setFilter(filter).setRowRange(range);
    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.v1.RowFilter.Chain.Builder) ReadRowsRequest(com.google.bigtable.v1.ReadRowsRequest) ByteString(com.google.bigtable.repackaged.com.google.protobuf.ByteString) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) RowFilter(com.google.bigtable.v1.RowFilter) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) RowRange(com.google.bigtable.v1.RowRange) Column(com.google.bigtable.v1.Column) Family(com.google.bigtable.v1.Family) DeleteFromRow(com.google.bigtable.v1.Mutation.DeleteFromRow) Row(com.google.bigtable.v1.Row) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with Row

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

the class FamiliesReadImplTest method testParentDataTypeToDataType.

@Test
public void testParentDataTypeToDataType() throws Exception {
    assertEquals(Lists.newArrayList(), familiesRead.parentTypeToCurrentType().apply(Optional.empty()));
    assertEquals(Lists.newArrayList(), familiesRead.parentTypeToCurrentType().apply(Optional.of(Row.getDefaultInstance())));
    final Family family = Family.getDefaultInstance();
    final Row row = Row.newBuilder().addFamilies(family).build();
    assertEquals(ImmutableList.of(family), familiesRead.parentTypeToCurrentType().apply(Optional.of(row)));
}
Also used : Family(com.google.bigtable.v2.Family) Row(com.google.bigtable.v2.Row) Test(org.junit.Test)

Example 5 with Row

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

the class FamilyReadImplTest method testParentDataTypeToDataType.

@Test
public void testParentDataTypeToDataType() throws Exception {
    assertEquals(Optional.empty(), familyRead.parentTypeToCurrentType().apply(Optional.empty()));
    assertEquals(Optional.empty(), familyRead.parentTypeToCurrentType().apply(Optional.of(Row.getDefaultInstance())));
    final Family family = Family.getDefaultInstance();
    final Row row = Row.newBuilder().addFamilies(family).build();
    assertEquals(Optional.of(family), familyRead.parentTypeToCurrentType().apply(Optional.of(row)));
}
Also used : Family(com.google.bigtable.v2.Family) Row(com.google.bigtable.v2.Row) Test(org.junit.Test)

Aggregations

Row (com.google.bigtable.v2.Row)16 ByteString (com.google.protobuf.ByteString)14 Test (org.junit.Test)14 Family (com.google.bigtable.v2.Family)8 Column (com.google.bigtable.v2.Column)6 RowFilter (com.google.bigtable.v2.RowFilter)6 ByteKey (org.apache.beam.sdk.io.range.ByteKey)6 ByteKeyRange (org.apache.beam.sdk.io.range.ByteKeyRange)5 ReadRowsRequest (com.google.bigtable.v2.ReadRowsRequest)4 HashMap (java.util.HashMap)4 Cell (com.google.bigtable.v2.Cell)3 Mutation (com.google.bigtable.v2.Mutation)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ExecutionException (java.util.concurrent.ExecutionException)3 BigtableSource (org.apache.beam.sdk.io.gcp.bigtable.BigtableIO.BigtableSource)3 ByteString (com.google.bigtable.repackaged.com.google.protobuf.ByteString)2 Column (com.google.bigtable.v1.Column)2 Family (com.google.bigtable.v1.Family)2 DeleteFromRow (com.google.bigtable.v1.Mutation.DeleteFromRow)2