Search in sources :

Example 71 with Mutation

use of com.google.spanner.v1.Mutation in project beam by apache.

the class BigtableServiceImplTest method testWrite.

/**
 * 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 {
    when(mockBigtableSource.getTableId()).thenReturn(StaticValueProvider.of(TABLE_ID));
    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();
}
Also used : MutateRowResponse(com.google.bigtable.v2.MutateRowResponse) Entry(com.google.bigtable.v2.MutateRowsRequest.Entry) ByteString(com.google.protobuf.ByteString) Mutation(com.google.bigtable.v2.Mutation) BulkMutation(com.google.cloud.bigtable.grpc.async.BulkMutation) Test(org.junit.Test)

Example 72 with Mutation

use of com.google.spanner.v1.Mutation in project DataflowTemplates by GoogleCloudPlatform.

the class BeamRowToBigtableFn method processElement.

@ProcessElement
@SuppressWarnings("unused")
public void processElement(@Element Row row, OutputReceiver<KV<ByteString, Iterable<Mutation>>> out) {
    // Generate the Bigtable Rowkey. This key will be used for all Cells for this row.
    ByteString rowkey = generateRowKey(row);
    // Number of mutations accumulated to be outputted.
    int cellsProcessed = 0;
    // DoFn return value.
    ImmutableList.Builder<Mutation> mutations = ImmutableList.builder();
    // Retrieve all fields that are not part of the rowkey. All fields that are part of the
    // rowkey must have a metadata key set to key_order. All other fields should become their own
    // cells in Bigtable.
    List<Field> nonKeyColumns = row.getSchema().getFields().stream().filter(f -> f.getType().getMetadataString(CassandraRowMapperFn.KEY_ORDER_METADATA_KEY).isEmpty()).collect(Collectors.toList());
    // Iterate over all the fields with three cases. Primitives, collections and maps.
    for (Field field : nonKeyColumns) {
        List<Mutation> mutationsToAdd;
        TypeName type = field.getType().getTypeName();
        if (type.isPrimitiveType()) {
            ByteString value = primitiveFieldToBytes(type, row.getValue(field.getName()));
            SetCell cell = createCell(defaultColumnFamily.get(), field.getName(), value);
            mutationsToAdd = Arrays.asList(Mutation.newBuilder().setSetCell(cell).build());
        } else if (type.isCollectionType()) {
            mutationsToAdd = createCollectionMutations(row, field);
        } else if (type.isMapType()) {
            mutationsToAdd = createMapMutations(row, field);
        } else {
            throw new UnsupportedOperationException("Mapper does not support type:" + field.getType().getTypeName().toString());
        }
        for (Mutation mutationToAdd : mutationsToAdd) {
            mutations.add(mutationToAdd);
            cellsProcessed++;
            if (this.splitLargeRows && cellsProcessed % maxMutationsPerRequest == 0) {
                // Send a MutateRow request when we have accumulated max mutations per request.
                out.output(KV.of(rowkey, mutations.build()));
                mutations = ImmutableList.builder();
            }
        }
    }
    // Flush any remaining mutations.
    ImmutableList remainingMutations = mutations.build();
    if (!remainingMutations.isEmpty()) {
        out.output(KV.of(rowkey, remainingMutations));
    }
}
Also used : Mutation(com.google.bigtable.v2.Mutation) Arrays(java.util.Arrays) KV(org.apache.beam.sdk.values.KV) MoreObjects(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.MoreObjects) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Charset(java.nio.charset.Charset) Map(java.util.Map) SetCell(com.google.bigtable.v2.Mutation.SetCell) Row(org.apache.beam.sdk.values.Row) ValueProvider(org.apache.beam.sdk.options.ValueProvider) Bytes(org.apache.hadoop.hbase.util.Bytes) Field(org.apache.beam.sdk.schemas.Schema.Field) DoFn(org.apache.beam.sdk.transforms.DoFn) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Set(java.util.Set) ReadableInstant(org.joda.time.ReadableInstant) Collectors(java.util.stream.Collectors) TypeName(org.apache.beam.sdk.schemas.Schema.TypeName) ByteString(com.google.protobuf.ByteString) List(java.util.List) TreeMap(java.util.TreeMap) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) StaticValueProvider(org.apache.beam.sdk.options.ValueProvider.StaticValueProvider) BaseEncoding(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.io.BaseEncoding) Field(org.apache.beam.sdk.schemas.Schema.Field) TypeName(org.apache.beam.sdk.schemas.Schema.TypeName) SetCell(com.google.bigtable.v2.Mutation.SetCell) ByteString(com.google.protobuf.ByteString) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Mutation(com.google.bigtable.v2.Mutation)

Example 73 with Mutation

use of com.google.spanner.v1.Mutation in project DataflowTemplates by GoogleCloudPlatform.

the class ParquetToBigtableTest method applyParquettoBigtableFnWithSplitLargeRows.

/**
 * Test whether {@link ParquetToBigtable} correctly maps a GenericRecord to a KV with
 * SplitLargeRows enabled..
 */
@Test
public void applyParquettoBigtableFnWithSplitLargeRows() throws Exception {
    byte[] rowKey1 = "row1".getBytes();
    ByteBuffer key1 = ByteBuffer.wrap(rowKey1);
    List<BigtableCell> cells1 = new ArrayList<>();
    addParquetCell(cells1, "family1", "column1", 1, "10");
    addParquetCell(cells1, "family1", "column1", 2, "20");
    addParquetCell(cells1, "family1", "column2", 1, "30");
    addParquetCell(cells1, "family2", "column1", 1, "40");
    GenericRecord parquetRow1 = new GenericRecordBuilder(BigtableRow.getClassSchema()).set("key", key1).set("cells", cells1).build();
    byte[] rowKey2 = "row2".getBytes();
    ByteBuffer key2 = ByteBuffer.wrap(rowKey2);
    List<BigtableCell> cells2 = new ArrayList<>();
    addParquetCell(cells2, "family2", "column2", 2, "40");
    GenericRecord parquetRow2 = new GenericRecordBuilder(BigtableRow.getClassSchema()).set("key", key2).set("cells", cells2).build();
    final List<GenericRecord> parquetRows = ImmutableList.of(parquetRow1, parquetRow2);
    KV<ByteString, Iterable<Mutation>> rowMutations1 = createBigtableRowMutations("row1");
    addBigtableMutation(rowMutations1, "family1", "column1", 1, "10");
    addBigtableMutation(rowMutations1, "family1", "column1", 2, "20");
    KV<ByteString, Iterable<Mutation>> rowMutations2 = createBigtableRowMutations("row1");
    addBigtableMutation(rowMutations2, "family1", "column2", 1, "30");
    addBigtableMutation(rowMutations2, "family2", "column1", 1, "40");
    KV<ByteString, Iterable<Mutation>> rowMutations3 = createBigtableRowMutations("row2");
    addBigtableMutation(rowMutations3, "family2", "column2", 2, "40");
    final List<KV<ByteString, Iterable<Mutation>>> expectedBigtableRows = ImmutableList.of(rowMutations1, rowMutations2, rowMutations3);
    PCollection<KV<ByteString, Iterable<Mutation>>> bigtableRows = pipeline.apply("Create", Create.of(parquetRows).withCoder(AvroCoder.of(GenericRecord.class, BigtableRow.getClassSchema()))).apply("TransformToBigtable", ParDo.of(ParquetToBigtableFn.createWithSplitLargeRows(StaticValueProvider.of(true), 2)));
    PAssert.that(bigtableRows).containsInAnyOrder(expectedBigtableRows);
    pipeline.run().waitUntilFinish();
}
Also used : ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) KV(org.apache.beam.sdk.values.KV) ByteBuffer(java.nio.ByteBuffer) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) Mutation(com.google.bigtable.v2.Mutation) TestUtils.addBigtableMutation(com.google.cloud.teleport.bigtable.TestUtils.addBigtableMutation) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.junit.Test)

Example 74 with Mutation

use of com.google.spanner.v1.Mutation in project DataflowTemplates by GoogleCloudPlatform.

the class TestUtils method addBigtableMutation.

static void addBigtableMutation(KV<ByteString, Iterable<Mutation>> rowMutations, String family, String qualifier, long timestamp, String value) {
    SetCell setCell = SetCell.newBuilder().setFamilyName(family).setColumnQualifier(toByteString(qualifier)).setTimestampMicros(timestamp).setValue(toByteString(value)).build();
    Mutation mutation = Mutation.newBuilder().setSetCell(setCell).build();
    ((List) rowMutations.getValue()).add(mutation);
}
Also used : SetCell(com.google.bigtable.v2.Mutation.SetCell) List(java.util.List) ArrayList(java.util.ArrayList) Mutation(com.google.bigtable.v2.Mutation)

Example 75 with Mutation

use of com.google.spanner.v1.Mutation in project DataflowTemplates by GoogleCloudPlatform.

the class BeamRowToBigtableFnTest method processElementWithMapColumn.

@Test
public void processElementWithMapColumn() {
    String columnFamily = "default";
    String rowKeyValue = "rowkeyvalue";
    String rowKeyColumnName = "rowkey";
    String listColumnName = "mapColumnName";
    Map<Integer, String> mapValue = new HashMap<>();
    mapValue.put(0, "first");
    mapValue.put(1, "second");
    mapValue.put(2, "third");
    Schema schema = Schema.builder().addField(Schema.Field.of(rowKeyColumnName, FieldType.STRING.withMetadata(CassandraRowMapperFn.KEY_ORDER_METADATA_KEY, "0"))).addField(Schema.Field.of(listColumnName, FieldType.map(FieldType.INT32, FieldType.STRING))).build();
    Row input = Row.withSchema(schema).addValue(rowKeyValue).addValue(mapValue).build();
    final List<Row> rows = Collections.singletonList(input);
    List<Mutation> mutations = new ArrayList<>();
    mutations.add(createMutation(columnFamily, "mapColumnName[0].key", ByteString.copyFrom(Bytes.toBytes(0))));
    mutations.add(createMutation(columnFamily, "mapColumnName[0].value", ByteString.copyFrom(Bytes.toBytes(mapValue.get(0)))));
    mutations.add(createMutation(columnFamily, "mapColumnName[1].key", ByteString.copyFrom(Bytes.toBytes(1))));
    mutations.add(createMutation(columnFamily, "mapColumnName[1].value", ByteString.copyFrom(Bytes.toBytes(mapValue.get(1)))));
    mutations.add(createMutation(columnFamily, "mapColumnName[2].key", ByteString.copyFrom(Bytes.toBytes(2))));
    mutations.add(createMutation(columnFamily, "mapColumnName[2].value", ByteString.copyFrom(Bytes.toBytes(mapValue.get(2)))));
    final List<KV<ByteString, Iterable<Mutation>>> expectedBigtableRows = ImmutableList.of(KV.of(ByteString.copyFrom(Bytes.toBytes("rowkeyvalue")), mutations));
    PCollection<KV<ByteString, Iterable<Mutation>>> bigtableRows = pipeline.apply("Create", Create.of(rows)).apply("Transform to Bigtable", ParDo.of(BeamRowToBigtableFn.create(ValueProvider.StaticValueProvider.of("#"), ValueProvider.StaticValueProvider.of("default"))));
    PAssert.that(bigtableRows).containsInAnyOrder(expectedBigtableRows);
    pipeline.run();
}
Also used : HashMap(java.util.HashMap) Schema(org.apache.beam.sdk.schemas.Schema) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) KV(org.apache.beam.sdk.values.KV) Row(org.apache.beam.sdk.values.Row) Mutation(com.google.bigtable.v2.Mutation) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)70 Mutation (com.google.bigtable.v2.Mutation)62 ArrayList (java.util.ArrayList)59 ByteString (com.google.protobuf.ByteString)56 CommitRequest (com.google.spanner.v1.CommitRequest)15 KV (org.apache.beam.sdk.values.KV)15 CheckAndMutateRowResponse (com.google.bigtable.v2.CheckAndMutateRowResponse)14 AbstractMessage (com.google.protobuf.AbstractMessage)13 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)12 RowFilter (com.google.bigtable.v2.RowFilter)12 TableName (com.google.bigtable.v2.TableName)12 StatusRuntimeException (io.grpc.StatusRuntimeException)12 CheckAndMutateRowRequest (com.google.bigtable.v2.CheckAndMutateRowRequest)11 MutateRowRequest (com.google.bigtable.v2.MutateRowRequest)11 Mutation (com.google.spanner.v1.Mutation)11 BaseBigtableDataClient (com.google.cloud.bigtable.data.v2.BaseBigtableDataClient)10 MutateRowResponse (com.google.bigtable.v2.MutateRowResponse)9 SetCell (com.google.bigtable.v2.Mutation.SetCell)9 Mutation (com.google.datastore.v1.Mutation)9 CommitResponse (com.google.spanner.v1.CommitResponse)9