diff --git a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java index c686118422a..0c2a62f68c2 100644 --- a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java +++ b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java @@ -251,6 +251,25 @@ static void writeExampleData(DatabaseClient dbClient) { } // [END spanner_insert_data] + // [START spanner_delete_data] + static void deleteExampleData(DatabaseClient dbClient) { + List 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. @@ -1077,6 +1096,9 @@ static void run( case "write": writeExampleData(dbClient); break; + case "delete": + deleteExampleData(dbClient); + break; case "query": query(dbClient); break; @@ -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"); diff --git a/spanner/cloud-client/src/test/java/com/example/spanner/SpannerSampleIT.java b/spanner/cloud-client/src/test/java/com/example/spanner/SpannerSampleIT.java index 2dee126ad6d..54c9407f772 100644 --- a/spanner/cloud-client/src/test/java/com/example/spanner/SpannerSampleIT.java +++ b/spanner/cloud-client/src/test/java/com/example/spanner/SpannerSampleIT.java @@ -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");