GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add Cloud Spanner Batch DML sample (#1361) · 11101171/java-docs-samples@ac1faf4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • spanner/cloud-client
    • pom.xml
    • src
      • main/java/com/example/spanner
        • SpannerSample.java
      • test/java/com/example/spanner
        • SpannerSampleIT.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎spanner/cloud-client/pom.xml‎

Copy file name to clipboardExpand all lines: spanner/cloud-client/pom.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ limitations under the License.
4242
<dependency>
4343
<groupId>com.google.cloud</groupId>
4444
<artifactId>google-cloud-bom</artifactId>
45-
<version>0.73.0-alpha</version>
45+
<version>0.84.0-alpha</version>
4646
<type>pom</type>
4747
<scope>import</scope>
4848
</dependency>
Collapse file

‎spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java‎

Copy file name to clipboardExpand all lines: spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java
+39-1Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.cloud.spanner.ReadOnlyTransaction;
3131
import com.google.cloud.spanner.ResultSet;
3232
import com.google.cloud.spanner.Spanner;
33+
import com.google.cloud.spanner.SpannerBatchUpdateException;
3334
import com.google.cloud.spanner.SpannerException;
3435
import com.google.cloud.spanner.SpannerExceptionFactory;
3536
import com.google.cloud.spanner.SpannerOptions;
@@ -1084,6 +1085,39 @@ static void deleteUsingPartitionedDml(DatabaseClient dbClient) {
10841085
}
10851086
// [END spanner_dml_partitioned_delete]
10861087

1088+
// [START spanner_dml_batch_update]
1089+
static void updateUsingBatchDml(DatabaseClient dbClient) {
1090+
dbClient
1091+
.readWriteTransaction()
1092+
.run(
1093+
new TransactionCallable<Void>() {
1094+
@Override
1095+
public Void run(TransactionContext transaction) throws Exception {
1096+
List<Statement> stmts = new ArrayList<Statement>();
1097+
String sql = "INSERT INTO Albums "
1098+
+ "(SingerId, AlbumId, AlbumTitle, MarketingBudget) "
1099+
+ "VALUES (1, 3, 'Test Album Title', 10000) ";
1100+
stmts.add(Statement.of(sql));
1101+
sql = "UPDATE Albums "
1102+
+ "SET MarketingBudget = MarketingBudget * 2 "
1103+
+ "WHERE SingerId = 1 and AlbumId = 3";
1104+
stmts.add(Statement.of(sql));
1105+
long [] rowCounts;
1106+
try {
1107+
rowCounts = transaction.batchUpdate(stmts);
1108+
} catch (SpannerBatchUpdateException e) {
1109+
rowCounts = e.getUpdateCounts();
1110+
}
1111+
for (int i = 0; i < rowCounts.length; i++) {
1112+
System.out.printf(
1113+
"%d record updated by stmt %d.\n", rowCounts[i], i);
1114+
}
1115+
return null;
1116+
}
1117+
});
1118+
}
1119+
// [END spanner_dml_batch_update]
1120+
10871121
static void run(
10881122
DatabaseClient dbClient,
10891123
DatabaseAdminClient dbAdminClient,
@@ -1203,7 +1237,10 @@ static void run(
12031237
break;
12041238
case "deleteusingpartitioneddml":
12051239
deleteUsingPartitionedDml(dbClient);
1206-
break;
1240+
break;
1241+
case "updateusingbatchdml":
1242+
updateUsingBatchDml(dbClient);
1243+
break;
12071244
default:
12081245
printUsageAndExit();
12091246
}
@@ -1252,6 +1289,7 @@ static void printUsageAndExit() {
12521289
System.err.println(" SpannerExample writewithtransactionusingdml my-instance example-db");
12531290
System.err.println(" SpannerExample updateusingpartitioneddml my-instance example-db");
12541291
System.err.println(" SpannerExample deleteusingpartitioneddml my-instance example-db");
1292+
System.err.println(" SpannerExample updateusingbatchdml my-instance example-db");
12551293
System.exit(1);
12561294
}
12571295

Collapse file

‎spanner/cloud-client/src/test/java/com/example/spanner/SpannerSampleIT.java‎

Copy file name to clipboardExpand all lines: spanner/cloud-client/src/test/java/com/example/spanner/SpannerSampleIT.java
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ public void testSample() throws Exception {
199199
assertThat(out).doesNotContain("Russell Morales");
200200
assertThat(out).doesNotContain("Jacqueline Long");
201201
assertThat(out).doesNotContain("Dylan Shaw");
202+
203+
out = runSample("updateusingbatchdml");
204+
assertThat(out).contains("1 record updated by stmt 0");
205+
assertThat(out).contains("1 record updated by stmt 1");
202206
}
203207

204208
private String formatForTest(String name) {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.