PHP hello world

This code sample is a "hello world" application that runs on PHP. The sampleillustrates 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.

    If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    If you're using a local shell, then create local authentication credentials for your user account:

    gcloudauthapplication-defaultlogin

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

For more information, see Set up authentication for a local development environment.

Running the sample

This code sample uses thePHP client library for Cloud Bigtable package of theGoogle Cloud Client Library for PHP to communicate with Bigtable.

To run this sample program, follow theinstructions for the sample on GitHub.

Using the Cloud Client Library with Bigtable

The sample application connects to Bigtable and demonstratessome basic operations.

Requiring the client library

The sample uses ApiCore'sApiException class as well as anumber of classes in thePHP client for Bigtable.

use Google\ApiCore\ApiException;use Google\Cloud\Bigtable\Admin\V2\Client\BigtableInstanceAdminClient;use Google\Cloud\Bigtable\Admin\V2\Client\BigtableTableAdminClient;use Google\Cloud\Bigtable\Admin\V2\ColumnFamily;use Google\Cloud\Bigtable\Admin\V2\CreateTableRequest;use Google\Cloud\Bigtable\Admin\V2\DeleteTableRequest;use Google\Cloud\Bigtable\Admin\V2\GetTableRequest;use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest;use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest\Modification;use Google\Cloud\Bigtable\Admin\V2\Table;use Google\Cloud\Bigtable\Admin\V2\Table\View;use Google\Cloud\Bigtable\BigtableClient;use Google\Cloud\Bigtable\Mutations;use Google\Cloud\Bigtable\V2\RowFilter;

Connecting to Bigtable

Establish the variables you will use in your application, using a validGoogle Cloud project ID, Bigtable instance ID, and table ID.Then instantiate newBigtableInstanceAdminClient,BigtableTableAdminClient, andBigtableClient objects that youuse to connect to Bigtable.

/** Uncomment and populate these variables in your code */// $projectId = 'The Google project ID';// $instanceId = 'The Bigtable instance ID';// $tableId = 'The Bigtable table ID';$instanceAdminClient = new BigtableInstanceAdminClient();$tableAdminClient = new BigtableTableAdminClient();$dataClient = new BigtableClient([    'projectId' => $projectId,]);

Creating a table

Check to see if your table already exists. If it doesn't, call thecreatetable() method to create aTable object. The table has a single column family that retains oneversion of each column value.

Note: Columns that are related to one another are typically grouped into a column family. For more information about column families, see theBigtable storage model.
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);$tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);// Check whether table exists in an instance.// Create table if it does not exists.$table = new Table();printf('Creating a Table: %s' . PHP_EOL, $tableId);try {    $getTableRequest = (new GetTableRequest())        ->setName($tableName)        ->setView(View::NAME_ONLY);    $tableAdminClient->getTable($getTableRequest);    printf('Table %s already exists' . PHP_EOL, $tableId);} catch (ApiException $e) {    if ($e->getStatus() === 'NOT_FOUND') {        printf('Creating the %s table' . PHP_EOL, $tableId);        $createTableRequest = (new CreateTableRequest())            ->setParent($instanceName)            ->setTableId($tableId)            ->setTable($table);        $tableAdminClient->createtable($createTableRequest);        $columnFamily = new ColumnFamily();        $columnModification = new Modification();        $columnModification->setId('cf1');        $columnModification->setCreate($columnFamily);        $modifyColumnFamiliesRequest = (new ModifyColumnFamiliesRequest())            ->setName($tableName)            ->setModifications([$columnModification]);        $tableAdminClient->modifyColumnFamilies($modifyColumnFamiliesRequest);        printf('Created table %s' . PHP_EOL, $tableId);    } else {        throw $e;    }}

Writing rows to a table

Next, use a string array of greetings to create some new rows for the table. Foreach greeting, create a newMutations object and add it toentries usingupsert(). Then write the entries to the table usingthe table'smutateRows() method.

Note: In this sample application,upsert() is only called once per mutation,but you can call it up to 10,000 times per single mutation.
$table = $dataClient->table($instanceId, $tableId);printf('Writing some greetings to the table.' . PHP_EOL);$greetings = ['Hello World!', 'Hello Cloud Bigtable!', 'Hello PHP!'];$entries = [];$columnFamilyId = 'cf1';$column = 'greeting';foreach ($greetings as $i => $value) {    $rowKey = sprintf('greeting%s', $i);    $rowMutation = new Mutations();    $rowMutation->upsert($columnFamilyId, $column, $value, time() * 1000 * 1000);    $entries[$rowKey] = $rowMutation;}$table->mutateRows($entries);

Using a filter to read a row

Before you read the data that you wrote, create a filter to limit the data thatBigtable returns. This filter tells Bigtable toreturn only the most recent version of each value, even if the table containsolder versions that haven't been garbage-collected.

Create a row object, then call thereadRow() method,passing in the filter, to get one version of each column in that row.

printf('Getting a single greeting by row key.' . PHP_EOL);$key = 'greeting0';// Only retrieve the most recent version of the cell.$rowFilter = (new RowFilter())->setCellsPerColumnLimitFilter(1);$column = 'greeting';$columnFamilyId = 'cf1';$row = $table->readRow($key, [    'filter' => $rowFilter]);printf('%s' . PHP_EOL, $row[$columnFamilyId][$column][0]['value']);

Scanning all table rows

Call thereadRows() method, passing in thefilter, to get all of the rows in the table. Because you passed in the filter,Bigtable returns only one version of each value.

$columnFamilyId = 'cf1';$column = 'greeting';printf('Scanning for all greetings:' . PHP_EOL);$partialRows = $table->readRows([])->readAll();foreach ($partialRows as $row) {    printf('%s' . PHP_EOL, $row[$columnFamilyId][$column][0]['value']);}

Deleting a table

Delete the table with the admin client'sdeleteTable() method.

try {    printf('Attempting to delete table %s.' . PHP_EOL, $tableId);    $deleteTableRequest = (new DeleteTableRequest())        ->setName($tableName);    $tableAdminClient->deleteTable($deleteTableRequest);    printf('Deleted %s table.' . PHP_EOL, $tableId);} catch (ApiException $e) {    if ($e->getStatus() === 'NOT_FOUND') {        printf('Table %s does not exists' . PHP_EOL, $tableId);    } else {        throw $e;    }}

Putting it all together

Here is the full code sample without comments.

<?phprequire_once __DIR__ . '/../vendor/autoload.php';if (count($argv) != 4) {    return printf('Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID' . PHP_EOL, __FILE__);}list($_, $projectId, $instanceId, $tableId) = $argv;use Google\ApiCore\ApiException;use Google\Cloud\Bigtable\Admin\V2\Client\BigtableInstanceAdminClient;use Google\Cloud\Bigtable\Admin\V2\Client\BigtableTableAdminClient;use Google\Cloud\Bigtable\Admin\V2\ColumnFamily;use Google\Cloud\Bigtable\Admin\V2\CreateTableRequest;use Google\Cloud\Bigtable\Admin\V2\DeleteTableRequest;use Google\Cloud\Bigtable\Admin\V2\GetTableRequest;use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest;use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest\Modification;use Google\Cloud\Bigtable\Admin\V2\Table;use Google\Cloud\Bigtable\Admin\V2\Table\View;use Google\Cloud\Bigtable\BigtableClient;use Google\Cloud\Bigtable\Mutations;use Google\Cloud\Bigtable\V2\RowFilter;$instanceAdminClient = new BigtableInstanceAdminClient();$tableAdminClient = new BigtableTableAdminClient();$dataClient = new BigtableClient([    'projectId' => $projectId,]);$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);$tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);$table = new Table();printf('Creating a Table: %s' . PHP_EOL, $tableId);try {    $getTableRequest = (new GetTableRequest())        ->setName($tableName)        ->setView(View::NAME_ONLY);    $tableAdminClient->getTable($getTableRequest);    printf('Table %s already exists' . PHP_EOL, $tableId);} catch (ApiException $e) {    if ($e->getStatus() === 'NOT_FOUND') {        printf('Creating the %s table' . PHP_EOL, $tableId);        $createTableRequest = (new CreateTableRequest())            ->setParent($instanceName)            ->setTableId($tableId)            ->setTable($table);        $tableAdminClient->createtable($createTableRequest);        $columnFamily = new ColumnFamily();        $columnModification = new Modification();        $columnModification->setId('cf1');        $columnModification->setCreate($columnFamily);        $modifyColumnFamiliesRequest = (new ModifyColumnFamiliesRequest())            ->setName($tableName)            ->setModifications([$columnModification]);        $tableAdminClient->modifyColumnFamilies($modifyColumnFamiliesRequest);        printf('Created table %s' . PHP_EOL, $tableId);    } else {        throw $e;    }}$table = $dataClient->table($instanceId, $tableId);printf('Writing some greetings to the table.' . PHP_EOL);$greetings = ['Hello World!', 'Hello Cloud Bigtable!', 'Hello PHP!'];$entries = [];$columnFamilyId = 'cf1';$column = 'greeting';foreach ($greetings as $i => $value) {    $rowKey = sprintf('greeting%s', $i);    $rowMutation = new Mutations();    $rowMutation->upsert($columnFamilyId, $column, $value, time() * 1000 * 1000);    $entries[$rowKey] = $rowMutation;}$table->mutateRows($entries);printf('Getting a single greeting by row key.' . PHP_EOL);$key = 'greeting0';$rowFilter = (new RowFilter())->setCellsPerColumnLimitFilter(1);$column = 'greeting';$columnFamilyId = 'cf1';$row = $table->readRow($key, [    'filter' => $rowFilter]);printf('%s' . PHP_EOL, $row[$columnFamilyId][$column][0]['value']);$columnFamilyId = 'cf1';$column = 'greeting';printf('Scanning for all greetings:' . PHP_EOL);$partialRows = $table->readRows([])->readAll();foreach ($partialRows as $row) {    printf('%s' . PHP_EOL, $row[$columnFamilyId][$column][0]['value']);}try {    printf('Attempting to delete table %s.' . PHP_EOL, $tableId);    $deleteTableRequest = (new DeleteTableRequest())        ->setName($tableName);    $tableAdminClient->deleteTable($deleteTableRequest);    printf('Deleted %s table.' . PHP_EOL, $tableId);} catch (ApiException $e) {    if ($e->getStatus() === 'NOT_FOUND') {        printf('Table %s does not exists' . PHP_EOL, $tableId);    } else {        throw $e;    }}

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.