private Timestamp write(Mutation m) { return client.write(Arrays.asList(m)); }
@Ignore // TODO(user): Fix this - backend currently accepts empty mutation. @Test public void emptyWrite() { expectedException.expect(isSpannerException(ErrorCode.INVALID_ARGUMENT)); client.write(Arrays.<Mutation>asList()); }
/** Example of blind write. */ // [TARGET write(Iterable)] // [VARIABLE my_singer_id] public void write(long singerId) { // [START write] Mutation mutation = Mutation.newInsertBuilder("Singer") .set("SingerId") .to(singerId) .set("FirstName") .to("Billy") .set("LastName") .to("Joel") .build(); dbClient.write(Collections.singletonList(mutation)); // [END write] }
client.write( Arrays.asList(Mutation.newInsertBuilder("T").set("K").to(key).set("V").to(0).build()));
@BeforeClass public static void setUpDatabase() { db = env.getTestHelper() .createTestDatabase( "CREATE TABLE TestTable (" + " Key STRING(MAX) NOT NULL," + " StringValue STRING(MAX)," + ") PRIMARY KEY (Key)", "CREATE INDEX TestTableByValue ON TestTable(StringValue)"); // Includes k0..k14. Note that strings k{10,14} sort between k1 and k2. List<Mutation> mutations = new ArrayList<>(); for (int i = 0; i < 15; ++i) { mutations.add( Mutation.newInsertOrUpdateBuilder(TABLE_NAME) .set("Key") .to("k" + i) .set("StringValue") .to("v" + i) .build()); } env.getTestHelper().getDatabaseClient(db).write(mutations); }
@BeforeClass public static void setUpDatabase() { db = env.getTestHelper() .createTestDatabase( "CREATE TABLE TestTable (" + " Key STRING(MAX) NOT NULL," + " StringValue STRING(MAX)," + ") PRIMARY KEY (Key)", "CREATE INDEX TestTableByValue ON TestTable(StringValue)", "CREATE INDEX TestTableByValueDesc ON TestTable(StringValue DESC)"); client = env.getTestHelper().getDatabaseClient(db); // Includes k0..k14. Note that strings k{10,14} sort between k1 and k2. List<Mutation> mutations = new ArrayList<>(); for (int i = 0; i < 15; ++i) { mutations.add( Mutation.newInsertOrUpdateBuilder(TABLE_NAME) .set("Key") .to("k" + i) .set("StringValue") .to("v" + i) .build()); } client.write(mutations); }
final String key2 = uniqueKey(); client.write( Arrays.asList( Mutation.newInsertBuilder("T").set("K").to(key1).set("V").to(0).build(),
@Test public void abortAndRetry() { client.write( Arrays.asList( Mutation.newInsertBuilder("T").set("K").to("Key3").set("BoolValue").to(true).build()));
static void deleteExampleData(DatabaseClient dbClient) { List<Mutation> mutations = new ArrayList<>(); // KeySet.all() can be used to delete all the rows in a table. mutations.add(Mutation.delete("Albums", KeySet.all())); // KeySet.singleKey() can be used to delete one row at a time. for (Singer singer : SINGERS) { mutations.add( Mutation.delete("Singers", KeySet.singleKey(Key.newBuilder().append(singer.singerId).build()))); } dbClient.write(mutations); System.out.printf("Records deleted.\n"); } // [END spanner_delete_data]
static void writeStructExampleData(DatabaseClient dbClient) { final List<Singer> singers = Arrays.asList( new Singer(6, "Elena", "Campbell"), new Singer(7, "Gabriel", "Wright"), new Singer(8, "Benjamin", "Martinez"), new Singer(9, "Hannah", "Harris")); List<Mutation> mutations = new ArrayList<>(); for (Singer singer : singers) { mutations.add( Mutation.newInsertBuilder("Singers") .set("SingerId") .to(singer.singerId) .set("FirstName") .to(singer.firstName) .set("LastName") .to(singer.lastName) .build()); } dbClient.write(mutations); System.out.println("Inserted example data for struct parameter queries."); } // [END spanner_write_data_for_struct_queries]
static void writeExampleDataWithTimestamp(DatabaseClient dbClient) { List<Mutation> mutations = new ArrayList<>(); for (Performance performance : PERFORMANCES) { mutations.add( Mutation.newInsertBuilder("Performances") .set("SingerId") .to(performance.singerId) .set("VenueId") .to(performance.venueId) .set("EventDate") .to(performance.eventDate) .set("Revenue") .to(performance.revenue) .set("LastUpdateTime") .to(Value.COMMIT_TIMESTAMP) .build()); } dbClient.write(mutations); } // [END spanner_insert_data_with_timestamp_column]
static void writeExampleData(DatabaseClient dbClient) { List<Mutation> mutations = new ArrayList<>(); for (Singer singer : SINGERS) { mutations.add( Mutation.newInsertBuilder("Singers") .set("SingerId") .to(singer.singerId) .set("FirstName") .to(singer.firstName) .set("LastName") .to(singer.lastName) .build()); } for (Album album : ALBUMS) { mutations.add( Mutation.newInsertBuilder("Albums") .set("SingerId") .to(album.singerId) .set("AlbumId") .to(album.albumId) .set("AlbumTitle") .to(album.albumTitle) .build()); } dbClient.write(mutations); } // [END spanner_insert_data]
static void update(DatabaseClient dbClient) { // Mutation can be used to update/insert/delete a single row in a table. Here we use // newUpdateBuilder to create update mutations. List<Mutation> mutations = Arrays.asList( Mutation.newUpdateBuilder("Albums") .set("SingerId") .to(1) .set("AlbumId") .to(1) .set("MarketingBudget") .to(100000) .build(), Mutation.newUpdateBuilder("Albums") .set("SingerId") .to(2) .set("AlbumId") .to(2) .set("MarketingBudget") .to(500000) .build()); // This writes all the mutations to Cloud Spanner atomically. dbClient.write(mutations); } // [END spanner_update_data]
static void updateWithTimestamp(DatabaseClient dbClient) { // Mutation can be used to update/insert/delete a single row in a table. Here we use // newUpdateBuilder to create update mutations. List<Mutation> mutations = Arrays.asList( Mutation.newUpdateBuilder("Albums") .set("SingerId") .to(1) .set("AlbumId") .to(1) .set("MarketingBudget") .to(1000000) .set("LastUpdateTime") .to(Value.COMMIT_TIMESTAMP) .build(), Mutation.newUpdateBuilder("Albums") .set("SingerId") .to(2) .set("AlbumId") .to(2) .set("MarketingBudget") .to(750000) .set("LastUpdateTime") .to(Value.COMMIT_TIMESTAMP) .build()); // This writes all the mutations to Cloud Spanner atomically. dbClient.write(mutations); } // [END spanner_update_data_with_timestamp_column]
protected void applyMutations(Collection<Mutation> mutations) { LOGGER.debug("Applying Mutation: " + mutations); doWithOrWithoutTransactionContext((x) -> { x.buffer(mutations); return null; }, () -> { this.databaseClient.write(mutations); return null; }); }
protected void applyMutations(Collection<Mutation> mutations) { LOGGER.debug("Applying Mutation: " + mutations); doWithOrWithoutTransactionContext((x) -> { x.buffer(mutations); return null; }, () -> { this.databaseClient.write(mutations); return null; }); }