/** * Add mutation for a particular row. The changes in the mutation will be applied atomic. However * there is no guarantees about the relative ordering between mutations affecting different rows. */ public BulkMutation add(@Nonnull ByteString rowKey, @Nonnull Mutation mutation) { Preconditions.checkNotNull(rowKey); Preconditions.checkNotNull(mutation); builder.addEntries( MutateRowsRequest.Entry.newBuilder() .setRowKey(rowKey) .addAllMutations(mutation.getMutations()) .build()); return this; }
.addEntries(Entry.newBuilder().setRowKey(ByteString.copyFromUtf8("0-ok"))) .addEntries(Entry.newBuilder().setRowKey(ByteString.copyFromUtf8("1-unavailable"))) .addEntries(Entry.newBuilder().setRowKey(ByteString.copyFromUtf8("2-invalid"))) .build();
private static MutateRowsRequest createRequest(int count) { MutateRowsRequest.Builder request = MutateRowsRequest.newBuilder() .setTableName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID)); for (int i = 0; i < count; i++) { Builder entry = Entry.newBuilder() .addMutations( Mutation.newBuilder() .setSetCell( SetCell.newBuilder() .setFamilyName("family") .setColumnQualifier(ByteString.copyFromUtf8("col" + i)) .setTimestampMicros(1000) .setValue(ByteString.copyFromUtf8("value" + i)))); request.addEntries(entry); } return request.build(); } }
/** * This test ensures that protobuf creation and interactions with {@link BulkMutation} work as * expected. * * @throws IOException * @throws InterruptedException */ @Test public void testWrite() throws IOException, InterruptedException { BigtableService.Writer underTest = new BigtableServiceImpl.BigtableWriterImpl(mockSession, TABLE_NAME); Mutation mutation = Mutation.newBuilder() .setSetCell(SetCell.newBuilder().setFamilyName("Family").build()) .build(); ByteString key = ByteString.copyFromUtf8("key"); SettableFuture<MutateRowResponse> fakeResponse = SettableFuture.create(); when(mockBulkMutation.add(any(MutateRowsRequest.Entry.class))).thenReturn(fakeResponse); underTest.writeRecord(KV.of(key, ImmutableList.of(mutation))); Entry expected = MutateRowsRequest.Entry.newBuilder().setRowKey(key).addMutations(mutation).build(); verify(mockBulkMutation, times(1)).add(expected); underTest.close(); verify(mockBulkMutation, times(1)).flush(); } }
@Override public CompletionStage<MutateRowResponse> writeRecord(KV<ByteString, Iterable<Mutation>> record) throws IOException { MutateRowsRequest.Entry request = MutateRowsRequest.Entry.newBuilder() .setRowKey(record.getKey()) .addAllMutations(record.getValue()) .build(); CompletableFuture<MutateRowResponse> result = new CompletableFuture<>(); Futures.addCallback( bulkMutation.add(request), new FutureCallback<MutateRowResponse>() { @Override public void onSuccess(MutateRowResponse mutateRowResponse) { result.complete(mutateRowResponse); } @Override public void onFailure(Throwable throwable) { result.completeExceptionally(throwable); } }); return result; } }
/** * Creates a single entry bulk {@link com.google.bigtable.v2.MutateRowsRequest}, which will be * merged by the batching logic in the callable chain. */ @InternalApi public MutateRowsRequest toBulkProto(RequestContext requestContext) { String tableName = NameUtil.formatTableName( requestContext.getProjectId(), requestContext.getInstanceId(), tableId); return MutateRowsRequest.newBuilder() .setAppProfileId(requestContext.getAppProfileId()) .setTableName(tableName) .addEntries( Entry.newBuilder().setRowKey(key).addAllMutations(mutation.getMutations()).build()) .build(); } }
public Builder newBuilderForType() { return newBuilder(); } public static Builder newBuilder() {
/** * Add mutation for a particular row. The changes in the mutation will be applied atomic. However * there is no guarantees about the relative ordering between mutations affecting different rows. */ public BulkMutation add(@Nonnull ByteString rowKey, @Nonnull Mutation mutation) { Preconditions.checkNotNull(rowKey); Preconditions.checkNotNull(mutation); builder.addEntries( MutateRowsRequest.Entry.newBuilder() .setRowKey(rowKey) .addAllMutations(mutation.getMutations()) .build()); return this; }
@java.lang.Override public Builder newBuilderForType() { return newBuilder(); }
private static MutateRowsRequest.Entry convert(MutateRowRequest request) { if (request == null) { return null; } else { return MutateRowsRequest.Entry.newBuilder().setRowKey(request.getRowKey()) .addAllMutations(request.getMutationsList()).build(); } }
/** * Creates a single entry bulk {@link com.google.bigtable.v2.MutateRowsRequest}, which will be * merged by the batching logic in the callable chain. */ @InternalApi public MutateRowsRequest toBulkProto(RequestContext requestContext) { String tableName = NameUtil.formatTableName( requestContext.getProjectId(), requestContext.getInstanceId(), tableId); return MutateRowsRequest.newBuilder() .setAppProfileId(requestContext.getAppProfileId()) .setTableName(tableName) .addEntries( Entry.newBuilder().setRowKey(key).addAllMutations(mutation.getMutations()).build()) .build(); } }
@java.lang.Override public Builder newBuilderForType() { return newBuilder(); }