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