Search in sources :

Example 26 with BigtableDataClient

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

the class BulkMutateIT method test.

@Test(timeout = 60 * 1000)
public void test() throws IOException, InterruptedException {
    BigtableDataSettings settings = testEnvRule.env().getDataClientSettings();
    String rowPrefix = UUID.randomUUID().toString();
    // Set target latency really low so it'll trigger adjusting thresholds
    BigtableDataSettings.Builder builder = settings.toBuilder().enableBatchMutationLatencyBasedThrottling(2L);
    try (BigtableDataClient client = BigtableDataClient.create(builder.build());
        BatcherImpl<RowMutationEntry, Void, BulkMutation, Void> batcher = (BatcherImpl<RowMutationEntry, Void, BulkMutation, Void>) client.newBulkMutationBatcher(testEnvRule.env().getTableId())) {
        FlowControlEventStats events = batcher.getFlowController().getFlowControlEventStats();
        long initialThreashold = Objects.requireNonNull(batcher.getFlowController().getCurrentElementCountLimit());
        assertThat(batcher.getFlowController().getCurrentElementCountLimit()).isNotEqualTo(batcher.getFlowController().getMinElementCountLimit());
        assertThat(batcher.getFlowController().getCurrentElementCountLimit()).isNotEqualTo(batcher.getFlowController().getMaxElementCountLimit());
        String familyId = testEnvRule.env().getFamilyId();
        long initial = batcher.getFlowController().getCurrentElementCountLimit();
        for (long i = 0; i < initial * 3; i++) {
            String key = rowPrefix + "test-key" + i;
            batcher.add(RowMutationEntry.create(key).setCell(familyId, "qualifier", i));
        }
        batcher.flush();
        assertThat(events.getLastFlowControlEvent()).isNotNull();
        // Verify that the threshold is adjusted
        assertThat(batcher.getFlowController().getCurrentElementCountLimit()).isNotEqualTo(initialThreashold);
        // Query a key to make sure the write succeeded
        Row row = testEnvRule.env().getDataClient().readRowsCallable().first().call(Query.create(testEnvRule.env().getTableId()).rowKey(rowPrefix + "test-key" + initial));
        assertThat(row.getCells()).hasSize(1);
    }
}
Also used : BulkMutation(com.google.cloud.bigtable.data.v2.models.BulkMutation) FlowControlEventStats(com.google.api.gax.batching.FlowControlEventStats) RowMutationEntry(com.google.cloud.bigtable.data.v2.models.RowMutationEntry) Row(com.google.cloud.bigtable.data.v2.models.Row) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) BatcherImpl(com.google.api.gax.batching.BatcherImpl) BigtableDataSettings(com.google.cloud.bigtable.data.v2.BigtableDataSettings) Test(org.junit.Test)

Example 27 with BigtableDataClient

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

the class ReadIT method rangeQueries.

@Test
public void rangeQueries() {
    BigtableDataClient client = testEnvRule.env().getDataClient();
    String tableId = testEnvRule.env().getTableId();
    String familyId = testEnvRule.env().getFamilyId();
    String uniqueKey = prefix + "-range-queries";
    String keyA = uniqueKey + "-" + "a";
    String keyZ = uniqueKey + "-" + "z";
    long timestampMicros = System.currentTimeMillis() * 1_000;
    client.bulkMutateRows(BulkMutation.create(tableId).add(RowMutationEntry.create(keyA).setCell(familyId, "", timestampMicros, "A")).add(RowMutationEntry.create(keyZ).setCell(familyId, "", timestampMicros, "Z")));
    Row expectedRowA = Row.create(ByteString.copyFromUtf8(keyA), ImmutableList.of(RowCell.create(testEnvRule.env().getFamilyId(), ByteString.copyFromUtf8(""), timestampMicros, ImmutableList.<String>of(), ByteString.copyFromUtf8("A"))));
    Row expectedRowZ = Row.create(ByteString.copyFromUtf8(keyZ), ImmutableList.of(RowCell.create(testEnvRule.env().getFamilyId(), ByteString.copyFromUtf8(""), timestampMicros, ImmutableList.<String>of(), ByteString.copyFromUtf8("Z"))));
    // Closed/Open
    assertThat(ImmutableList.copyOf(client.readRows(Query.create(tableId).range(ByteStringRange.unbounded().startClosed(keyA).endOpen(keyZ))))).containsExactly(expectedRowA);
    // Closed/Closed
    assertThat(ImmutableList.copyOf(client.readRows(Query.create(tableId).range(ByteStringRange.unbounded().startClosed(keyA).endClosed(keyZ))))).containsExactly(expectedRowA, expectedRowZ);
    // Open/Closed
    assertThat(ImmutableList.copyOf(client.readRows(Query.create(tableId).range(ByteStringRange.unbounded().startOpen(keyA).endClosed(keyZ))))).containsExactly(expectedRowZ);
    // Open/Open
    assertThat(ImmutableList.copyOf(client.readRows(Query.create(tableId).range(ByteStringRange.unbounded().startOpen(keyA).endOpen(keyZ))))).isEmpty();
}
Also used : ByteString(com.google.protobuf.ByteString) Row(com.google.cloud.bigtable.data.v2.models.Row) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) Test(org.junit.Test)

Example 28 with BigtableDataClient

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

the class BigtableDataClient method existsAsync.

/**
 * Confirms asynchronously if given row key exists or not.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * try (BigtableDataClient bigtableDataClient = BigtableDataClient.create("[PROJECT]", "[INSTANCE]")) {
 *   String tableId = "[TABLE]";
 *   final ByteString key = ByteString.copyFromUtf8("key");
 *
 *   ApiFuture<Boolean> futureResult = bigtableDataClient.existsAsync(tableId, key);
 *
 *   ApiFutures.addCallback(futureResult, new ApiFutureCallback<Boolean>() {
 *     public void onFailure(Throwable t) {
 *       t.printStackTrace();
 *     }
 *     public void onSuccess(Boolean isRowPresent) {
 *       if(isRowPresent) {
 *         System.out.println(key.toStringUtf8() + " is present");
 *       }
 *     }
 *   }, MoreExecutors.directExecutor());
 * }
 * }</pre>
 */
public ApiFuture<Boolean> existsAsync(String tableId, ByteString rowKey) {
    Query query = Query.create(tableId).rowKey(rowKey).filter(FILTERS.chain().filter(FILTERS.limit().cellsPerRow(1)).filter(FILTERS.value().strip()));
    ApiFuture<Row> resultFuture = stub.readRowCallable().futureCall(query);
    return ApiFutures.transform(resultFuture, new ApiFunction<Row, Boolean>() {

        @Override
        public Boolean apply(Row row) {
            return row != null;
        }
    }, MoreExecutors.directExecutor());
}
Also used : Query(com.google.cloud.bigtable.data.v2.models.Query) Row(com.google.cloud.bigtable.data.v2.models.Row) ReadModifyWriteRow(com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow)

Example 29 with BigtableDataClient

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

the class Quickstart method quickstart.

public static void quickstart(String projectId, String instanceId, String tableId) {
    BigtableDataSettings settings = BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (BigtableDataClient dataClient = BigtableDataClient.create(settings)) {
        System.out.println("\nReading a single row by row key");
        Row row = dataClient.readRow(tableId, "r1");
        System.out.println("Row: " + row.getKey().toStringUtf8());
        for (RowCell cell : row.getCells()) {
            System.out.printf("Family: %s    Qualifier: %s    Value: %s%n", cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
        }
    } catch (NotFoundException e) {
        System.err.println("Failed to read from a non-existent table: " + e.getMessage());
    } catch (Exception e) {
        System.out.println("Error during quickstart: \n" + e.toString());
    }
}
Also used : 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) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) BigtableDataSettings(com.google.cloud.bigtable.data.v2.BigtableDataSettings) NotFoundException(com.google.api.gax.rpc.NotFoundException)

Example 30 with BigtableDataClient

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

the class Reads method readPrefix.

public static void readPrefix(String projectId, String instanceId, String tableId) {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
        Query query = Query.create(tableId).prefix("phone");
        ServerStream<Row> rows = dataClient.readRows(query);
        for (Row row : rows) {
            printRow(row);
        }
    } catch (IOException e) {
        System.out.println("Unable to initialize service client, as a network error occurred: \n" + e.toString());
    }
}
Also used : Query(com.google.cloud.bigtable.data.v2.models.Query) Row(com.google.cloud.bigtable.data.v2.models.Row) IOException(java.io.IOException) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient)

Aggregations

BigtableDataClient (com.google.cloud.bigtable.data.v2.BigtableDataClient)27 Row (com.google.cloud.bigtable.data.v2.models.Row)18 IOException (java.io.IOException)13 Query (com.google.cloud.bigtable.data.v2.models.Query)8 BulkMutation (com.google.cloud.bigtable.data.v2.models.BulkMutation)7 ByteString (com.google.protobuf.ByteString)7 Test (org.junit.Test)6 BigtableTableAdminClient (com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient)4 BigtableDataSettings (com.google.cloud.bigtable.data.v2.BigtableDataSettings)4 CreateTableRequest (com.google.cloud.bigtable.admin.v2.models.CreateTableRequest)3 RowCell (com.google.cloud.bigtable.data.v2.models.RowCell)3 RowMutation (com.google.cloud.bigtable.data.v2.models.RowMutation)3 RowMutationEntry (com.google.cloud.bigtable.data.v2.models.RowMutationEntry)3 BeforeClass (org.junit.BeforeClass)3 ApiFuture (com.google.api.core.ApiFuture)2 Filters (com.google.cloud.bigtable.data.v2.models.Filters)2 ReadModifyWriteRow (com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow)2 ArrayList (java.util.ArrayList)2 BatcherImpl (com.google.api.gax.batching.BatcherImpl)1 FlowControlEventStats (com.google.api.gax.batching.FlowControlEventStats)1