php-docs-samples/spanner at master · itwebtiger/php-docs-samples · GitHub
Skip to content

Latest commit

 

History

History
 
 

Folders and files

README.md

Google Cloud Spanner PHP Samples

Open in Cloud Shell

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.

See Getting Started in PHP for a walkthrough of these samples.

Setup

Authentication

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:

  1. When running locally, use the Google Cloud SDK

     gcloud auth application-default login
    
  2. 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.

  3. 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
    

Install Dependencies

  1. Ensure the gRPC PHP Extension is installed and enabled on your machine.

  2. Enable the Cloud Spanner API.

  3. Install dependencies via Composer. Run php composer.phar install (if composer is installed locally) or composer install (if composer is installed globally).

  4. Create a service account at the Service account section in the Cloud Console

  5. Download the json key file of the service account.

  6. Set GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to that file.

Samples

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.
  add-column                           Adds a new column to the Albums table in the example database.
  add-timestamp-column                 Adds a commit timestamp column to a table.
  batch-query-data                     Batch queries sample data from the database using SQL.
  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.
  create-table-timestamp               Creates a table with a commit timestamp column.
  help                                 Displays help for a command
  insert-data                          Inserts sample data into the given database.
  insert-data-timestamp                Inserts data into a table with a commit timestamp column.
  insert-struct-data                   Inserts sample data that can be used to test STRUCT parameters in queries.
  list                                 Lists commands
  query-data                           Queries sample data from the database using SQL.
  query-data-timestamp                 Queries sample data from a database with a commit timestamp column.
  query-data-with-array-of-struct      Queries sample data from the database with an array of struct.
  query-data-with-index                Queries sample data from the database using SQL and an index.
  query-data-with-nested-struct-field  Queries sample data from the database with a nested struct field value.
  query-data-with-new-column           Queries sample data from the database using SQL.
  query-data-with-struct               Queries sample data from the database with a struct.
  query-data-with-struct-field         Queries sample data from the database with a struct field value.
  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-stale-data                      Reads sample data from the database, with a maximum staleness of 3 seconds.
  read-write-transaction               Performs a read-write transaction to update two sample records in the database.
  update-data                          Updates sample data in the database.
  update-data-timestamp                Updates sample data in a table with a commit timestamp column.

Troubleshooting

If you get the following error, set the environment variable GCLOUD_PROJECT to your project ID:

[Google\Cloud\Core\Exception\GoogleException]
No project ID was provided, and we were unable to detect a default project ID.

The client library

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.