Skip to content

Commit

Permalink
Add code snippet to delete rows. (#1269)
Browse files Browse the repository at this point in the history
* Update SpannerSample.java
* Update SpannerSampleIT.java
  • Loading branch information
RobinRH authored and tswast committed Nov 26, 2018
1 parent 1612612 commit dd07e56
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,25 @@ static void writeExampleData(DatabaseClient dbClient) {
}
// [END spanner_insert_data]

// [START spanner_delete_data]
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]

// [START spanner_query_data]
static void query(DatabaseClient dbClient) {
// singleUse() can be used to execute a single read or query against Cloud Spanner.
Expand Down Expand Up @@ -1077,6 +1096,9 @@ static void run(
case "write":
writeExampleData(dbClient);
break;
case "delete":
deleteExampleData(dbClient);
break;
case "query":
query(dbClient);
break;
Expand Down Expand Up @@ -1194,6 +1216,7 @@ static void printUsageAndExit() {
System.err.println("Examples:");
System.err.println(" SpannerExample createdatabase my-instance example-db");
System.err.println(" SpannerExample write my-instance example-db");
System.err.println(" SpannerExample delete my-instance example-db");
System.err.println(" SpannerExample query my-instance example-db");
System.err.println(" SpannerExample read my-instance example-db");
System.err.println(" SpannerExample addmarketingbudget my-instance example-db");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public void testSample() throws Exception {

runSample("write");

out = runSample("delete");
assertThat(out).contains("Records deleted.");

runSample("write");

out = runSample("read");
assertThat(out).contains("1 1 Total Junk");

Expand Down

0 comments on commit dd07e56

Please sign in to comment.