Skip to content

Commit 3f55744

Browse files
billyjacobsonbshaffer
authored andcommitted
Bigtable write samples (GoogleCloudPlatform#907)
1 parent b9f9784 commit 3f55744

32 files changed

+397
-47
lines changed

bigtable/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Google BigTable Sample
1+
# Google Bigtable Sample
22

33
## Description
44

55
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.
66

77
## Build and Run
8-
1. **Enable APIs** - [Enable the BigTable API](https://console.cloud.google.com/flows/enableapi?apiid=bigtable)
8+
1. **Enable APIs** - [Enable the Bigtable API](https://console.cloud.google.com/flows/enableapi?apiid=bigtable)
99
and create a new project or select an existing project.
1010
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials"
1111
and select "Service Account Key". Create a new service account, use the JSON key type, and

bigtable/composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"require": {
3-
"google/cloud-bigtable": "^0.12.1"
3+
"google/cloud-bigtable": "^0.12.1",
4+
"psr/cache": "^1.0"
45
},
56
"require-dev": {
6-
"google/cloud-tools": "^0.8",
7+
"google/cloud-tools": "dev-master",
78
"phpunit/phpunit": "^5"
89
}
910
}

bigtable/phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
timeoutForMediumTests="30"
2727
timeoutForLargeTests="120">
2828
<testsuites>
29-
<testsuite name="PHP bigtable Instance test">
30-
<file>./test/bigtableTest.phpfile>
29+
<testsuite name="PHP Bigtable tests">
30+
<directory>testdirectory>
3131
testsuite>
3232
testsuites>
3333
<logging>

bigtable/src/create_cluster.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) < 3 || count($argv) > 5) {

bigtable/src/create_dev_instance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) < 3 || count($argv) > 5) {

bigtable/src/create_family_gc_intersection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 4) {

bigtable/src/create_family_gc_max_age.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 4) {

bigtable/src/create_family_gc_max_versions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 4) {

bigtable/src/create_family_gc_nested.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 4) {

bigtable/src/create_family_gc_union.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 4) {

bigtable/src/create_production_instance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) < 3 || count($argv) > 4) {

bigtable/src/create_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 4) {

bigtable/src/delete_cluster.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 4) {

bigtable/src/delete_family.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 4) {

bigtable/src/delete_instance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 3) {

bigtable/src/delete_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 4) {

bigtable/src/helloworld/create_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../../vendor/autoload.php';
2727

2828
if (count($argv) != 4) {

bigtable/src/helloworld/get_with_filter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../../vendor/autoload.php';
2727

2828
if (count($argv) < 3 || count($argv) > 5) {

bigtable/src/helloworld/scan_all.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../../vendor/autoload.php';
2727

2828
if (count($argv) < 3 || count($argv) > 5) {

bigtable/src/helloworld/write_rows.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../../vendor/autoload.php';
2727

2828
if (count($argv) < 3 || count($argv) > 5) {

bigtable/src/list_column_families.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 4) {

bigtable/src/list_instance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 3) {

bigtable/src/list_instance_clusters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 3) {

bigtable/src/list_tables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) != 3) {

bigtable/src/quickstart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) < 3 || count($argv) > 5) {

bigtable/src/update_gc_rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependendencies using Composer
25+
// Include Google Cloud dependencies using Composer
2626
require_once __DIR__ . '/../vendor/autoload.php';
2727

2828
if (count($argv) < 3 || count($argv) > 5) {

bigtable/src/writes/write_batch.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
3+
/**
4+
* Copyright 2019 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
23+
*/
24+
25+
// Include Google Cloud dependencies using Composer
26+
require_once __DIR__ . '/../../vendor/autoload.php';
27+
28+
if (count($argv) < 3 || count($argv) > 5) {
29+
return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID" . PHP_EOL, __FILE__);
30+
}
31+
list($_, $project_id, $instance_id, $table_id) = $argv;
32+
33+
// [START bigtable_writes_batch]
34+
35+
use Google\Cloud\Bigtable\BigtableClient;
36+
use Google\Cloud\Bigtable\Mutations;
37+
38+
/** Uncomment and populate these variables in your code */
39+
// $project_id = 'The Google project ID';
40+
// $instance_id = 'The Bigtable instance ID';
41+
// $table_id = 'mobile-time-series';
42+
43+
// Connect to an existing table with an existing instance.
44+
$dataClient = new BigtableClient([
45+
'projectId' => $project_id,
46+
]);
47+
$table = $dataClient->table($instance_id, $table_id);
48+
49+
$timestamp = time() * 1000;
50+
$columnFamilyId = 'stats_summary';
51+
$mutations = [
52+
(new Mutations())
53+
->upsert($columnFamilyId, "connected_wifi", 1, $timestamp)
54+
->upsert($columnFamilyId, "os_build", "12155.0.0-rc1", $timestamp),
55+
(new Mutations())
56+
->upsert($columnFamilyId, "connected_wifi", 1, $timestamp)
57+
->upsert($columnFamilyId, "os_build", "12145.0.0-rc6", $timestamp)];
58+
59+
$table->mutateRows([
60+
"tablet#a0b81f74#20190501" => $mutations[0],
61+
"tablet#a0b81f74#20190502" => $mutations[1]]
62+
);
63+
64+
printf('Successfully wrote 2 rows.' . PHP_EOL);
65+
// [END bigtable_writes_batch]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
3+
/**
4+
* Copyright 2019 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
23+
*/
24+
25+
// Include Google Cloud dependencies using Composer
26+
require_once __DIR__ . '/../../vendor/autoload.php';
27+
28+
if (count($argv) < 3 || count($argv) > 5) {
29+
return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID" . PHP_EOL, __FILE__);
30+
}
31+
list($_, $project_id, $instance_id, $table_id) = $argv;
32+
33+
// [START bigtable_writes_conditional]
34+
35+
use Google\Cloud\Bigtable\BigtableClient;
36+
use Google\Cloud\Bigtable\Filter;
37+
use Google\Cloud\Bigtable\Mutations;
38+
39+
/** Uncomment and populate these variables in your code */
40+
// $project_id = 'The Google project ID';
41+
// $instance_id = 'The Bigtable instance ID';
42+
// $table_id = 'mobile-time-series';
43+
44+
// Connect to an existing table with an existing instance.
45+
$dataClient = new BigtableClient([
46+
'projectId' => $project_id,
47+
]);
48+
$table = $dataClient->table($instance_id, $table_id);
49+
50+
$timestamp = time() * 1000;
51+
$columnFamilyId = 'stats_summary';
52+
53+
$mutations = (new Mutations())->upsert($columnFamilyId, "os_name", "android", $timestamp);
54+
$predicateFilter = Filter::chain()
55+
->addFilter(Filter::family()->exactMatch($columnFamilyId))
56+
->addFilter(Filter::qualifier()->exactMatch('os_build'))
57+
->addFilter(Filter::value()->regex('PQ2A.*'));
58+
$options = ['predicateFilter' => $predicateFilter, 'trueMutations' => $mutations];
59+
60+
$table->checkAndMutateRow("phone#4c410523#20190501", $options);
61+
62+
printf('Successfully updated row\'s os_name' . PHP_EOL);
63+
// [END bigtable_writes_conditional]

0 commit comments

Comments
 (0)