|
30 | 30 | import com.google.cloud.spanner.ReadOnlyTransaction;
|
31 | 31 | import com.google.cloud.spanner.ResultSet;
|
32 | 32 | import com.google.cloud.spanner.Spanner;
|
| 33 | +import com.google.cloud.spanner.SpannerBatchUpdateException; |
33 | 34 | import com.google.cloud.spanner.SpannerException;
|
34 | 35 | import com.google.cloud.spanner.SpannerExceptionFactory;
|
35 | 36 | import com.google.cloud.spanner.SpannerOptions;
|
@@ -1084,6 +1085,39 @@ static void deleteUsingPartitionedDml(DatabaseClient dbClient) {
|
1084 | 1085 | }
|
1085 | 1086 | // [END spanner_dml_partitioned_delete]
|
1086 | 1087 |
|
| 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 | + |
1087 | 1121 | static void run(
|
1088 | 1122 | DatabaseClient dbClient,
|
1089 | 1123 | DatabaseAdminClient dbAdminClient,
|
@@ -1203,7 +1237,10 @@ static void run(
|
1203 | 1237 | break;
|
1204 | 1238 | case "deleteusingpartitioneddml":
|
1205 | 1239 | deleteUsingPartitionedDml(dbClient);
|
1206 |
| - break; |
| 1240 | + break; |
| 1241 | + case "updateusingbatchdml": |
| 1242 | + updateUsingBatchDml(dbClient); |
| 1243 | + break; |
1207 | 1244 | default:
|
1208 | 1245 | printUsageAndExit();
|
1209 | 1246 | }
|
@@ -1252,6 +1289,7 @@ static void printUsageAndExit() {
|
1252 | 1289 | System.err.println(" SpannerExample writewithtransactionusingdml my-instance example-db");
|
1253 | 1290 | System.err.println(" SpannerExample updateusingpartitioneddml my-instance example-db");
|
1254 | 1291 | System.err.println(" SpannerExample deleteusingpartitioneddml my-instance example-db");
|
| 1292 | + System.err.println(" SpannerExample updateusingbatchdml my-instance example-db"); |
1255 | 1293 | System.exit(1);
|
1256 | 1294 | }
|
1257 | 1295 |
|
|
0 commit comments