@Override public Mutation setCell( @Nonnull String familyName, @Nonnull ByteString qualifier, long timestamp, @Nonnull ByteString value) { Validations.validateFamily(familyName); Preconditions.checkNotNull(qualifier, "qualifier can't be null."); Preconditions.checkNotNull(value, "value can't be null."); if (!allowServersideTimestamp) { Preconditions.checkArgument( timestamp != SERVER_SIDE_TIMESTAMP, "Serverside timestamps are not supported"); } addMutation( com.google.bigtable.v2.Mutation.newBuilder() .setSetCell( SetCell.newBuilder() .setFamilyName(familyName) .setColumnQualifier(qualifier) .setTimestampMicros(timestamp) .setValue(value) .build()) .build()); return this; }
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(); } }
@Override public Mutation setCell( @Nonnull String familyName, @Nonnull ByteString qualifier, long timestamp, @Nonnull ByteString value) { Validations.validateFamily(familyName); Preconditions.checkNotNull(qualifier, "qualifier can't be null."); Preconditions.checkNotNull(value, "value can't be null."); if (!allowServersideTimestamp) { Preconditions.checkArgument( timestamp != SERVER_SIDE_TIMESTAMP, "Serverside timestamps are not supported"); } addMutation( com.google.bigtable.v2.Mutation.newBuilder() .setSetCell( SetCell.newBuilder() .setFamilyName(familyName) .setColumnQualifier(qualifier) .setTimestampMicros(timestamp) .setValue(value) .build()) .build()); return this; }
/** * 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 KV<ByteString, Iterable<Mutation>> apply(BigtableRow row) { ByteString key = toByteString(row.getKey()); // BulkMutation doesn't split rows. Currently, if a single row contains more than 100,000 // mutations, the service will fail the request. ImmutableList.Builder<Mutation> mutations = ImmutableList.builder(); for (BigtableCell cell : row.getCells()) { SetCell setCell = SetCell.newBuilder() .setFamilyName(cell.getFamily().toString()) .setColumnQualifier(toByteString(cell.getQualifier())) .setTimestampMicros(cell.getTimestamp()) .setValue(toByteString(cell.getValue())) .build(); mutations.add(Mutation.newBuilder().setSetCell(setCell).build()); } return KV.of(key, mutations.build()); } }
@ProcessElement public void processElement(ProcessContext c) { int index = c.element().intValue(); Iterable<Mutation> mutations = ImmutableList.of( Mutation.newBuilder() .setSetCell( Mutation.SetCell.newBuilder() .setValue(testData.get(index).getValue()) .setFamilyName(COLUMN_FAMILY_NAME)) .build()); c.output(KV.of(testData.get(index).getKey(), mutations)); } }))
/** Helper function to make a single row mutation to be written. */ private static KV<ByteString, Iterable<Mutation>> makeWrite(String key, String value) { ByteString rowKey = ByteString.copyFromUtf8(key); Iterable<Mutation> mutations = ImmutableList.of( Mutation.newBuilder() .setSetCell(SetCell.newBuilder().setValue(ByteString.copyFromUtf8(value))) .build()); return KV.of(rowKey, mutations); }