Search in sources :

Example 1 with BulkMutation

use of com.google.cloud.bigtable.data.v2.models.BulkMutation in project java-docs-samples by GoogleCloudPlatform.

the class ReadsTest method beforeClass.

@BeforeClass
public static void beforeClass() throws IOException {
    projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
    instanceId = requireEnv(INSTANCE_ENV);
    try (BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(projectId, instanceId)) {
        CreateTableRequest createTableRequest = CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_NAME);
        adminClient.createTable(createTableRequest);
        try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
            BulkMutation bulkMutation = BulkMutation.create(TABLE_ID).add("phone#4c410523#20190501", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190405.003")).add("phone#4c410523#20190502", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190405.004")).add("phone#4c410523#20190505", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 0).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190406.000")).add("phone#5c10102#20190501", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190401.002")).add("phone#5c10102#20190502", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 0).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190406.000"));
            dataClient.bulkMutateRows(bulkMutation);
        }
    } catch (Exception e) {
        System.out.println("Error during beforeClass: \n" + e.toString());
        throw (e);
    }
}
Also used : BulkMutation(com.google.cloud.bigtable.data.v2.models.BulkMutation) BigtableTableAdminClient(com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) CreateTableRequest(com.google.cloud.bigtable.admin.v2.models.CreateTableRequest) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 2 with BulkMutation

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

the class EnhancedBigtableStub method createBulkMutateRowsCallable.

/**
 * Creates a callable chain to handle MutatesRows RPCs. This is meant to be used for manual
 * batching. The chain will:
 *
 * <ul>
 *   <li>Convert a {@link BulkMutation} into a {@link MutateRowsRequest}.
 *   <li>Process the response and schedule retries. At the end of each attempt, entries that have
 *       been applied, are filtered from the next attempt. Also, any entries that failed with a
 *       nontransient error, are filtered from the next attempt. This will continue until there
 *       are no more entries or there are no more retry attempts left.
 *   <li>Wrap batch failures in a {@link
 *       com.google.cloud.bigtable.data.v2.models.MutateRowsException}.
 *   <li>Add tracing & metrics.
 * </ul>
 */
private UnaryCallable<BulkMutation, Void> createBulkMutateRowsCallable() {
    UnaryCallable<MutateRowsRequest, Void> baseCallable = createMutateRowsBaseCallable();
    UnaryCallable<MutateRowsRequest, Void> flowControlCallable = null;
    if (settings.bulkMutateRowsSettings().isLatencyBasedThrottlingEnabled()) {
        flowControlCallable = new DynamicFlowControlCallable(baseCallable, bulkMutationFlowController, bulkMutationDynamicFlowControlStats, settings.bulkMutateRowsSettings().getTargetRpcLatencyMs(), FLOW_CONTROL_ADJUSTING_INTERVAL_MS);
    }
    UnaryCallable<BulkMutation, Void> userFacing = new BulkMutateRowsUserFacingCallable(flowControlCallable != null ? flowControlCallable : baseCallable, requestContext);
    SpanName spanName = getSpanName("MutateRows");
    UnaryCallable<BulkMutation, Void> tracedBatcher = new TracedBatcherUnaryCallable<>(userFacing);
    UnaryCallable<BulkMutation, Void> withHeaderTracer = new HeaderTracerUnaryCallable<>(tracedBatcher);
    UnaryCallable<BulkMutation, Void> traced = new TracedUnaryCallable<>(withHeaderTracer, clientContext.getTracerFactory(), spanName);
    return traced.withDefaultCallContext(clientContext.getDefaultCallContext());
}
Also used : BulkMutation(com.google.cloud.bigtable.data.v2.models.BulkMutation) BulkMutateRowsUserFacingCallable(com.google.cloud.bigtable.data.v2.stub.mutaterows.BulkMutateRowsUserFacingCallable) SpanName(com.google.api.gax.tracing.SpanName) TracedUnaryCallable(com.google.api.gax.tracing.TracedUnaryCallable) HeaderTracerUnaryCallable(com.google.cloud.bigtable.data.v2.stub.metrics.HeaderTracerUnaryCallable) MutateRowsRequest(com.google.bigtable.v2.MutateRowsRequest) TracedBatcherUnaryCallable(com.google.cloud.bigtable.data.v2.stub.metrics.TracedBatcherUnaryCallable)

Example 3 with BulkMutation

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

the class BulkReadIT method testBulkRead.

@Test
public void testBulkRead() throws InterruptedException, ExecutionException {
    BigtableDataClient client = testEnvRule.env().getDataClient();
    String family = testEnvRule.env().getFamilyId();
    String rowPrefix = UUID.randomUUID().toString();
    int numRows = 10;
    BulkMutation bulkMutation = BulkMutation.create(testEnvRule.env().getTableId());
    List<Row> expectedRows = new ArrayList<>();
    for (int i = 0; i < numRows; i++) {
        bulkMutation.add(RowMutationEntry.create(rowPrefix + "-" + i).setCell(family, "qualifier", 10_000L, "value-" + i));
        expectedRows.add(Row.create(ByteString.copyFromUtf8(rowPrefix + "-" + i), ImmutableList.of(RowCell.create(family, ByteString.copyFromUtf8("qualifier"), 10_000L, ImmutableList.<String>of(), ByteString.copyFromUtf8("value-" + i)))));
    }
    client.bulkMutateRows(bulkMutation);
    try (Batcher<ByteString, Row> batcher = client.newBulkReadRowsBatcher(testEnvRule.env().getTableId())) {
        List<ApiFuture<Row>> rowFutures = new ArrayList<>(numRows);
        for (int rowCount = 0; rowCount < numRows; rowCount++) {
            ApiFuture<Row> entryResponse = batcher.add(ByteString.copyFromUtf8(rowPrefix + "-" + rowCount));
            rowFutures.add(entryResponse);
        }
        batcher.flush();
        List<Row> actualRows = ApiFutures.allAsList(rowFutures).get();
        assertThat(actualRows).isEqualTo(expectedRows);
        // To verify non-existent and duplicate row keys
        rowFutures = new ArrayList<>();
        // non-existent row key
        rowFutures.add(batcher.add(ByteString.copyFromUtf8(UUID.randomUUID().toString())));
        // duplicate row key
        rowFutures.add(batcher.add(ByteString.copyFromUtf8(rowPrefix + "-" + 0)));
        rowFutures.add(batcher.add(ByteString.copyFromUtf8(rowPrefix + "-" + 0)));
        batcher.flush();
        actualRows = ApiFutures.allAsList(rowFutures).get();
        assertThat(actualRows.get(0)).isNull();
        assertThat(actualRows.get(1)).isEqualTo(expectedRows.get(0));
        assertThat(actualRows.get(2)).isEqualTo(expectedRows.get(0));
    }
}
Also used : BulkMutation(com.google.cloud.bigtable.data.v2.models.BulkMutation) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) ApiFuture(com.google.api.core.ApiFuture) Row(com.google.cloud.bigtable.data.v2.models.Row) Test(org.junit.Test)

Example 4 with BulkMutation

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

the class HeaderTracerCallableTest method testGFELatencyMetricMutateRows.

@Test
public void testGFELatencyMetricMutateRows() throws InterruptedException {
    BulkMutation mutations = BulkMutation.create(TABLE_ID).add("key", Mutation.create().setCell("fake-family", "fake-qualifier", "fake-value"));
    stub.bulkMutateRowsCallable().call(mutations);
    Thread.sleep(WAIT_FOR_METRICS_TIME_MS);
    long latency = StatsTestUtils.getAggregationValueAsLong(localStats, RpcViewConstants.BIGTABLE_GFE_LATENCY_VIEW, ImmutableMap.of(RpcMeasureConstants.BIGTABLE_OP, TagValue.create("Bigtable.MutateRows"), RpcMeasureConstants.BIGTABLE_STATUS, TagValue.create("OK")), PROJECT_ID, INSTANCE_ID, APP_PROFILE_ID);
    assertThat(latency).isEqualTo(fakeServerTiming.get());
}
Also used : BulkMutation(com.google.cloud.bigtable.data.v2.models.BulkMutation) Test(org.junit.Test)

Example 5 with BulkMutation

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

the class WriteBatch method writeBatch.

public static void writeBatch(String projectId, String instanceId, String tableId) {
    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
        long timestamp = System.currentTimeMillis() * 1000;
        BulkMutation bulkMutation = BulkMutation.create(tableId).add("tablet#a0b81f74#20190501", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), timestamp, 1).setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc1")).add("tablet#a0b81f74#20190502", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), timestamp, 1).setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6"));
        dataClient.bulkMutateRows(bulkMutation);
        System.out.print("Successfully wrote 2 rows");
    } catch (Exception e) {
        System.out.println("Error during WriteBatch: \n" + e.toString());
    }
}
Also used : BulkMutation(com.google.cloud.bigtable.data.v2.models.BulkMutation) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient)

Aggregations

BulkMutation (com.google.cloud.bigtable.data.v2.models.BulkMutation)14 BigtableDataClient (com.google.cloud.bigtable.data.v2.BigtableDataClient)7 Test (org.junit.Test)6 Row (com.google.cloud.bigtable.data.v2.models.Row)4 IOException (java.io.IOException)4 RowMutationEntry (com.google.cloud.bigtable.data.v2.models.RowMutationEntry)3 ApiFuture (com.google.api.core.ApiFuture)2 BigtableTableAdminClient (com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient)2 CreateTableRequest (com.google.cloud.bigtable.admin.v2.models.CreateTableRequest)2 Query (com.google.cloud.bigtable.data.v2.models.Query)2 ByteString (com.google.protobuf.ByteString)2 BeforeClass (org.junit.BeforeClass)2 ApiFutures (com.google.api.core.ApiFutures)1 Batcher (com.google.api.gax.batching.Batcher)1 BatcherImpl (com.google.api.gax.batching.BatcherImpl)1 FlowControlEventStats (com.google.api.gax.batching.FlowControlEventStats)1 ResponseObserver (com.google.api.gax.rpc.ResponseObserver)1 ServerStreamingCallable (com.google.api.gax.rpc.ServerStreamingCallable)1 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)1 SpanName (com.google.api.gax.tracing.SpanName)1