Skip to content

Commit b616dd7

Browse files
authored
BigTable Admin Samples (GoogleCloudPlatform#775)
1 parent e90b59a commit b616dd7

24 files changed

+2539
-0
lines changed

bigtable/api/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Google BigTable Sample
2+
3+
## Description
4+
5+
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.
6+
7+
## Build and Run
8+
1. **Enable APIs** - [Enable the BigTable API](https://console.cloud.google.com/flows/enableapi?apiid=bigtable)
9+
and create a new project or select an existing project.
10+
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials"
11+
and select "Service Account Key". Create a new service account, use the JSON key type, and
12+
select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
13+
to the path of the JSON key that was downloaded.
14+
3. **Clone the repo** and cd into this directory
15+
```sh
16+
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
17+
$ cd php-docs-samples/bigtable/api
18+
```
19+
20+
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
21+
Run `php composer.phar install` (if composer is installed locally) or `composer install`
22+
(if composer is installed globally).
23+
5. Run `php SNIPPET_NAME.php`. The usage will print for each if no arguments
24+
are provided:
25+
```sh
26+
$ php src/run_instance_operations.php
27+
Usage: php src/run_instance_operations.php PROJECT_ID INSTANCE_ID TABLE_ID
28+
29+
$ php src/run_instance_operations.php your-project-id your-instance-id your-table-id
30+
```
31+
32+
## Contributing changes
33+
34+
* See [CONTRIBUTING.md](../../CONTRIBUTING.md)
35+
36+
## Licensing
37+
38+
* See [LICENSE](../../LICENSE)

bigtable/api/composer.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"require": {
3+
"google/cloud-bigtable": "^0.9.1"
4+
},
5+
"require-dev": {
6+
"google/cloud-tools": "^0.8",
7+
"phpunit/phpunit": "^5.0"
8+
}
9+
}

bigtable/api/file.txt

Lines changed: 671 additions & 0 deletions
Large diffs are not rendered by default.

bigtable/api/phpunit.xml.dist

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit backupGlobals="false"
18+
strict="true"
19+
backupStaticAttributes="false"
20+
bootstrap="vendor/autoload.php"
21+
colors="true"
22+
processIsolation="false"
23+
stopOnFailure="false"
24+
syntaxCheck="false"
25+
timeoutForSmallTests="10"
26+
timeoutForMediumTests="30"
27+
timeoutForLargeTests="120">
28+
<testsuites>
29+
<testsuite name="PHP bigtable Instance test">
30+
<file>./test/bigtableTest.phpfile>
31+
testsuite>
32+
testsuites>
33+
<logging>
34+
<log type="coverage-clover" target="./build/logs/clover.xml"/>
35+
logging>
36+
<filter>
37+
<whitelist addUncoveredFilesFromWhitelist="true">
38+
<directory>./srcdirectory>
39+
whitelist>
40+
filter>
41+
phpunit>

bigtable/api/src/create_cluster.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
3+
/**
4+
* Copyright 2018 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/api/README.md
23+
*/
24+
25+
// Include Google Cloud dependendencies 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 CLUSTER_ID [LOCATION_ID]" . PHP_EOL, __FILE__);
30+
}
31+
list($_, $project_id, $instance_id, $cluster_id) = $argv;
32+
$location_id = isset($argv[4]) ? $argv[4] : 'us-east1-b';
33+
34+
// [START bigtable_create_cluster]
35+
36+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
37+
use Google\Cloud\Bigtable\Admin\V2\Cluster;
38+
use Google\Cloud\Bigtable\Admin\V2\StorageType;
39+
use Google\ApiCore\ApiException;
40+
41+
42+
/** Uncomment and populate these variables in your code */
43+
// $project_id = 'The Google project ID';
44+
// $instance_id = 'The Bigtable instance ID';
45+
// $cluster_id = 'The Bigtable cluster ID';
46+
// $location_id = 'The Bigtable region ID';
47+
48+
49+
$instanceAdminClient = new BigtableInstanceAdminClient();
50+
51+
$instanceName = $instanceAdminClient->instanceName($project_id, $instance_id);
52+
$clusterName = $instanceAdminClient->clusterName($project_id, $instance_id, $cluster_id);
53+
54+
printf("Adding Cluster to Instance %s" . PHP_EOL, $instance_id);
55+
try {
56+
$instanceAdminClient->getInstance($instanceName);
57+
} catch (ApiException $e) {
58+
if ($e->getStatus() === 'NOT_FOUND') {
59+
printf("Instance %s does not exists." . PHP_EOL, $instance_id);
60+
return;
61+
} else {
62+
throw $e;
63+
}
64+
}
65+
printf("Listing Clusters:" . PHP_EOL);
66+
67+
$storage_type = StorageType::SSD;
68+
$serve_nodes = 3;
69+
70+
$clustersBefore = $instanceAdminClient->listClusters($instanceName)->getClusters();
71+
$clusters = $clustersBefore->getIterator();
72+
foreach ($clusters as $cluster) {
73+
print($cluster->getName() . PHP_EOL);
74+
}
75+
76+
$cluster = new Cluster();
77+
$cluster->setServeNodes($serve_nodes);
78+
$cluster->setDefaultStorageType($storage_type);
79+
$cluster->setLocation(
80+
$instanceAdminClient->locationName(
81+
$project_id,
82+
$location_id
83+
)
84+
);
85+
try {
86+
$instanceAdminClient->getCluster($clusterName);
87+
printf("Cluster %s already exists, aborting...", $cluster_id);
88+
} catch (ApiException $e) {
89+
if ($e->getStatus() === 'NOT_FOUND') {
90+
$operationResponse = $instanceAdminClient->createCluster($instanceName, $cluster_id, $cluster);
91+
92+
$operationResponse->pollUntilComplete();
93+
if ($operationResponse->operationSucceeded()) {
94+
$result = $operationResponse->getResult();
95+
printf("Cluster created: %s", $cluster_id);
96+
} else {
97+
$error = $operationResponse->getError();
98+
printf("Cluster not created: %s", $error);
99+
}
100+
} else {
101+
throw $e;
102+
}
103+
}
104+
// [END bigtable_create_cluster]
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
2+
3+
/**
4+
* Copyright 2018 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/api/README.md
23+
*/
24+
25+
// Include Google Cloud dependendencies 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 CLUSTER_ID [LOCATION_ID]" . PHP_EOL, __FILE__);
30+
}
31+
list($_, $project_id, $instance_id, $cluster_id) = $argv;
32+
$location_id = isset($argv[4]) ? $argv[4] : 'us-east1-b';
33+
34+
// [START bigtable_create_dev_instance]
35+
36+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
37+
use Google\Cloud\Bigtable\Admin\V2\Instance;
38+
use Google\Cloud\Bigtable\Admin\V2\Cluster;
39+
use Google\Cloud\Bigtable\Admin\V2\StorageType;
40+
use Google\Cloud\Bigtable\Admin\V2\Instance\Type as InstanceType;
41+
use Google\ApiCore\ApiException;
42+
43+
/** Uncomment and populate these variables in your code */
44+
// $project_id = 'The Google project ID';
45+
// $instance_id = 'The Bigtable instance ID';
46+
// $cluster_id = 'The Bigtable cluster ID';
47+
// $location_id = 'The Bigtable region ID';
48+
49+
50+
$instanceAdminClient = new BigtableInstanceAdminClient();
51+
52+
$projectName = $instanceAdminClient->projectName($project_id);
53+
$instanceName = $instanceAdminClient->instanceName($project_id, $instance_id);
54+
55+
56+
printf("Creating a DEVELOPMENT Instance" . PHP_EOL);
57+
// Set options to create an Instance
58+
59+
$storage_type = StorageType::HDD;
60+
$development = InstanceType::DEVELOPMENT;
61+
$labels = ['dev-label' => 'dev-label'];
62+
63+
64+
# Create instance with given options
65+
$instance = new Instance();
66+
$instance->setDisplayName($instance_id);
67+
$instance->setLabels($labels);
68+
$instance->setType($development);
69+
70+
// Create cluster with given options
71+
$cluster = new Cluster();
72+
$cluster->setDefaultStorageType($storage_type);
73+
$cluster->setLocation(
74+
$instanceAdminClient->locationName(
75+
$project_id,
76+
$location_id
77+
)
78+
);
79+
$clusters = [
80+
$cluster_id => $cluster
81+
];
82+
// Create development instance with given options
83+
try {
84+
$instanceAdminClient->getInstance($instanceName);
85+
printf("Instance %s already exists." . PHP_EOL, $instance_id);
86+
} catch (ApiException $e) {
87+
if ($e->getStatus() === 'NOT_FOUND') {
88+
printf("Creating a development Instance: %s" . PHP_EOL, $instance_id);
89+
$operationResponse = $instanceAdminClient->createInstance(
90+
$projectName,
91+
$instance_id,
92+
$instance,
93+
$clusters
94+
);
95+
$operationResponse->pollUntilComplete();
96+
if (!$operationResponse->operationSucceeded()) {
97+
$error = $operationResponse->getError();
98+
throw $error;
99+
} else {
100+
printf("Instance %s created.", $instance_id);
101+
}
102+
} else {
103+
throw $e;
104+
}
105+
}
106+
// [END bigtable_create_dev_instance]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
3+
/**
4+
* Copyright 2018 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/api/README.md
23+
*/
24+
25+
// Include Google Cloud dependendencies using Composer
26+
require_once __DIR__ . '/../vendor/autoload.php';
27+
28+
if (count($argv) != 4) {
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_create_family_gc_intersection]
34+
use Google\Cloud\Bigtable\Admin\V2\GcRule\Intersection as GcRuleIntersection;
35+
use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest\Modification;
36+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
37+
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
38+
use Google\Cloud\Bigtable\Admin\V2\ColumnFamily;
39+
use Google\Cloud\Bigtable\Admin\V2\GcRule;
40+
use Google\Protobuf\Duration;
41+
42+
43+
/** Uncomment and populate these variables in your code */
44+
// $project_id = 'The Google project ID';
45+
// $instance_id = 'The Bigtable instance ID';
46+
// $table_id = 'The Bigtable table ID';
47+
48+
$tableAdminClient = new BigtableTableAdminClient();
49+
50+
$tableName = $tableAdminClient->tableName($project_id, $instance_id, $table_id);
51+
52+
print('Creating column family cf4 with Intersection GC rule...' . PHP_EOL);
53+
$columnFamily4 = new ColumnFamily();
54+
55+
$intersection_rule = new GcRuleIntersection();
56+
$intersection_array = [
57+
(new GcRule)->setMaxAge((new Duration())->setSeconds(3600 * 24 * 5)),
58+
(new GcRule)->setMaxNumVersions(2)
59+
];
60+
$intersection_rule->setRules($intersection_array);
61+
62+
$intersection = new GcRule();
63+
$intersection->setIntersection($intersection_rule);
64+
65+
$columnFamily4->setGCRule($intersection);
66+
67+
$columnModification = new Modification();
68+
$columnModification->setId('cf4');
69+
$columnModification->setCreate($columnFamily4);
70+
$tableAdminClient->modifyColumnFamilies($tableName, [$columnModification]);
71+
72+
print('Created column family cf4 with Union GC rule' . PHP_EOL);
73+
74+
// [END bigtable_create_family_gc_intersection]

0 commit comments

Comments
 (0)