This directory contains samples for Google Cloud Spanner. Google Cloud Spanner is a highly scalable, transactional, managed, NewSQL database service. Cloud Spanner solves the need for a horizontally-scaling database with consistent global transactions and SQL semantics.
Authentication is typically done through Application Default Credentials which means you do not have to change the code to authenticate as long as your environment has credentials. You have a few options for setting up authentication:
-
When running locally, use the Google Cloud SDK
gcloud auth application-default login
-
When running on App Engine or Compute Engine, credentials are already set-up. However, you may need to configure your Compute Engine instance with additional scopes.
-
You can create a Service Account key file. This file can be used to authenticate to Google Cloud Platform services from any environment. To use the file, set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to the path to the key file, for example:export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
-
Ensure the gRPC PHP Extension is installed and enabled on your machine.
-
Install dependencies via Composer. Run
php composer.phar install
(if composer is installed locally) orcomposer install
(if composer is installed globally). -
Create a service account at the Service account section in the Cloud Console
-
Download the json key file of the service account.
-
Set
GOOGLE_APPLICATION_CREDENTIALS
environment variable pointing to that file.
To run the Spanner Samples:
$ php spanner.php
Cloud Spanner
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
add-column Adds a new column to the Albums table in the example database.
create-database Creates a database and tables for sample data.
create-index Adds a simple index to the example database.
create-storing-index Adds an storing index to the example database.
help Displays help for a command
insert-data Inserts sample data into the given database.
list Lists commands
query-data Queries sample data from the database using SQL.
query-data-with-index Queries sample data from the database using SQL and an index.
query-data-with-new-column Queries sample data from the database using SQL.
read-data Reads sample data from the database.
read-data-with-index Reads sample data from the database using an index.
read-data-with-storing-index Reads sample data from the database using an index with a storing clause.
read-only-transaction Reads data inside of a read-only transaction.
read-write-transaction Performs a read-write transaction to update two sample records in the database.
update-data Updates sample data in the database.
This sample uses the Google Cloud Client Library for PHP. You can read the documentation for more details on API usage and use GitHub to browse the source and report issues.