/
hello_world.php
159 lines (140 loc) · 5.48 KB
/
hello_world.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* Copyright 2019 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/main/bigtable/README.md
*/
// Include Google Cloud dependencies 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($_, $projectId, $instanceId, $tableId) = $argv;
// [START bigtable_hw_imports]
use Google\ApiCore\ApiException;
use Google\Cloud\Bigtable\Admin\V2\Client\BigtableInstanceAdminClient;
use Google\Cloud\Bigtable\Admin\V2\Client\BigtableTableAdminClient;
use Google\Cloud\Bigtable\Admin\V2\ColumnFamily;
use Google\Cloud\Bigtable\Admin\V2\CreateTableRequest;
use Google\Cloud\Bigtable\Admin\V2\DeleteTableRequest;
use Google\Cloud\Bigtable\Admin\V2\GetTableRequest;
use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest;
use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest\Modification;
use Google\Cloud\Bigtable\Admin\V2\Table;
use Google\Cloud\Bigtable\Admin\V2\Table\View;
use Google\Cloud\Bigtable\BigtableClient;
use Google\Cloud\Bigtable\Mutations;
use Google\Cloud\Bigtable\V2\RowFilter;
// [END bigtable_hw_imports]
// [START bigtable_hw_connect]
/** Uncomment and populate these variables in your code */
// $projectId = 'The Google project ID';
// $instanceId = 'The Bigtable instance ID';
// $tableId = 'The Bigtable table ID';
$instanceAdminClient = new BigtableInstanceAdminClient();
$tableAdminClient = new BigtableTableAdminClient();
$dataClient = new BigtableClient([
'projectId' => $projectId,
]);
// [END bigtable_hw_connect]
// [START bigtable_hw_create_table]
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
$tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);
// Check whether table exists in an instance.
// Create table if it does not exists.
$table = new Table();
printf('Creating a Table: %s' . PHP_EOL, $tableId);
try {
$getTableRequest = (new GetTableRequest())
->setName($tableName)
->setView(View::NAME_ONLY);
$tableAdminClient->getTable($getTableRequest);
printf('Table %s already exists' . PHP_EOL, $tableId);
} catch (ApiException $e) {
if ($e->getStatus() === 'NOT_FOUND') {
printf('Creating the %s table' . PHP_EOL, $tableId);
$createTableRequest = (new CreateTableRequest())
->setParent($instanceName)
->setTableId($tableId)
->setTable($table);
$tableAdminClient->createtable($createTableRequest);
$columnFamily = new ColumnFamily();
$columnModification = new Modification();
$columnModification->setId('cf1');
$columnModification->setCreate($columnFamily);
$modifyColumnFamiliesRequest = (new ModifyColumnFamiliesRequest())
->setName($tableName)
->setModifications([$columnModification]);
$tableAdminClient->modifyColumnFamilies($modifyColumnFamiliesRequest);
printf('Created table %s' . PHP_EOL, $tableId);
} else {
throw $e;
}
}
// [END bigtable_hw_create_table]
// [START bigtable_hw_write_rows]
$table = $dataClient->table($instanceId, $tableId);
printf('Writing some greetings to the table.' . PHP_EOL);
$greetings = ['Hello World!', 'Hello Cloud Bigtable!', 'Hello PHP!'];
$entries = [];
$columnFamilyId = 'cf1';
$column = 'greeting';
foreach ($greetings as $i => $value) {
$rowKey = sprintf('greeting%s', $i);
$rowMutation = new Mutations();
$rowMutation->upsert($columnFamilyId, $column, $value, time() * 1000 * 1000);
$entries[$rowKey] = $rowMutation;
}
$table->mutateRows($entries);
// [END bigtable_hw_write_rows]
// [START bigtable_hw_get_with_filter]
printf('Getting a single greeting by row key.' . PHP_EOL);
$key = 'greeting0';
// Only retrieve the most recent version of the cell.
$rowFilter = (new RowFilter())->setCellsPerColumnLimitFilter(1);
$column = 'greeting';
$columnFamilyId = 'cf1';
$row = $table->readRow($key, [
'rowFilter' => $rowFilter
]);
printf('%s' . PHP_EOL, $row[$columnFamilyId][$column][0]['value']);
// [END bigtable_hw_get_with_filter]
// [START bigtable_hw_scan_all]
$columnFamilyId = 'cf1';
$column = 'greeting';
printf('Scanning for all greetings:' . PHP_EOL);
$partialRows = $table->readRows([])->readAll();
foreach ($partialRows as $row) {
printf('%s' . PHP_EOL, $row[$columnFamilyId][$column][0]['value']);
}
// [END bigtable_hw_scan_all]
// [START bigtable_hw_delete_table]
try {
printf('Attempting to delete table %s.' . PHP_EOL, $tableId);
$deleteTableRequest = (new DeleteTableRequest())
->setName($tableName);
$tableAdminClient->deleteTable($deleteTableRequest);
printf('Deleted %s table.' . PHP_EOL, $tableId);
} catch (ApiException $e) {
if ($e->getStatus() === 'NOT_FOUND') {
printf('Table %s does not exists' . PHP_EOL, $tableId);
} else {
throw $e;
}
}
// [END bigtable_hw_delete_table]