Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code snippet to delete rows. #1269

Merged
merged 6 commits into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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