|
23 | 23 | */
|
24 | 24 |
|
25 | 25 | // Include Google Cloud dependencies using Composer
|
26 |
| -require_once __DIR__ . '/../../vendor/autoload.php'; |
| 26 | +require_once __DIR__ . '/../vendor/autoload.php'; |
27 | 27 |
|
28 | 28 | if (count($argv) != 4) {
|
29 | 29 | return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID" . PHP_EOL, __FILE__);
|
30 | 30 | }
|
31 | 31 | list($_, $project_id, $instance_id, $table_id) = $argv;
|
32 | 32 |
|
33 |
| -// [START bigtable_hw_create_table] |
34 |
| - |
| 33 | +// [START bigtable_hw_imports] |
| 34 | +use Google\ApiCore\ApiException; |
35 | 35 | use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
|
36 | 36 | use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
|
37 | 37 | use Google\Cloud\Bigtable\Admin\V2\ColumnFamily;
|
38 | 38 | use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest\Modification;
|
39 |
| -use Google\Cloud\Bigtable\Admin\V2\Table\View; |
40 | 39 | 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; |
42 | 44 |
|
| 45 | +// [END bigtable_hw_imports] |
| 46 | + |
| 47 | +// [START bigtable_hw_connect] |
43 | 48 | /** Uncomment and populate these variables in your code */
|
44 | 49 | // $project_id = 'The Google project ID';
|
45 | 50 | // $instance_id = 'The Bigtable instance ID';
|
46 | 51 | // $table_id = 'The Bigtable table ID';
|
47 | 52 |
|
48 | 53 | $instanceAdminClient = new BigtableInstanceAdminClient();
|
49 | 54 | $tableAdminClient = new BigtableTableAdminClient();
|
| 55 | +$dataClient = new BigtableClient([ |
| 56 | + 'projectId' => $project_id, |
| 57 | +]); |
| 58 | +// [END bigtable_hw_connect] |
50 | 59 |
|
| 60 | +// [START bigtable_hw_create_table] |
51 | 61 | $instanceName = $instanceAdminClient->instanceName($project_id, $instance_id);
|
52 | 62 | $tableName = $tableAdminClient->tableName($project_id, $instance_id, $table_id);
|
53 | 63 |
|
54 | 64 | // Check whether table exists in an instance.
|
55 | 65 | // Create table if it does not exists.
|
56 | 66 | $table = new Table();
|
57 |
| -printf('Creating a Table : %s' . PHP_EOL, $table_id); |
| 67 | +printf('Creating a Table: %s' . PHP_EOL, $table_id); |
58 | 68 |
|
59 | 69 | try {
|
60 | 70 | $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); |
62 | 72 | } catch (ApiException $e) {
|
63 | 73 | if ($e->getStatus() === 'NOT_FOUND') {
|
64 | 74 | printf('Creating the %s table' . PHP_EOL, $table_id);
|
|
79 | 89 | }
|
80 | 90 | }
|
81 | 91 | // [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] |
0 commit comments