Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
dbb8f07
BigTable Admin Samples
Oct 26, 2018
4f63e67
Set some changes to the Google Bigtable Admin Samples
Oct 30, 2018
ee0faf2
Setting Format of some print functions
Oct 30, 2018
d8e963e
Divided the InstanceAdmin in Snippets Files
Oct 30, 2018
77f3a4a
InstanceAdmin Test completed
Oct 30, 2018
22beb1a
Divided TableAdmin in Snippets Files & TableAdmin Test completed
Oct 30, 2018
b285266
Changed the README to set the Snippets
Oct 30, 2018
aabfb4c
changed 'Instance' to 'instance'
Oct 30, 2018
a45b2d3
All samples on /bigtable/api/src
Nov 1, 2018
070a85d
Argv in Bigtable samples
Nov 1, 2018
adaa040
Move the START tag before the use statements.
Nov 5, 2018
c276da1
Split the Table Admin file and create all the test of the snippet files
Nov 5, 2018
7794250
Tested all the snippets
Nov 6, 2018
924f96e
Admin format
Nov 6, 2018
866dc61
Deleting unused variables
Nov 7, 2018
0a0299d
Removed the comment in clear_instance
Nov 12, 2018
21f92c1
Resolved Review
Nov 19, 2018
8914702
Formatting & Conflict Resolve
Nov 20, 2018
4e5af77
Delete Cluster & Delete Family Conflicts
Nov 20, 2018
c6201e8
Creating a Test Sample
Nov 20, 2018
8f12807
Create Cluster
Nov 20, 2018
18a0ecb
Created Test Snippets
Nov 20, 2018
5a70628
Throwing an exception when the instances is already exists.
Nov 21, 2018
6b49ed7
Delete Instance and Delete Table
Nov 21, 2018
3f75614
Delete Table
Nov 21, 2018
1017060
Bigtable Update Gc Rule
Nov 21, 2018
3b99938
UpdateGcRule - CheckRule Function
Nov 21, 2018
9ca0797
a
Nov 21, 2018
5ef8f5f
create a function for the try/catch
Nov 21, 2018
9fa6d12
Check in Delete Instance and Delete Table
Nov 21, 2018
6372e28
Generate Dynamic Instance Name
Nov 28, 2018
c279f9e
Generate Dynamic Instance Name
Nov 28, 2018
0033754
Deleted the Test File
Nov 28, 2018
19399ed
Create Cluster Try Catch Throw other Status.
Nov 28, 2018
83e0200
Delete all the other test
raelcorrales Nov 29, 2018
5b6b6cd
Deleting the iterator_to_array
Nov 29, 2018
3f11972
Update bigtable to 0.9.1 in composer and set the setUpBeforeClass
Nov 29, 2018
5a88736
Changed setupUpBeforeClass to static
Nov 29, 2018
c666340
List errors
Nov 30, 2018
8e70951
List error changed
Nov 30, 2018
4108d26
Getting the Key File
Nov 30, 2018
7cfe24c
Setting the ProjectID variable
Nov 30, 2018
81ce228
Test Listing
Nov 30, 2018
9c51b31
Checking the List Instance Cluster
Nov 30, 2018
cbe478d
Checking the Segmentation Fault in the GetCluster()->getIterator()
Nov 30, 2018
eee2b72
Segmentation Fault GetInstance
Nov 30, 2018
28450f5
Formatted Code Test
Nov 30, 2018
b73a2cd
Changed the code to get the Instance and Cluster List, Issue: SegFault
Dec 3, 2018
ae4d904
Running test in docker
Dec 3, 2018
6a0609b
Test runs successfully
Dec 4, 2018
e5d09cc
Test phpunit.xml.dist ConvertErrors and PHP 5.6 return types
Dec 4, 2018
0a4ba3d
Adding Bigtable to Run Test Suite
Dec 4, 2018
1313502
TestTrait added
Dec 4, 2018
ae3a80b
Merge branch 'bigtable_admin' of https://github.com/juan-rael/php-doc…
bshaffer Dec 4, 2018
e33c07e
projectId in line 77
Dec 4, 2018
1cc1dca
projectId in line 47 and 59
Dec 4, 2018
c16bd2a
Added Exponential Backoff Trait
Dec 4, 2018
656bfee
Added try/catch to delete_instance
Dec 4, 2018
d11ea80
Merge branch 'bigtable_admin' of https://github.com/juan-rael/php-doc…
bshaffer Dec 4, 2018
ea45f7f
Adding TestTrait again
Dec 4, 2018
fe17217
Changed runSnippet to static
Dec 4, 2018
6679d99
Removed the tearDownAfterClass method
Dec 5, 2018
e0782c3
Changed Test structure, to use less instances
Dec 5, 2018
b086efc
Added Delete Test
Dec 5, 2018
2dd2c73
Merge branch 'bigtable_admin' of https://github.com/juan-rael/php-doc…
bshaffer Dec 5, 2018
ea030c8
Resolve errors of typing
Dec 5, 2018
a02432f
create_cluster test setting other region cluster
Dec 5, 2018
9091e2a
create_cluster test setting other region cluster
Dec 5, 2018
ba63e00
Merge branch 'bigtable_admin' of https://github.com/juan-rael/php-doc…
bshaffer Dec 5, 2018
6a8f728
fixes bigtable tests
bshaffer Dec 5, 2018
2a74961
clean up for code standards
bshaffer Dec 5, 2018
3984f2e
ensures deleteTable test actually works
bshaffer Dec 5, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions bigtable/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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)
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
select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
to the path of the JSON key that was downloaded.
3. **Clone the repo** and cd into this directory
```sh
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
$ cd php-docs-samples/bigtable/api
```

4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
Run `php composer.phar install` (if composer is installed locally) or `composer install`
(if composer is installed globally).
5. Run `php SNIPPET_NAME.php`. The usage will print for each if no arguments
are provided:
```sh
$ php src/run_instance_operations.php
Usage: php src/run_instance_operations.php PROJECT_ID INSTANCE_ID TABLE_ID

$ php src/run_instance_operations.php your-project-id your-instance-id your-table-id
```

## Contributing changes

* See [CONTRIBUTING.md](../../CONTRIBUTING.md)

## Licensing

* See [LICENSE](../../LICENSE)
9 changes: 9 additions & 0 deletions bigtable/api/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"require": {
"google/cloud-bigtable": "^0.9.1"
},
"require-dev": {
"google/cloud-tools": "^0.8",
"phpunit/phpunit": "^5.0"
}
}
671 changes: 671 additions & 0 deletions bigtable/api/file.txt

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions bigtable/api/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2018 Google LLC.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<phpunit backupGlobals="false"
strict="true"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
timeoutForSmallTests="10"
timeoutForMediumTests="30"
timeoutForLargeTests="120">
<testsuites>
<testsuite name="PHP bigtable Instance test">
<file>./test/bigtableTest.php</file>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="./build/logs/clover.xml"/>
</logging>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
104 changes: 104 additions & 0 deletions bigtable/api/src/create_cluster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/**
* Copyright 2018 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/api/README.md
*/

// Include Google Cloud dependendencies using Composer
require_once __DIR__ . '/../vendor/autoload.php';

if (count($argv) < 3 || count($argv) > 5) {
return printf("Usage: php %s PROJECT_ID INSTANCE_ID CLUSTER_ID [LOCATION_ID]" . PHP_EOL, __FILE__);
}
list($_, $project_id, $instance_id, $cluster_id) = $argv;
$location_id = isset($argv[4]) ? $argv[4] : 'us-east1-b';

// [START bigtable_create_cluster]

use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
use Google\Cloud\Bigtable\Admin\V2\Cluster;
use Google\Cloud\Bigtable\Admin\V2\StorageType;
use Google\ApiCore\ApiException;


/** Uncomment and populate these variables in your code */
// $project_id = 'The Google project ID';
// $instance_id = 'The Bigtable instance ID';
// $cluster_id = 'The Bigtable cluster ID';
// $location_id = 'The Bigtable region ID';


$instanceAdminClient = new BigtableInstanceAdminClient();

$instanceName = $instanceAdminClient->instanceName($project_id, $instance_id);
$clusterName = $instanceAdminClient->clusterName($project_id, $instance_id, $cluster_id);

printf("Adding Cluster to Instance %s" . PHP_EOL, $instance_id);
try {
$instanceAdminClient->getInstance($instanceName);
} catch (ApiException $e) {
if ($e->getStatus() === 'NOT_FOUND') {
printf("Instance %s does not exists." . PHP_EOL, $instance_id);
return;
} else {
throw $e;
}
}
printf("Listing Clusters:" . PHP_EOL);

$storage_type = StorageType::SSD;
$serve_nodes = 3;

$clustersBefore = $instanceAdminClient->listClusters($instanceName)->getClusters();
$clusters = $clustersBefore->getIterator();
foreach ($clusters as $cluster) {
print($cluster->getName() . PHP_EOL);
}

$cluster = new Cluster();
$cluster->setServeNodes($serve_nodes);
$cluster->setDefaultStorageType($storage_type);
$cluster->setLocation(
$instanceAdminClient->locationName(
$project_id,
$location_id
)
);
try {
$instanceAdminClient->getCluster($clusterName);
printf("Cluster %s already exists, aborting...", $cluster_id);
} catch (ApiException $e) {
if ($e->getStatus() === 'NOT_FOUND') {
$operationResponse = $instanceAdminClient->createCluster($instanceName, $cluster_id, $cluster);

$operationResponse->pollUntilComplete();
if ($operationResponse->operationSucceeded()) {
$result = $operationResponse->getResult();
printf("Cluster created: %s", $cluster_id);
} else {
$error = $operationResponse->getError();
printf("Cluster not created: %s", $error);
}
} else {
throw $e;
}
}
// [END bigtable_create_cluster]
106 changes: 106 additions & 0 deletions bigtable/api/src/create_dev_instance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* Copyright 2018 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/api/README.md
*/

// Include Google Cloud dependendencies using Composer
require_once __DIR__ . '/../vendor/autoload.php';

if (count($argv) < 3 || count($argv) > 5) {
return printf("Usage: php %s PROJECT_ID INSTANCE_ID CLUSTER_ID [LOCATION_ID]" . PHP_EOL, __FILE__);
}
list($_, $project_id, $instance_id, $cluster_id) = $argv;
$location_id = isset($argv[4]) ? $argv[4] : 'us-east1-b';

// [START bigtable_create_dev_instance]

use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
use Google\Cloud\Bigtable\Admin\V2\Instance;
use Google\Cloud\Bigtable\Admin\V2\Cluster;
use Google\Cloud\Bigtable\Admin\V2\StorageType;
use Google\Cloud\Bigtable\Admin\V2\Instance\Type as InstanceType;
use Google\ApiCore\ApiException;

/** Uncomment and populate these variables in your code */
// $project_id = 'The Google project ID';
// $instance_id = 'The Bigtable instance ID';
// $cluster_id = 'The Bigtable cluster ID';
// $location_id = 'The Bigtable region ID';


$instanceAdminClient = new BigtableInstanceAdminClient();

$projectName = $instanceAdminClient->projectName($project_id);
$instanceName = $instanceAdminClient->instanceName($project_id, $instance_id);


printf("Creating a DEVELOPMENT Instance" . PHP_EOL);
// Set options to create an Instance

$storage_type = StorageType::HDD;
$development = InstanceType::DEVELOPMENT;
$labels = ['dev-label' => 'dev-label'];


# Create instance with given options
$instance = new Instance();
$instance->setDisplayName($instance_id);
$instance->setLabels($labels);
$instance->setType($development);

// Create cluster with given options
$cluster = new Cluster();
$cluster->setDefaultStorageType($storage_type);
$cluster->setLocation(
$instanceAdminClient->locationName(
$project_id,
$location_id
)
);
$clusters = [
$cluster_id => $cluster
];
// Create development instance with given options
try {
$instanceAdminClient->getInstance($instanceName);
printf("Instance %s already exists." . PHP_EOL, $instance_id);
} catch (ApiException $e) {
if ($e->getStatus() === 'NOT_FOUND') {
printf("Creating a development Instance: %s" . PHP_EOL, $instance_id);
$operationResponse = $instanceAdminClient->createInstance(
$projectName,
$instance_id,
$instance,
$clusters
);
$operationResponse->pollUntilComplete();
if (!$operationResponse->operationSucceeded()) {
$error = $operationResponse->getError();
throw $error;
} else {
printf("Instance %s created.", $instance_id);
}
} else {
throw $e;
}
}
// [END bigtable_create_dev_instance]
74 changes: 74 additions & 0 deletions bigtable/api/src/create_family_gc_intersection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* Copyright 2018 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/api/README.md
*/

// Include Google Cloud dependendencies using Composer
require_once __DIR__ . '/../vendor/autoload.php';

if (count($argv) != 4) {
return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID" . PHP_EOL, __FILE__);
}
list($_, $project_id, $instance_id, $table_id) = $argv;

// [START bigtable_create_family_gc_intersection]
use Google\Cloud\Bigtable\Admin\V2\GcRule\Intersection as GcRuleIntersection;
use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest\Modification;
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
use Google\Cloud\Bigtable\Admin\V2\ColumnFamily;
use Google\Cloud\Bigtable\Admin\V2\GcRule;
use Google\Protobuf\Duration;


/** Uncomment and populate these variables in your code */
// $project_id = 'The Google project ID';
// $instance_id = 'The Bigtable instance ID';
// $table_id = 'The Bigtable table ID';

$tableAdminClient = new BigtableTableAdminClient();

$tableName = $tableAdminClient->tableName($project_id, $instance_id, $table_id);

print('Creating column family cf4 with Intersection GC rule...' . PHP_EOL);
$columnFamily4 = new ColumnFamily();

$intersection_rule = new GcRuleIntersection();
$intersection_array = [
(new GcRule)->setMaxAge((new Duration())->setSeconds(3600 * 24 * 5)),
(new GcRule)->setMaxNumVersions(2)
];
$intersection_rule->setRules($intersection_array);

$intersection = new GcRule();
$intersection->setIntersection($intersection_rule);

$columnFamily4->setGCRule($intersection);

$columnModification = new Modification();
$columnModification->setId('cf4');
$columnModification->setCreate($columnFamily4);
$tableAdminClient->modifyColumnFamilies($tableName, [$columnModification]);

print('Created column family cf4 with Union GC rule' . PHP_EOL);

// [END bigtable_create_family_gc_intersection]
Loading