Search in sources :

Example 16 with RowCell

use of com.google.cloud.bigtable.data.v2.models.RowCell in project java-bigtable by googleapis.

the class HelloWorld method readTable.

/**
 * Demonstrates how to read an entire table.
 */
public List<Row> readTable() {
    // [START bigtable_hw_scan_all]
    try {
        System.out.println("\nReading the entire table");
        Query query = Query.create(tableId);
        ServerStream<Row> rowStream = dataClient.readRows(query);
        List<Row> tableRows = new ArrayList<>();
        for (Row r : rowStream) {
            System.out.println("Row Key: " + r.getKey().toStringUtf8());
            tableRows.add(r);
            for (RowCell cell : r.getCells()) {
                System.out.printf("Family: %s    Qualifier: %s    Value: %s%n", cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
            }
        }
        return tableRows;
    } catch (NotFoundException e) {
        System.err.println("Failed to read a non-existent table: " + e.getMessage());
        return null;
    }
// [END bigtable_hw_scan_all]
}
Also used : Query(com.google.cloud.bigtable.data.v2.models.Query) ArrayList(java.util.ArrayList) RowCell(com.google.cloud.bigtable.data.v2.models.RowCell) NotFoundException(com.google.api.gax.rpc.NotFoundException) Row(com.google.cloud.bigtable.data.v2.models.Row)

Example 17 with RowCell

use of com.google.cloud.bigtable.data.v2.models.RowCell in project java-bigtable by googleapis.

the class Reads method printRow.

// [END bigtable_reads_filter]
// [END_EXCLUDE]
private static void printRow(Row row) {
    System.out.printf("Reading data for %s%n", row.getKey().toStringUtf8());
    String colFamily = "";
    for (RowCell cell : row.getCells()) {
        if (!cell.getFamily().equals(colFamily)) {
            colFamily = cell.getFamily();
            System.out.printf("Column Family %s%n", colFamily);
        }
        System.out.printf("\t%s: %s @%s%n", cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8(), cell.getTimestamp());
    }
    System.out.println();
}
Also used : RowCell(com.google.cloud.bigtable.data.v2.models.RowCell)

Example 18 with RowCell

use of com.google.cloud.bigtable.data.v2.models.RowCell in project java-bigtable by googleapis.

the class BigtableDataClientTest method proxyNewBulkReadRowsTest.

@Test
public void proxyNewBulkReadRowsTest() {
    Mockito.when(mockStub.newBulkReadRowsBatcher(Mockito.any(Query.class), Mockito.any())).thenReturn(mockBulkReadRowsBatcher);
    ApiFuture<Row> expectedResponse = ApiFutures.immediateFuture(Row.create(ByteString.copyFromUtf8("fake-row-key"), Collections.<RowCell>emptyList()));
    ByteString request = ByteString.copyFromUtf8("fake-row-key");
    Batcher<ByteString, Row> batcher = bigtableDataClient.newBulkReadRowsBatcher("fake-table");
    Mockito.when(mockBulkReadRowsBatcher.add(request)).thenReturn(expectedResponse);
    ApiFuture<Row> actualResponse = batcher.add(request);
    assertThat(actualResponse).isSameInstanceAs(expectedResponse);
    Mockito.verify(mockStub).newBulkReadRowsBatcher(Mockito.any(Query.class), Mockito.any());
}
Also used : Query(com.google.cloud.bigtable.data.v2.models.Query) ByteString(com.google.protobuf.ByteString) RowCell(com.google.cloud.bigtable.data.v2.models.RowCell) Row(com.google.cloud.bigtable.data.v2.models.Row) ReadModifyWriteRow(com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow) Test(org.junit.Test)

Example 19 with RowCell

use of com.google.cloud.bigtable.data.v2.models.RowCell in project java-bigtable-hbase by googleapis.

the class TestBigtableTable method getScanner_withBigtableResultScannerAdapter.

@Test
public void getScanner_withBigtableResultScannerAdapter() throws IOException {
    when(mockBigtableDataClient.readRows(isA(Query.class))).thenReturn(mockResultScanner);
    // A row with no matching label. In case of {@link BigtableResultScannerAdapter} the result is
    // non-null.
    Result expected = Result.create(ImmutableList.<Cell>of(new RowCell(Bytes.toBytes("row_key"), Bytes.toBytes("family_name"), Bytes.toBytes("q_name"), 0, ByteString.EMPTY.toByteArray(), Collections.singletonList("label-in")), new RowCell(Bytes.toBytes("row_key"), Bytes.toBytes("family_name"), Bytes.toBytes("q_name"), 0, Bytes.toBytes("value"))));
    when(mockResultScanner.next()).thenReturn(expected);
    QualifierFilter filter = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("x")));
    Scan scan = new Scan();
    scan.setFilter(filter);
    org.apache.hadoop.hbase.client.ResultScanner resultScanner = table.getScanner(scan);
    Result result = resultScanner.next();
    assertEquals("row_key", new String(result.getRow()));
    List<org.apache.hadoop.hbase.Cell> cells = result.getColumnCells("family_name".getBytes(), "q_name".getBytes());
    // HBase ResultScanner now allows cells with labels
    assertEquals(2, cells.size());
    assertEquals("", new String(CellUtil.cloneValue(cells.get(0))));
    assertEquals("value", new String(CellUtil.cloneValue(cells.get(1))));
    verify(mockBigtableDataClient).readRows(isA(Query.class));
    verify(mockResultScanner).next();
}
Also used : Query(com.google.cloud.bigtable.data.v2.models.Query) RowCell(com.google.cloud.bigtable.hbase.adapters.read.RowCell) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ByteString(com.google.protobuf.ByteString) BinaryComparator(org.apache.hadoop.hbase.filter.BinaryComparator) Result(org.apache.hadoop.hbase.client.Result) QualifierFilter(org.apache.hadoop.hbase.filter.QualifierFilter) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) Scan(org.apache.hadoop.hbase.client.Scan) Cell(org.apache.hadoop.hbase.Cell) RowCell(com.google.cloud.bigtable.hbase.adapters.read.RowCell) Test(org.junit.Test)

Example 20 with RowCell

use of com.google.cloud.bigtable.data.v2.models.RowCell in project java-bigtable-hbase by googleapis.

the class TestBatchExecutor method testPartialResults.

@Test
public void testPartialResults() throws Exception {
    when(mockBigtableApi.getDataClient()).thenReturn(mockDataClientWrapper);
    when(mockDataClientWrapper.createBulkRead(isA(String.class))).thenReturn(mockBulkRead);
    byte[] key1 = randomBytes(8);
    byte[] key2 = randomBytes(8);
    Result expected = Result.create(ImmutableList.<org.apache.hadoop.hbase.Cell>of(new RowCell(key1, Bytes.toBytes("cf"), Bytes.toBytes(""), 10, Bytes.toBytes("hi!"), ImmutableList.<String>of())));
    RuntimeException exception = new RuntimeException("Something bad happened");
    when(mockBulkRead.add(any(ByteString.class), any(Filters.Filter.class))).thenReturn(ApiFutures.immediateFuture(expected)).thenReturn(ApiFutures.<Result>immediateFailedFuture(exception));
    List<Get> gets = Arrays.asList(new Get(key1), new Get(key2));
    Object[] results = new Object[2];
    try {
        createExecutor().batch(gets, results);
    } catch (RetriesExhaustedWithDetailsException ignored) {
    }
    assertTrue("first result is a result", results[0] instanceof Result);
    assertTrue(matchesRow(expected).matches(results[0]));
    Assert.assertEquals(exception, results[1]);
}
Also used : RowCell(com.google.cloud.bigtable.hbase.adapters.read.RowCell) ByteString(com.google.protobuf.ByteString) Result(org.apache.hadoop.hbase.client.Result) Filters(com.google.cloud.bigtable.data.v2.models.Filters) RetriesExhaustedWithDetailsException(org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException) Get(org.apache.hadoop.hbase.client.Get) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)22 Row (com.google.cloud.bigtable.data.v2.models.Row)21 RowCell (com.google.cloud.bigtable.data.v2.models.RowCell)16 Query (com.google.cloud.bigtable.data.v2.models.Query)10 ByteString (com.google.protobuf.ByteString)10 Result (org.apache.hadoop.hbase.client.Result)10 ReadModifyWriteRow (com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow)9 RowCell (com.google.cloud.bigtable.hbase.adapters.read.RowCell)8 NotFoundException (com.google.api.gax.rpc.NotFoundException)4 RowAdapter (com.google.cloud.bigtable.data.v2.models.RowAdapter)4 QualifierFilter (org.apache.hadoop.hbase.filter.QualifierFilter)3 ReadRowsRequest (com.google.bigtable.v2.ReadRowsRequest)2 ReadRowsResponse (com.google.bigtable.v2.ReadRowsResponse)2 BigtableDataClient (com.google.cloud.bigtable.data.v2.BigtableDataClient)2 DefaultRowAdapter (com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter)2 Filters (com.google.cloud.bigtable.data.v2.models.Filters)2 Filter (com.google.cloud.bigtable.data.v2.models.Filters.Filter)2 ServerStreamingStashCallable (com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi.ServerStreamingStashCallable)2 Cell (org.apache.hadoop.hbase.Cell)2 Get (org.apache.hadoop.hbase.client.Get)2