diff --git a/spanner/src/batch_write.php b/spanner/src/batch_write.php new file mode 100644 index 0000000000..79a46e5fec --- /dev/null +++ b/spanner/src/batch_write.php @@ -0,0 +1,138 @@ + $projectId]); + $database = $spanner->instance($instanceId)->database($databaseId); + + // Create Mutation Groups + // All mutations within a single group are applied atomically. + // Mutations across groups are applied non-atomically. + + // Group 1: Single mutation + $mutationGroup1 = new MutationGroup([ + 'mutations' => [ + new Mutation([ + 'insert_or_update' => new Write([ + 'table' => 'Singers', + 'columns' => ['SingerId', 'FirstName', 'LastName'], + 'values' => [ + [16, 'Scarlet', 'Terry'] + ] + ]) + ]) + ] + ]); + + // Group 2: Multiple mutations + $mutationGroup2 = new MutationGroup([ + 'mutations' => [ + new Mutation([ + 'insert_or_update' => new Write([ + 'table' => 'Singers', + 'columns' => ['SingerId', 'FirstName'], + 'values' => [ + [17, 'Marc'] + ] + ]) + ]), + new Mutation([ + 'insert_or_update' => new Write([ + 'table' => 'Singers', + 'columns' => ['SingerId', 'FirstName', 'LastName'], + 'values' => [ + [18, 'Catalina', 'Smith'] + ] + ]) + ]), + new Mutation([ + 'insert_or_update' => new Write([ + 'table' => 'Albums', + 'columns' => ['SingerId', 'AlbumId', 'AlbumTitle'], + 'values' => [ + [17, 1, 'Total Junk'] + ] + ]) + ]), + new Mutation([ + 'insert_or_update' => new Write([ + 'table' => 'Albums', + 'columns' => ['SingerId', 'AlbumId', 'AlbumTitle'], + 'values' => [ + [18, 2, 'Go, Go, Go'] + ] + ]) + ]) + ] + ]); + + $responses = $database->batchWriteAtLeastOnce([$mutationGroup1, $mutationGroup2], [ + 'requestOptions' => ['transactionTag' => 'batch-write-tag'] + ]); + + // Check the response code of each response to determine whether the mutation group(s) were applied successfully. + foreach ($responses as $response) { + $status = $response->getStatus(); + $indexes = implode(', ', iterator_to_array($response->getIndexes())); + if ($status->getCode() === 0) { + $timestamp = $response->getCommitTimestamp(); + printf('Mutation group indexes [%s] have been applied with commit timestamp %s' . PHP_EOL, + $indexes, + $timestamp ? $timestamp->getSeconds() : 'Unknown' + ); + } else { + printf('Mutation group indexes [%s] could not be applied with error code %s and error message %s' . PHP_EOL, + $indexes, + $status->getCode(), + $status->getMessage() + ); + } + } +} +// [END spanner_batch_write_at_least_once] + +// The following 2 lines are only needed to run the samples +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/spanner/test/spannerBatchWriteTest.php b/spanner/test/spannerBatchWriteTest.php new file mode 100644 index 0000000000..412cd59722 --- /dev/null +++ b/spanner/test/spannerBatchWriteTest.php @@ -0,0 +1,70 @@ +runFunctionSnippet('batch_write'); + $this->assertStringContainsString('Mutation group indexes', $output); + $this->assertStringContainsString('have been applied', $output); + } + + private function runFunctionSnippet($sampleName, $params = []) + { + return $this->traitRunFunctionSnippet( + $sampleName, + array_merge([self::$projectId, self::$instanceId, self::$databaseId], array_values($params)) + ); + } +}