Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: adds samples for commit stats (#899)
* samples: adds commit stats samples Adds samples for commit stats. Shows has to get the mutation count from a write. * test: adds tests for commit stats samples * samples: refactors samples for get commit stats * samples: reformats samples for commit stats * samples: addresses PR comments * test: fixes tests for get commit stats samples * test: addresses pr comments Throws an error if the instance is not set. Fixes the format database id name function.
- Loading branch information
1 parent
1681f9f
commit 533257e
Showing
2 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
samples/snippets/src/main/java/com/example/spanner/GetCommitStatsSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
// [START spanner_get_commit_stats] | ||
|
||
import com.google.cloud.spanner.CommitResponse; | ||
import com.google.cloud.spanner.DatabaseClient; | ||
import com.google.cloud.spanner.DatabaseId; | ||
import com.google.cloud.spanner.Mutation; | ||
import com.google.cloud.spanner.Options; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import java.util.Arrays; | ||
|
||
public class GetCommitStatsSample { | ||
|
||
static void getCommitStats() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
final String projectId = "my-project"; | ||
final String instanceId = "my-instance"; | ||
final String databaseId = "my-database"; | ||
|
||
try (Spanner spanner = | ||
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
final DatabaseClient databaseClient = spanner | ||
.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); | ||
getCommitStats(databaseClient); | ||
} | ||
} | ||
|
||
static void getCommitStats(DatabaseClient databaseClient) { | ||
final CommitResponse commitResponse = databaseClient.writeWithOptions(Arrays.asList( | ||
Mutation.newInsertOrUpdateBuilder("Albums") | ||
.set("SingerId") | ||
.to("1") | ||
.set("AlbumId") | ||
.to("1") | ||
.set("MarketingBudget") | ||
.to("200000") | ||
.build(), | ||
Mutation.newInsertOrUpdateBuilder("Albums") | ||
.set("SingerId") | ||
.to("2") | ||
.set("AlbumId") | ||
.to("2") | ||
.set("MarketingBudget") | ||
.to("400000") | ||
.build() | ||
), Options.commitStats()); | ||
|
||
System.out.println( | ||
"Updated data with " + commitResponse.getCommitStats().getMutationCount() + " mutations."); | ||
} | ||
} | ||
// [END spanner_get_commit_stats] |
146 changes: 146 additions & 0 deletions
146
samples/snippets/src/test/java/com/example/spanner/GetCommitStatsSampleIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.cloud.spanner.DatabaseAdminClient; | ||
import com.google.cloud.spanner.DatabaseClient; | ||
import com.google.cloud.spanner.DatabaseId; | ||
import com.google.cloud.spanner.KeySet; | ||
import com.google.cloud.spanner.Mutation; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableList; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.UUID; | ||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** | ||
* Integration tests for {@link GetCommitStatsSample} | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class GetCommitStatsSampleIT { | ||
|
||
private static String instanceId = System.getProperty("spanner.test.instance"); | ||
private static String databaseId = formatForTest( | ||
System.getProperty("spanner.sample.database", "commitstatssample")); | ||
private static DatabaseId dbId; | ||
private static DatabaseAdminClient dbClient; | ||
private static Spanner spanner; | ||
|
||
@BeforeClass | ||
public static void createTestDatabase() throws Exception { | ||
Preconditions.checkState(instanceId != null, "No instance id set"); | ||
final SpannerOptions options = | ||
SpannerOptions.newBuilder().setAutoThrottleAdministrativeRequests().build(); | ||
spanner = options.getService(); | ||
dbClient = spanner.getDatabaseAdminClient(); | ||
dbId = DatabaseId.of(options.getProjectId(), instanceId, databaseId); | ||
dbClient.dropDatabase(dbId.getInstanceId().getInstance(), dbId.getDatabase()); | ||
dbClient | ||
.createDatabase( | ||
instanceId, | ||
databaseId, | ||
ImmutableList.of( | ||
"CREATE TABLE Singers (" | ||
+ " SingerId INT64 NOT NULL," | ||
+ " FirstName STRING(1024)," | ||
+ " LastName STRING(1024)," | ||
+ " SingerInfo BYTES(MAX)" | ||
+ ") PRIMARY KEY (SingerId)", | ||
"CREATE TABLE Albums (" | ||
+ " SingerId INT64 NOT NULL," | ||
+ " AlbumId INT64 NOT NULL," | ||
+ " AlbumTitle STRING(MAX)," | ||
+ " MarketingBudget INT64" | ||
+ ") PRIMARY KEY (SingerId, AlbumId)," | ||
+ " INTERLEAVE IN PARENT Singers ON DELETE CASCADE")) | ||
.get(); | ||
} | ||
|
||
@AfterClass | ||
public static void dropTestDatabase() { | ||
dbClient.dropDatabase(dbId.getInstanceId().getInstance(), dbId.getDatabase()); | ||
spanner.close(); | ||
} | ||
|
||
@Before | ||
public void insertTestData() { | ||
final DatabaseClient client = spanner.getDatabaseClient(dbId); | ||
client.write(Arrays.asList( | ||
Mutation.newInsertBuilder("Singers") | ||
.set("SingerId") | ||
.to(1L) | ||
.set("FirstName") | ||
.to("first name 1") | ||
.set("LastName") | ||
.to("last name 1") | ||
.build(), | ||
Mutation.newInsertBuilder("Singers") | ||
.set("SingerId") | ||
.to(2L) | ||
.set("FirstName") | ||
.to("first name 2") | ||
.set("LastName") | ||
.to("last name 2") | ||
.build() | ||
)); | ||
} | ||
|
||
@After | ||
public void removeTestData() { | ||
final DatabaseClient client = spanner.getDatabaseClient(dbId); | ||
client.write(Collections.singletonList(Mutation.delete("Singers", KeySet.all()))); | ||
} | ||
|
||
@Test | ||
public void testGetCommitStatsSample() { | ||
final DatabaseClient client = spanner.getDatabaseClient(dbId); | ||
final String out = runExample(client); | ||
|
||
assertThat(out).contains("Updated data with 6 mutations."); | ||
} | ||
|
||
private String runExample(DatabaseClient client) { | ||
PrintStream stdOut = System.out; | ||
ByteArrayOutputStream bout = new ByteArrayOutputStream(); | ||
PrintStream out = new PrintStream(bout); | ||
System.setOut(out); | ||
GetCommitStatsSample.getCommitStats(client); | ||
System.setOut(stdOut); | ||
return bout.toString(); | ||
} | ||
|
||
private static String formatForTest(String name) { | ||
return ( | ||
name | ||
+ "-" | ||
+ UUID.randomUUID().toString().replaceAll("-", "") | ||
).substring(0, 30); | ||
} | ||
} |