diff --git a/spanner/src/batch_write.php b/spanner/src/batch_write.php new file mode 100644 index 0000000000..30b5267e83 --- /dev/null +++ b/spanner/src/batch_write.php @@ -0,0 +1,102 @@ + $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 = $database->mutationGroup(); + $mutationGroup1->insertOrUpdate('Singers', [ + 'SingerId' => 16, + 'FirstName' => 'Scarlet', + 'LastName' => 'Terry' + ]); + + // Group 2: Multiple mutations + $mutationGroup2 = $database->mutationGroup(); + $mutationGroup2->insertOrUpdateBatch('Singers', [ + ['SingerId' => 17, 'FirstName' => 'Marc'], + ['SingerId' => 18, 'FirstName' => 'Catalina', 'LastName' => 'Smith'] + ]); + $mutationGroup2->insertOrUpdateBatch('Albums', [ + ['SingerId' => 17, 'AlbumId' => 1, 'AlbumTitle' => 'Total Junk'], + ['SingerId' => 18, 'AlbumId' => 2, 'AlbumTitle' => 'Go, Go, Go'] + ]); + + // Call batchWrite on the high-level Database client. + // Equivalent to batchWriteAtLeastOnce in other languages. + $responses = $database->batchWrite([$mutationGroup1, $mutationGroup2], [ + 'requestOptions' => ['transactionTag' => 'batch-write-tag'] + ]); + + // Check the response code of each response to determine whether the mutation group(s) were applied successfully. + // $responses is a Generator yielding V1\BatchWriteResponse items. + // Check the response code of each response to determine whether the mutation group(s) were applied successfully. + // $responses is a Generator yielding response arrays. + foreach ($responses as $response) { + $status = $response['status']; + $indexes = implode(', ', $response['indexes']); + if ($status['code'] === 0) { + $timestamp = $response['commitTimestamp'] ?? 'Unknown'; + printf('Mutation group indexes [%s] have been applied with commit timestamp %s' . PHP_EOL, + $indexes, + $timestamp + ); + } else { + printf('Mutation group indexes [%s] could not be applied with error code %s and error message %s' . PHP_EOL, + $indexes, + $status['code'], + $status['message'] + ); + } + } +} +// [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/spannerTest.php b/spanner/test/spannerTest.php index eb06bb2e9d..548042e6cc 100644 --- a/spanner/test/spannerTest.php +++ b/spanner/test/spannerTest.php @@ -372,6 +372,16 @@ public function testInsertData() $this->assertEquals('Inserted data.' . PHP_EOL, $output); } + /** + * @depends testInsertData + */ + public function testBatchWrite() + { + $output = $this->runFunctionSnippet('batch_write'); + $this->assertStringContainsString('Mutation group indexes', $output); + $this->assertStringContainsString('have been applied', $output); + } + /** * @depends testInsertData */