diff --git a/bigtable/README.md b/bigtable/README.md index fcc35f1dc8..635ab3842b 100644 --- a/bigtable/README.md +++ b/bigtable/README.md @@ -1,11 +1,11 @@ -# Google BigTable Sample +# Google Bigtable Sample ## Description All code in the `src` directory demonstrates how to connect to Cloud Bigtable and run some basic operations to create instance, create cluster, delete instance and delete cluster. ## Build and Run -1. **Enable APIs** - [Enable the BigTable API](https://console.cloud.google.com/flows/enableapi?apiid=bigtable) +1. **Enable APIs** - [Enable the Bigtable API](https://console.cloud.google.com/flows/enableapi?apiid=bigtable) and create a new project or select an existing project. 2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials" and select "Service Account Key". Create a new service account, use the JSON key type, and diff --git a/bigtable/composer.json b/bigtable/composer.json index 9288005599..b172733a3f 100644 --- a/bigtable/composer.json +++ b/bigtable/composer.json @@ -1,9 +1,10 @@ { "require": { - "google/cloud-bigtable": "^0.12.1" + "google/cloud-bigtable": "^0.12.1", + "psr/cache": "^1.0" }, "require-dev": { - "google/cloud-tools": "^0.8", + "google/cloud-tools": "dev-master", "phpunit/phpunit": "^5" } } diff --git a/bigtable/phpunit.xml.dist b/bigtable/phpunit.xml.dist index 90407917fd..b481ecb82e 100644 --- a/bigtable/phpunit.xml.dist +++ b/bigtable/phpunit.xml.dist @@ -26,8 +26,8 @@ timeoutForMediumTests="30" timeoutForLargeTests="120"> - - ./test/bigtableTest.php + + test diff --git a/bigtable/src/create_cluster.php b/bigtable/src/create_cluster.php index 74be599188..85a6f90b52 100644 --- a/bigtable/src/create_cluster.php +++ b/bigtable/src/create_cluster.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) < 3 || count($argv) > 5) { diff --git a/bigtable/src/create_dev_instance.php b/bigtable/src/create_dev_instance.php index 3f243dd34c..2b05d1434b 100644 --- a/bigtable/src/create_dev_instance.php +++ b/bigtable/src/create_dev_instance.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) < 3 || count($argv) > 5) { diff --git a/bigtable/src/create_family_gc_intersection.php b/bigtable/src/create_family_gc_intersection.php index f886bf41a3..851eceb84c 100644 --- a/bigtable/src/create_family_gc_intersection.php +++ b/bigtable/src/create_family_gc_intersection.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 4) { diff --git a/bigtable/src/create_family_gc_max_age.php b/bigtable/src/create_family_gc_max_age.php index 5a72c986cc..da00a4cfd7 100644 --- a/bigtable/src/create_family_gc_max_age.php +++ b/bigtable/src/create_family_gc_max_age.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 4) { diff --git a/bigtable/src/create_family_gc_max_versions.php b/bigtable/src/create_family_gc_max_versions.php index 0f18f68c9c..709e31c7dd 100644 --- a/bigtable/src/create_family_gc_max_versions.php +++ b/bigtable/src/create_family_gc_max_versions.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 4) { diff --git a/bigtable/src/create_family_gc_nested.php b/bigtable/src/create_family_gc_nested.php index b287a63821..b2dfc7b2b9 100644 --- a/bigtable/src/create_family_gc_nested.php +++ b/bigtable/src/create_family_gc_nested.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 4) { diff --git a/bigtable/src/create_family_gc_union.php b/bigtable/src/create_family_gc_union.php index 9233857b5c..54a5756dba 100644 --- a/bigtable/src/create_family_gc_union.php +++ b/bigtable/src/create_family_gc_union.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 4) { diff --git a/bigtable/src/create_production_instance.php b/bigtable/src/create_production_instance.php index 11d4c4470d..30bfba4487 100644 --- a/bigtable/src/create_production_instance.php +++ b/bigtable/src/create_production_instance.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) < 3 || count($argv) > 4) { diff --git a/bigtable/src/create_table.php b/bigtable/src/create_table.php index f289d9798c..330ca8e95a 100644 --- a/bigtable/src/create_table.php +++ b/bigtable/src/create_table.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 4) { diff --git a/bigtable/src/delete_cluster.php b/bigtable/src/delete_cluster.php index d694c96bf8..85ce126772 100644 --- a/bigtable/src/delete_cluster.php +++ b/bigtable/src/delete_cluster.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 4) { diff --git a/bigtable/src/delete_family.php b/bigtable/src/delete_family.php index e15ef343ea..aaf4e81dbb 100644 --- a/bigtable/src/delete_family.php +++ b/bigtable/src/delete_family.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 4) { diff --git a/bigtable/src/delete_instance.php b/bigtable/src/delete_instance.php index 84e2c22e49..d59f389ad6 100644 --- a/bigtable/src/delete_instance.php +++ b/bigtable/src/delete_instance.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 3) { diff --git a/bigtable/src/delete_table.php b/bigtable/src/delete_table.php index b621a4b9b5..9b2664a726 100644 --- a/bigtable/src/delete_table.php +++ b/bigtable/src/delete_table.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 4) { diff --git a/bigtable/src/helloworld/create_table.php b/bigtable/src/helloworld/create_table.php index d53bfcff07..e6db785da9 100644 --- a/bigtable/src/helloworld/create_table.php +++ b/bigtable/src/helloworld/create_table.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../../vendor/autoload.php'; if (count($argv) != 4) { diff --git a/bigtable/src/helloworld/get_with_filter.php b/bigtable/src/helloworld/get_with_filter.php index 1b17005159..4a6c752946 100644 --- a/bigtable/src/helloworld/get_with_filter.php +++ b/bigtable/src/helloworld/get_with_filter.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../../vendor/autoload.php'; if (count($argv) < 3 || count($argv) > 5) { diff --git a/bigtable/src/helloworld/scan_all.php b/bigtable/src/helloworld/scan_all.php index b945e0701b..dac412e951 100644 --- a/bigtable/src/helloworld/scan_all.php +++ b/bigtable/src/helloworld/scan_all.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../../vendor/autoload.php'; if (count($argv) < 3 || count($argv) > 5) { diff --git a/bigtable/src/helloworld/write_rows.php b/bigtable/src/helloworld/write_rows.php index 933320e141..09dae515aa 100644 --- a/bigtable/src/helloworld/write_rows.php +++ b/bigtable/src/helloworld/write_rows.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../../vendor/autoload.php'; if (count($argv) < 3 || count($argv) > 5) { diff --git a/bigtable/src/list_column_families.php b/bigtable/src/list_column_families.php index 1e4b473c6d..229c1763e2 100644 --- a/bigtable/src/list_column_families.php +++ b/bigtable/src/list_column_families.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 4) { diff --git a/bigtable/src/list_instance.php b/bigtable/src/list_instance.php index 6dd442a943..71205103bc 100644 --- a/bigtable/src/list_instance.php +++ b/bigtable/src/list_instance.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 3) { diff --git a/bigtable/src/list_instance_clusters.php b/bigtable/src/list_instance_clusters.php index e26eb00f71..f8be0fa5db 100644 --- a/bigtable/src/list_instance_clusters.php +++ b/bigtable/src/list_instance_clusters.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 3) { diff --git a/bigtable/src/list_tables.php b/bigtable/src/list_tables.php index 40d3eee298..2ccef8828f 100644 --- a/bigtable/src/list_tables.php +++ b/bigtable/src/list_tables.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) != 3) { diff --git a/bigtable/src/quickstart.php b/bigtable/src/quickstart.php index d5efe84460..40b32b11e1 100644 --- a/bigtable/src/quickstart.php +++ b/bigtable/src/quickstart.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) < 3 || count($argv) > 5) { diff --git a/bigtable/src/update_gc_rule.php b/bigtable/src/update_gc_rule.php index 181dac39f3..dcc1ae36ec 100644 --- a/bigtable/src/update_gc_rule.php +++ b/bigtable/src/update_gc_rule.php @@ -22,7 +22,7 @@ * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md */ -// Include Google Cloud dependendencies using Composer +// Include Google Cloud dependencies using Composer require_once __DIR__ . '/../vendor/autoload.php'; if (count($argv) < 3 || count($argv) > 5) { diff --git a/bigtable/src/writes/write_batch.php b/bigtable/src/writes/write_batch.php new file mode 100644 index 0000000000..aa430a2304 --- /dev/null +++ b/bigtable/src/writes/write_batch.php @@ -0,0 +1,65 @@ + 5) { + return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID" . PHP_EOL, __FILE__); +} +list($_, $project_id, $instance_id, $table_id) = $argv; + +// [START bigtable_writes_batch] + +use Google\Cloud\Bigtable\BigtableClient; +use Google\Cloud\Bigtable\Mutations; + +/** Uncomment and populate these variables in your code */ +// $project_id = 'The Google project ID'; +// $instance_id = 'The Bigtable instance ID'; +// $table_id = 'mobile-time-series'; + +// Connect to an existing table with an existing instance. +$dataClient = new BigtableClient([ + 'projectId' => $project_id, +]); +$table = $dataClient->table($instance_id, $table_id); + +$timestamp = time() * 1000; +$columnFamilyId = 'stats_summary'; +$mutations = [ + (new Mutations()) + ->upsert($columnFamilyId, "connected_wifi", 1, $timestamp) + ->upsert($columnFamilyId, "os_build", "12155.0.0-rc1", $timestamp), + (new Mutations()) + ->upsert($columnFamilyId, "connected_wifi", 1, $timestamp) + ->upsert($columnFamilyId, "os_build", "12145.0.0-rc6", $timestamp)]; + +$table->mutateRows([ + "tablet#a0b81f74#20190501" => $mutations[0], + "tablet#a0b81f74#20190502" => $mutations[1]] +); + +printf('Successfully wrote 2 rows.' . PHP_EOL); +// [END bigtable_writes_batch] diff --git a/bigtable/src/writes/write_conditionally.php b/bigtable/src/writes/write_conditionally.php new file mode 100644 index 0000000000..f8136e9350 --- /dev/null +++ b/bigtable/src/writes/write_conditionally.php @@ -0,0 +1,63 @@ + 5) { + return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID" . PHP_EOL, __FILE__); +} +list($_, $project_id, $instance_id, $table_id) = $argv; + +// [START bigtable_writes_conditional] + +use Google\Cloud\Bigtable\BigtableClient; +use Google\Cloud\Bigtable\Filter; +use Google\Cloud\Bigtable\Mutations; + +/** Uncomment and populate these variables in your code */ +// $project_id = 'The Google project ID'; +// $instance_id = 'The Bigtable instance ID'; +// $table_id = 'mobile-time-series'; + +// Connect to an existing table with an existing instance. +$dataClient = new BigtableClient([ + 'projectId' => $project_id, +]); +$table = $dataClient->table($instance_id, $table_id); + +$timestamp = time() * 1000; +$columnFamilyId = 'stats_summary'; + +$mutations = (new Mutations())->upsert($columnFamilyId, "os_name", "android", $timestamp); +$predicateFilter = Filter::chain() + ->addFilter(Filter::family()->exactMatch($columnFamilyId)) + ->addFilter(Filter::qualifier()->exactMatch('os_build')) + ->addFilter(Filter::value()->regex('PQ2A.*')); +$options = ['predicateFilter' => $predicateFilter, 'trueMutations' => $mutations]; + +$table->checkAndMutateRow("phone#4c410523#20190501", $options); + +printf('Successfully updated row\'s os_name' . PHP_EOL); +// [END bigtable_writes_conditional] diff --git a/bigtable/src/writes/write_increment.php b/bigtable/src/writes/write_increment.php new file mode 100644 index 0000000000..65afb6ed20 --- /dev/null +++ b/bigtable/src/writes/write_increment.php @@ -0,0 +1,56 @@ + 5) { + return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID" . PHP_EOL, __FILE__); +} +list($_, $project_id, $instance_id, $table_id) = $argv; + +// [START bigtable_writes_increment] + +use Google\Cloud\Bigtable\BigtableClient; +use Google\Cloud\Bigtable\ReadModifyWriteRowRules; + +/** Uncomment and populate these variables in your code */ +// $project_id = 'The Google project ID'; +// $instance_id = 'The Bigtable instance ID'; +// $table_id = 'mobile-time-series'; + +// Connect to an existing table with an existing instance. +$dataClient = new BigtableClient([ + 'projectId' => $project_id, +]); +$table = $dataClient->table($instance_id, $table_id); + +$timestamp = time() * 1000; +$columnFamilyId = 'stats_summary'; + +$rules = (new ReadModifyWriteRowRules)->increment($columnFamilyId, 'connected_wifi', -1); +$row = $table->readModifyWriteRow('phone#4c410523#20190501', $rules); + +printf('Successfully updated row.' . PHP_EOL); +// [END bigtable_writes_increment] diff --git a/bigtable/src/writes/write_simple.php b/bigtable/src/writes/write_simple.php new file mode 100644 index 0000000000..49a649fe93 --- /dev/null +++ b/bigtable/src/writes/write_simple.php @@ -0,0 +1,60 @@ + 5) { + return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID" . PHP_EOL, __FILE__); +} +list($_, $project_id, $instance_id, $table_id) = $argv; + +// [START bigtable_writes_simple] + +use Google\Cloud\Bigtable\BigtableClient; +use Google\Cloud\Bigtable\DataUtil; +use Google\Cloud\Bigtable\Mutations; + +/** Uncomment and populate these variables in your code */ +// $project_id = 'The Google project ID'; +// $instance_id = 'The Bigtable instance ID'; +// $table_id = 'mobile-time-series'; + +// Connect to an existing table with an existing instance. +$dataClient = new BigtableClient([ + 'projectId' => $project_id, +]); +$table = $dataClient->table($instance_id, $table_id); + +$timestamp = time() * 1000; +$columnFamilyId = 'stats_summary'; +$mutations = (new Mutations()) + ->upsert($columnFamilyId, "connected_cell", 1, $timestamp) + ->upsert($columnFamilyId, "connected_wifi", DataUtil::intToByteString(1), $timestamp) + ->upsert($columnFamilyId, "os_build", "PQ2A.190405.003", $timestamp); + +$table->mutateRow("phone#4c410523#20190501", $mutations); + +printf('Successfully wrote row.' . PHP_EOL); +// [END bigtable_writes_simple] diff --git a/bigtable/test/bigtableTest.php b/bigtable/test/bigtableTest.php index a3e562c06d..bff018830a 100644 --- a/bigtable/test/bigtableTest.php +++ b/bigtable/test/bigtableTest.php @@ -1,6 +1,6 @@ execute($testFunc); - } - - return $testFunc(); - } } diff --git a/bigtable/test/writeTest.php b/bigtable/test/writeTest.php new file mode 100644 index 0000000000..986ca200cc --- /dev/null +++ b/bigtable/test/writeTest.php @@ -0,0 +1,121 @@ +instanceName(self::$projectId, self::$instanceId); + $table = (new Table())->setColumnFamilies(["stats_summary" => new ColumnFamily()]); + self::$bigtableTableAdminClient->createtable( + $formattedParent, + self::$tableId, + $table + ); + } + + public function setUp() + { + $this->useResourceExhaustedBackoff(); + } + + public static function tearDownAfterClass() + { + $instanceName = self::$bigtableInstanceAdminClient->instanceName(self::$projectId, self::$instanceId); + self::$bigtableInstanceAdminClient->deleteInstance($instanceName); + } + + public function testWriteSimple() + { + $output = $this->runSnippet('writes/write_simple', [ + self::$projectId, + self::$instanceId, + self::$tableId + ]); + + $this->assertContains('Successfully wrote row.', $output); + } + + public function testWriteConditional() + { + $output = $this->runSnippet('writes/write_conditionally', [ + self::$projectId, + self::$instanceId, + self::$tableId + ]); + + $this->assertContains('Successfully updated row\'s os_name', $output); + } + + public function testWriteIncrement() + { + $output = $this->runSnippet('writes/write_increment', [ + self::$projectId, + self::$instanceId, + self::$tableId + ]); + + $this->assertContains('Successfully updated row.', $output); + } + + public function testWriteBatch() + { + $output = $this->runSnippet('writes/write_batch', [ + self::$projectId, + self::$instanceId, + self::$tableId + ]); + + $this->assertContains('Successfully wrote 2 rows.', $output); + } +}