Skip to content

Commit 241a197

Browse files
Merge pull request GoogleCloudPlatform#964 from GoogleCloudPlatform/hello-world
Putting Hello World into one file for more clarity on docs page.
2 parents 19d15fe + 811b57f commit 241a197

File tree

7 files changed

+81
-266
lines changed

7 files changed

+81
-266
lines changed

bigtable/src/create_production_instance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
);
8686
$operationResponse->pollUntilComplete();
8787
if (!$operationResponse->operationSucceeded()) {
88-
print('Error: ' . $operationResponse->getError());
88+
print('Error: ' . $operationResponse->getError()->getMessage());
8989
} else {
9090
printf("Instance %s created.", $instance_id);
9191
}

bigtable/src/create_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
try {
5858
$tableAdminClient->getTable($tableName, ['view' => View::NAME_ONLY]);
59-
printf('Table %s alredy exists' . PHP_EOL, $table_id);
59+
printf('Table %s already exists' . PHP_EOL, $table_id);
6060
} catch (ApiException $e) {
6161
if ($e->getStatus() === 'NOT_FOUND') {
6262
printf('Creating the %s table' . PHP_EOL, $table_id);

bigtable/src/helloworld/create_table.php renamed to bigtable/src/hello_world.php

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,52 @@
2323
*/
2424

2525
// Include Google Cloud dependencies using Composer
26-
require_once __DIR__ . '/../../vendor/autoload.php';
26+
require_once __DIR__ . '/../vendor/autoload.php';
2727

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

33-
// [START bigtable_hw_create_table]
34-
33+
// [START bigtable_hw_imports]
34+
use Google\ApiCore\ApiException;
3535
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
3636
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
3737
use Google\Cloud\Bigtable\Admin\V2\ColumnFamily;
3838
use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest\Modification;
39-
use Google\Cloud\Bigtable\Admin\V2\Table\View;
4039
use Google\Cloud\Bigtable\Admin\V2\Table;
41-
use Google\ApiCore\ApiException;
40+
use Google\Cloud\Bigtable\Admin\V2\Table\View;
41+
use Google\Cloud\Bigtable\BigtableClient;
42+
use Google\Cloud\Bigtable\Mutations;
43+
use Google\Cloud\Bigtable\V2\RowFilter;
4244

45+
// [END bigtable_hw_imports]
46+
47+
// [START bigtable_hw_connect]
4348
/** Uncomment and populate these variables in your code */
4449
// $project_id = 'The Google project ID';
4550
// $instance_id = 'The Bigtable instance ID';
4651
// $table_id = 'The Bigtable table ID';
4752

4853
$instanceAdminClient = new BigtableInstanceAdminClient();
4954
$tableAdminClient = new BigtableTableAdminClient();
55+
$dataClient = new BigtableClient([
56+
'projectId' => $project_id,
57+
]);
58+
// [END bigtable_hw_connect]
5059

60+
// [START bigtable_hw_create_table]
5161
$instanceName = $instanceAdminClient->instanceName($project_id, $instance_id);
5262
$tableName = $tableAdminClient->tableName($project_id, $instance_id, $table_id);
5363

5464
// Check whether table exists in an instance.
5565
// Create table if it does not exists.
5666
$table = new Table();
57-
printf('Creating a Table : %s' . PHP_EOL, $table_id);
67+
printf('Creating a Table: %s' . PHP_EOL, $table_id);
5868

5969
try {
6070
$tableAdminClient->getTable($tableName, ['view' => View::NAME_ONLY]);
61-
printf('Table %s alredy exists' . PHP_EOL, $table_id);
71+
printf('Table %s already exists' . PHP_EOL, $table_id);
6272
} catch (ApiException $e) {
6373
if ($e->getStatus() === 'NOT_FOUND') {
6474
printf('Creating the %s table' . PHP_EOL, $table_id);
@@ -79,3 +89,59 @@
7989
}
8090
}
8191
// [END bigtable_hw_create_table]
92+
93+
// [START bigtable_hw_write_rows]
94+
$table = $dataClient->table($instance_id, $table_id);
95+
96+
printf('Writing some greetings to the table.' . PHP_EOL);
97+
$greetings = ['Hello World!', 'Hello Cloud Bigtable!', 'Hello PHP!'];
98+
$entries = [];
99+
$columnFamilyId = 'cf1';
100+
$column = 'greeting';
101+
foreach ($greetings as $i => $value) {
102+
$row_key = sprintf('greeting%s', $i);
103+
$rowMutation = new Mutations();
104+
$rowMutation->upsert($columnFamilyId, $column, $value, time() * 1000);
105+
$entries[$row_key] = $rowMutation;
106+
}
107+
$table->mutateRows($entries);
108+
// [END bigtable_hw_write_rows]
109+
110+
// [START bigtable_hw_get_with_filter]
111+
printf('Getting a single greeting by row key.' . PHP_EOL);
112+
$key = 'greeting0';
113+
// Only retrieve the most recent version of the cell.
114+
$row_filter = (new RowFilter)->setCellsPerColumnLimitFilter(1);
115+
116+
$column = 'greeting';
117+
$columnFamilyId = 'cf1';
118+
119+
$row = $table->readRow($key, [
120+
'rowFilter' => $row_filter
121+
]);
122+
printf('%s' . PHP_EOL, $row[$columnFamilyId][$column][0]['value']);
123+
// [END bigtable_hw_get_with_filter]
124+
125+
// [START bigtable_hw_scan_all]
126+
$columnFamilyId = 'cf1';
127+
$column = 'greeting';
128+
printf('Scanning for all greetings:' . PHP_EOL);
129+
$partial_rows = $table->readRows([])->readAll();
130+
foreach ($partial_rows as $row) {
131+
printf('%s' . PHP_EOL, $row[$columnFamilyId][$column][0]['value']);
132+
}
133+
// [END bigtable_hw_scan_all]
134+
135+
// [START bigtable_hw_delete_table]
136+
try {
137+
printf('Attempting to delete table %s.' . PHP_EOL, $table_id);
138+
$tableAdminClient->deleteTable($tableName);
139+
printf('Deleted %s table.' . PHP_EOL, $table_id);
140+
} catch (ApiException $e) {
141+
if ($e->getStatus() === 'NOT_FOUND') {
142+
printf('Table %s does not exists' . PHP_EOL, $table_id);
143+
} else {
144+
throw $e;
145+
}
146+
}
147+
// [END bigtable_hw_delete_table]

bigtable/src/helloworld/get_with_filter.php

Lines changed: 0 additions & 62 deletions
This file was deleted.

bigtable/src/helloworld/scan_all.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

bigtable/src/helloworld/write_rows.php

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)