Search in sources :

Example 11 with Mutation

use of com.google.spanner.v1.Mutation in project simple-bigtable by spotify.

the class BigtableMutationImplTest method testDeleteRow.

@Test
public void testDeleteRow() throws Exception {
    final BigtableMutationImpl bigtableMutationImpl = (BigtableMutationImpl) this.bigtableMutation.deleteRow();
    verifyGetMutateRowRequest();
    bigtableMutationImpl.execute();
    bigtableMutationImpl.executeAsync();
    assertEquals(1, bigtableMutationImpl.getMutateRowRequest().getMutationsCount());
    final Mutation mutation = bigtableMutationImpl.getMutateRowRequest().getMutations(0);
    assertEquals(Mutation.MutationCase.DELETE_FROM_ROW, mutation.getMutationCase());
    assertEquals(Mutation.DeleteFromRow.getDefaultInstance(), mutation.getDeleteFromRow());
    assertEquals(Mutation.DeleteFromFamily.getDefaultInstance(), mutation.getDeleteFromFamily());
    assertEquals(Mutation.DeleteFromColumn.getDefaultInstance(), mutation.getDeleteFromColumn());
    assertEquals(Mutation.SetCell.getDefaultInstance(), mutation.getSetCell());
    verify(bigtableMock.getMockedDataClient()).mutateRow(bigtableMutation.getMutateRowRequest().build());
    verify(bigtableMock.getMockedDataClient()).mutateRowAsync(bigtableMutation.getMutateRowRequest().build());
    verifyNoMoreInteractions(bigtableMock.getMockedDataClient());
}
Also used : Mutation(com.google.bigtable.v2.Mutation) Test(org.junit.Test)

Example 12 with Mutation

use of com.google.spanner.v1.Mutation in project simple-bigtable by spotify.

the class BigtableMutationImplTest method testWriteColumnValue.

@Test
public void testWriteColumnValue() throws Exception {
    final BigtableMutationImpl bigtableMutationImpl = (BigtableMutationImpl) this.bigtableMutation.write("family:qualifier", ByteString.copyFromUtf8("value")).write("family", "qualifier", ByteString.copyFromUtf8("value"));
    verifyGetMutateRowRequest();
    bigtableMutationImpl.execute();
    bigtableMutationImpl.executeAsync();
    assertEquals(2, bigtableMutationImpl.getMutateRowRequest().getMutationsCount());
    for (Mutation mutation : bigtableMutationImpl.getMutateRowRequest().getMutationsList()) {
        assertEquals(Mutation.MutationCase.SET_CELL, mutation.getMutationCase());
        assertEquals("family", mutation.getSetCell().getFamilyName());
        assertEquals("qualifier", mutation.getSetCell().getColumnQualifier().toStringUtf8());
        assertEquals("value", mutation.getSetCell().getValue().toStringUtf8());
        assertEquals(Mutation.DeleteFromRow.getDefaultInstance(), mutation.getDeleteFromRow());
        assertEquals(Mutation.DeleteFromFamily.getDefaultInstance(), mutation.getDeleteFromFamily());
        assertEquals(Mutation.DeleteFromColumn.getDefaultInstance(), mutation.getDeleteFromColumn());
        // Check timestamp in last 1 second
        final long tsMillis = TimeUnit.MICROSECONDS.toMillis(mutation.getSetCell().getTimestampMicros());
        final long nowMillis = System.currentTimeMillis();
        assertTrue(tsMillis <= nowMillis && tsMillis > nowMillis - TimeUnit.SECONDS.toMillis(1));
    }
    verify(bigtableMock.getMockedDataClient()).mutateRow(bigtableMutation.getMutateRowRequest().build());
    verify(bigtableMock.getMockedDataClient()).mutateRowAsync(bigtableMutation.getMutateRowRequest().build());
    verifyNoMoreInteractions(bigtableMock.getMockedDataClient());
}
Also used : Mutation(com.google.bigtable.v2.Mutation) Test(org.junit.Test)

Example 13 with Mutation

use of com.google.spanner.v1.Mutation in project simple-bigtable by spotify.

the class BigtableMutationImplTest method testDeleteColumn.

@Test
public void testDeleteColumn() throws Exception {
    final BigtableMutationImpl bigtableMutationImpl = (BigtableMutationImpl) this.bigtableMutation.deleteColumn("family:qualifier");
    verifyGetMutateRowRequest();
    bigtableMutationImpl.execute();
    bigtableMutationImpl.executeAsync();
    assertEquals(1, bigtableMutationImpl.getMutateRowRequest().getMutationsCount());
    final Mutation mutation = bigtableMutationImpl.getMutateRowRequest().getMutations(0);
    assertEquals(Mutation.MutationCase.DELETE_FROM_COLUMN, mutation.getMutationCase());
    assertEquals("family", mutation.getDeleteFromColumn().getFamilyName());
    assertEquals("qualifier", mutation.getDeleteFromColumn().getColumnQualifier().toStringUtf8());
    assertEquals(Mutation.DeleteFromRow.getDefaultInstance(), mutation.getDeleteFromRow());
    assertEquals(Mutation.DeleteFromFamily.getDefaultInstance(), mutation.getDeleteFromFamily());
    assertEquals(Mutation.SetCell.getDefaultInstance(), mutation.getSetCell());
    verify(bigtableMock.getMockedDataClient()).mutateRow(bigtableMutation.getMutateRowRequest().build());
    verify(bigtableMock.getMockedDataClient()).mutateRowAsync(bigtableMutation.getMutateRowRequest().build());
    verifyNoMoreInteractions(bigtableMock.getMockedDataClient());
}
Also used : Mutation(com.google.bigtable.v2.Mutation) Test(org.junit.Test)

Example 14 with Mutation

use of com.google.spanner.v1.Mutation in project java-docs-samples by GoogleCloudPlatform.

the class LoadCsvExample method writeToSpanner.

/**
 * Write CSV file data to Spanner using JDBC Mutation API *
 */
static void writeToSpanner(Iterable<CSVRecord> records, String tableName) throws SQLException {
    System.out.println("Writing data into table...");
    List<Mutation> mutations = new ArrayList<>();
    for (CSVRecord record : records) {
        int index = 0;
        WriteBuilder builder = Mutation.newInsertOrUpdateBuilder(tableName);
        for (String columnName : tableColumns.keySet()) {
            // Iterates through columns in order. Assumes in order columns when no headers provided.
            TypeCode columnType = tableColumns.get(columnName);
            String recordValue = null;
            if (validHeaderField(record, columnName)) {
                recordValue = record.get(columnName).trim();
            } else if (validNonHeaderField(record, index)) {
                recordValue = record.get(index).trim();
                index++;
            }
            if (recordValue != null) {
                switch(columnType) {
                    case STRING:
                        builder.set(columnName).to(recordValue);
                        break;
                    case BYTES:
                        builder.set(columnName).to(Byte.parseByte(recordValue));
                        break;
                    case INT64:
                        builder.set(columnName).to(Integer.parseInt(recordValue));
                        break;
                    case FLOAT64:
                        builder.set(columnName).to(Float.parseFloat(recordValue));
                        break;
                    case BOOL:
                        builder.set(columnName).to(Boolean.parseBoolean(recordValue));
                        break;
                    case NUMERIC:
                        builder.set(columnName).to(Value.numeric(BigDecimal.valueOf(Double.parseDouble(recordValue))));
                        break;
                    case DATE:
                        builder.set(columnName).to(com.google.cloud.Date.parseDate(recordValue));
                        break;
                    case TIMESTAMP:
                        builder.set(columnName).to(com.google.cloud.Timestamp.parseTimestamp(recordValue));
                        break;
                    default:
                        System.out.print("Invalid Type. This type is not supported.");
                }
            }
        }
        mutations.add(builder.build());
    }
    CloudSpannerJdbcConnection spannerConnection = connection.unwrap(CloudSpannerJdbcConnection.class);
    spannerConnection.write(mutations);
    spannerConnection.close();
    System.out.println("Data successfully written into table.");
}
Also used : TypeCode(com.google.spanner.v1.TypeCode) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) ArrayList(java.util.ArrayList) CloudSpannerJdbcConnection(com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection) CSVRecord(org.apache.commons.csv.CSVRecord) Mutation(com.google.cloud.spanner.Mutation)

Example 15 with Mutation

use of com.google.spanner.v1.Mutation in project dataflow-pipelines by baeminbo.

the class BigtableWritePipeline method main.

public static void main(String[] args) {
    BigtableWritePipelineOptions options = PipelineOptionsFactory.fromArgs(args).as(BigtableWritePipelineOptions.class);
    // Call getXXX for options used in DoFn only to avoid https://issues.apache.org/jira/browse/BEAM-7983
    options.getBigtableAppProfileId();
    options.getBigtableColumnFamily();
    Pipeline pipeline = Pipeline.create(options);
    pipeline.apply(Create.of(KV.of("first", "hello"), KV.of("second", "world"))).apply(ParDo.of(new DoFn<KV<String, String>, KV<ByteString, Iterable<Mutation>>>() {

        @ProcessElement
        public void processElement(ProcessContext context) {
            // Put a cell to row key: <key of input>, column family: <value of options>, column:
            // "vale", value: <value of input>
            String key = context.element().getKey();
            String value = context.element().getValue();
            String columnFamily = context.getPipelineOptions().as(BigtableWritePipelineOptions.class).getBigtableColumnFamily().get();
            ByteString row = ByteString.copyFromUtf8(key);
            Mutation mutation = Mutation.newBuilder().setSetCell(Mutation.SetCell.newBuilder().setFamilyName(columnFamily).setColumnQualifier(ByteString.copyFromUtf8("value")).setValue(ByteString.copyFromUtf8(value)).build()).build();
            context.output(KV.of(row, Collections.singletonList(mutation)));
        }
    })).apply(BigtableIO.write().withProjectId(options.getBigtableProject()).withInstanceId(options.getBigtableInstance()).withTableId(options.getBigtableTable()).withBigtableOptionsConfigurator(builder -> {
        if (runtimePipelineOptions != null) {
            @Nullable String appProfileId = runtimePipelineOptions.as(BigtableWritePipelineOptions.class).getBigtableAppProfileId().get();
            if (appProfileId != null && !appProfileId.isEmpty()) {
                LOG.info("Override AppProfile ID with '{}'", appProfileId);
                builder.setAppProfileId(appProfileId);
            } else {
                LOG.info("Use default AppProfile ID. No AppProfile ID was set in options");
            }
        } else {
            LOG.warn("Use default AppProfile ID because runtime option is not set properly");
        }
        return builder;
    }));
    pipeline.run();
}
Also used : DoFn(org.apache.beam.sdk.transforms.DoFn) JvmInitializer(org.apache.beam.sdk.harness.JvmInitializer) Mutation(com.google.bigtable.v2.Mutation) KV(org.apache.beam.sdk.values.KV) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) PipelineOptionsFactory(org.apache.beam.sdk.options.PipelineOptionsFactory) Description(org.apache.beam.sdk.options.Description) ByteString(com.google.protobuf.ByteString) ParDo(org.apache.beam.sdk.transforms.ParDo) Create(org.apache.beam.sdk.transforms.Create) BigtableIO(org.apache.beam.sdk.io.gcp.bigtable.BigtableIO) AutoService(com.google.auto.service.AutoService) Pipeline(org.apache.beam.sdk.Pipeline) Collections(java.util.Collections) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) Nullable(javax.annotation.Nullable) ValueProvider(org.apache.beam.sdk.options.ValueProvider) ByteString(com.google.protobuf.ByteString) KV(org.apache.beam.sdk.values.KV) Mutation(com.google.bigtable.v2.Mutation) ByteString(com.google.protobuf.ByteString) Pipeline(org.apache.beam.sdk.Pipeline)

Aggregations

Test (org.junit.Test)67 Mutation (com.google.bigtable.v2.Mutation)62 ArrayList (java.util.ArrayList)59 ByteString (com.google.protobuf.ByteString)56 KV (org.apache.beam.sdk.values.KV)15 CheckAndMutateRowResponse (com.google.bigtable.v2.CheckAndMutateRowResponse)14 AbstractMessage (com.google.protobuf.AbstractMessage)13 CommitRequest (com.google.spanner.v1.CommitRequest)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 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 Mutation (com.google.spanner.v1.Mutation)9