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);
}
}
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());
}
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));
}
}
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());
}
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());
}
}
Aggregations