PHP hello world
This code sample is a "hello world" application that runs on PHP. The sample illustrates how to complete the following tasks:
- Set up authentication
- Connect to a Bigtable instance.
- Create a new table.
- Write data to the table.
- Read the data back.
- Delete the table.
Set up authentication
To use the Python samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
Running the sample
This code sample uses the PHP client library for Cloud Bigtable package of the Google Cloud Client Library for PHP to communicate with Bigtable.
To run this sample program, follow the instructions for the sample on GitHub.
Using the Cloud Client Library with Bigtable
The sample application connects to Bigtable and demonstrates some basic operations.
Requiring the client library
The sample uses ApiCore's ApiException class as well as a number of classes in the PHP client for Bigtable.
Connecting to Bigtable
Establish the variables you will use in your application, using a valid Google Cloud project ID, Bigtable instance ID, and table ID. Then instantiate new BigtableInstanceAdminClient, BigtableTableAdminClient, and BigtableClient objects that you use to connect to Bigtable.
Creating a table
Check to see if your table already exists. If it doesn't, call the
createtable()
method to create a Table
object. The table has a single column family that retains one
version of each column value.
Writing rows to a table
Next, use a string array of greetings to create some new rows for the table. For
each greeting, create a new Mutations
object and add it to
entries
using upsert()
. Then write the entries to the table using
the table's mutateRows()
method.
Using a filter to read a row
Before you read the data that you wrote, create a filter to limit the data that Bigtable returns. This filter tells Bigtable to return only the most recent version of each value, even if the table contains older versions that haven't been garbage-collected.
Create a row object, then call the readRow()
method,
passing in the filter, to get one version of each column in that row.
Scanning all table rows
Call the readRows()
method, passing in the
filter, to get all of the rows in the table. Because you passed in the filter,
Bigtable returns only one version of each value.
Deleting a table
Delete the table with the admin client's deleteTable()
method.
Putting it all together
Here is the full code sample without comments.