Skip to content

Commit 697d09e

Browse files
committed
Fixed tests
Add create table hello world which also adds a column family
1 parent ba2f901 commit 697d09e

File tree

4 files changed

+111
-13
lines changed

4 files changed

+111
-13
lines changed

bigtable/api/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-bigtable": "^0.9.1"
3+
"google/cloud-bigtable": "^0.10.1"
44
},
55
"require-dev": {
66
"google/cloud-tools": "^0.8",

bigtable/api/src/hw_create_table.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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_hw_create_table]
34+
35+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
36+
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
37+
use Google\Cloud\Bigtable\Admin\V2\ColumnFamily;
38+
use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest\Modification;
39+
use Google\Cloud\Bigtable\Admin\V2\Table\View;
40+
use Google\Cloud\Bigtable\Admin\V2\Table;
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+
// $table_id = 'The Bigtable table ID';
47+
48+
$instanceAdminClient = new BigtableInstanceAdminClient();
49+
$tableAdminClient = new BigtableTableAdminClient();
50+
51+
$instanceName = $instanceAdminClient->instanceName($project_id, $instance_id);
52+
$tableName = $tableAdminClient->tableName($project_id, $instance_id, $table_id);
53+
54+
// Check whether table exists in an instance.
55+
// Create table if it does not exists.
56+
$table = new Table();
57+
printf('Creating a Table : %s' . PHP_EOL, $table_id);
58+
59+
try {
60+
$tableAdminClient->getTable($tableName, ['view' => View::NAME_ONLY]);
61+
printf('Table %s alredy exists' . PHP_EOL, $table_id);
62+
} catch (ApiException $e) {
63+
if ($e->getStatus() === 'NOT_FOUND') {
64+
printf('Creating the %s table' . PHP_EOL, $table_id);
65+
66+
$tableAdminClient->createtable(
67+
$instanceName,
68+
$table_id,
69+
$table
70+
);
71+
$columnFamily = new ColumnFamily();
72+
$columnModification = new Modification();
73+
$columnModification->setId('cf1');
74+
$columnModification->setCreate($columnFamily);
75+
$tableAdminClient->modifyColumnFamilies($tableName, [$columnModification]);
76+
printf('Created table %s' . PHP_EOL, $table_id);
77+
} else {
78+
throw $e;
79+
}
80+
}
81+
// [END bigtable_hw_create_table]

bigtable/api/src/hw_write_rows.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
printf('Writing some greetings to the table.' . PHP_EOL);
5858
$greetings = ['Hello World!', 'Hello Cloud Bigtable!', 'Hello PHP!'];
5959
$entries = [];
60-
$column = 'greeting';
6160
$columnFamilyId = 'cf1';
61+
$column = 'greeting';
6262
foreach ($greetings as $i => $value) {
6363
$row_key = sprintf('greeting%s', $i);
6464
$rowMutation = new Mutations();

bigtable/api/test/bigtableTest.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
2+
23
namespace Google\Cloud\Samples\BigTable\Tests;
34

5+
use Google\Cloud\Bigtable\BigtableClient;
46
use PHPUnit\Framework\TestCase;
57

68
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
@@ -12,13 +14,14 @@
1214

1315
final class BigTableTest extends TestCase
1416
{
15-
use TestTrait,ExponentialBackoffTrait;
17+
use TestTrait, ExponentialBackoffTrait;
1618

1719
const INSTANCE_ID_PREFIX = 'php-instance-';
1820
const CLUSTER_ID_PREFIX = 'php-cluster-';
1921
const TABLE_ID_PREFIX = 'php-table-';
2022
private static $instanceAdminClient;
2123
private static $tableAdminClient;
24+
private static $bigtableClient;
2225
private static $instanceId;
2326
private static $clusterId;
2427

@@ -27,11 +30,14 @@ public static function setUpBeforeClass()
2730
self::checkProjectEnvVarBeforeClass();
2831
self::$instanceAdminClient = new BigtableInstanceAdminClient();
2932
self::$tableAdminClient = new BigtableTableAdminClient();
33+
self::$bigtableClient = new BigtableClient([
34+
'projectId' => self::$projectId,
35+
]);
3036

3137
self::$instanceId = uniqid(self::INSTANCE_ID_PREFIX);
3238
self::$clusterId = uniqid(self::CLUSTER_ID_PREFIX);
3339

34-
self::create_production_instance(self::$projectId,self::$instanceId,self::$clusterId);
40+
self::create_production_instance(self::$projectId, self::$instanceId, self::$clusterId);
3541
}
3642

3743
public function setUp()
@@ -368,16 +374,18 @@ public function testWritingRows()
368374
$tableId = uniqid(self::TABLE_ID_PREFIX);
369375
$tableName = self::$tableAdminClient->tableName(self::$projectId, self::$instanceId, $tableId);
370376

371-
$this->createTable(self::$projectId, self::$instanceId, self::$clusterId, $tableId);
377+
$this->createHWTable(self::$projectId, self::$instanceId, self::$clusterId, $tableId);
372378
$this->checkTable($tableName);
373379

374-
$content = self::runSnippet('writing_rows', [
380+
$content = self::runSnippet('hw_write_rows', [
375381
self::$projectId,
376382
self::$instanceId,
377383
$tableId
378384
]);
379385

380-
$table = self::$tableAdminClient->table(self::$instanceId, self::$tableId);
386+
$table = self::$bigtableClient->table(self::$instanceId, $tableId);
387+
$columnFamilyId = 'cf1';
388+
$column = 'greeting';
381389

382390
$partial_rows = $table->readRows([])->readAll();
383391
$array = [];
@@ -395,16 +403,16 @@ public function testGettingARow()
395403
$tableId = uniqid(self::TABLE_ID_PREFIX);
396404
$tableName = self::$tableAdminClient->tableName(self::$projectId, self::$instanceId, $tableId);
397405

398-
$this->createTable(self::$projectId, self::$instanceId, self::$clusterId, $tableId);
406+
$this->createHWTable(self::$projectId, self::$instanceId, self::$clusterId, $tableId);
399407
$this->checkTable($tableName);
400408

401-
self::runSnippet('writing_rows', [
409+
self::runSnippet('hw_write_rows', [
402410
self::$projectId,
403411
self::$instanceId,
404412
$tableId
405413
]);
406414

407-
$content = self::runSnippet('getting_a_row', [
415+
$content = self::runSnippet('hw_get_with_filter', [
408416
self::$projectId,
409417
self::$instanceId,
410418
$tableId
@@ -421,16 +429,16 @@ public function testScanningAllRows()
421429
$tableId = uniqid(self::TABLE_ID_PREFIX);
422430
$tableName = self::$tableAdminClient->tableName(self::$projectId, self::$instanceId, $tableId);
423431

424-
$this->createTable(self::$projectId, self::$instanceId, self::$clusterId, $tableId);
432+
$this->createHWTable(self::$projectId, self::$instanceId, self::$clusterId, $tableId);
425433
$this->checkTable($tableName);
426434

427-
self::runSnippet('writing_rows', [
435+
self::runSnippet('hw_write_rows', [
428436
self::$projectId,
429437
self::$instanceId,
430438
$tableId
431439
]);
432440

433-
$content = self::runSnippet('scanning_all_rows', [
441+
$content = self::runSnippet('hw_scan_all', [
434442
self::$projectId,
435443
self::$instanceId,
436444
$tableId
@@ -548,6 +556,15 @@ private function createTable($projectId, $instanceId, $clusterId, $tableId)
548556
]);
549557
}
550558

559+
private function createHWTable($projectId, $instanceId, $clusterId, $tableId)
560+
{
561+
self::runSnippet('hw_create_table', [
562+
$projectId,
563+
$instanceId,
564+
$tableId
565+
]);
566+
}
567+
551568
private function cleanInstance($projectId, $instanceId)
552569
{
553570
$content = self::runSnippet('delete_instance', [

0 commit comments

Comments
 (0)