|
| 1 | +
|
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2019 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/README.md |
| 23 | + */ |
| 24 | + |
| 25 | +// Include Google Cloud dependencies using Composer |
| 26 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 27 | + |
| 28 | +if (count($argv) !== 5) { |
| 29 | + return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID FILTER_TYPE" . PHP_EOL, __FILE__); |
| 30 | +} |
| 31 | +list($_, $project_id, $instance_id, $table_id, $filter_type) = $argv; |
| 32 | + |
| 33 | +$validFilterTypes = [ |
| 34 | + 'filter_limit_row_sample', |
| 35 | + 'filter_limit_row_regex', |
| 36 | + 'filter_limit_cells_per_col', |
| 37 | + 'filter_limit_cells_per_row', |
| 38 | + 'filter_limit_cells_per_row_offset', |
| 39 | + 'filter_limit_col_family_regex', |
| 40 | + 'filter_limit_col_qualifier_regex', |
| 41 | + 'filter_limit_col_range', |
| 42 | + 'filter_limit_value_range', |
| 43 | + 'filter_limit_value_regex', |
| 44 | + 'filter_limit_timestamp_range', |
| 45 | + 'filter_limit_block_all', |
| 46 | + 'filter_limit_pass_all', |
| 47 | + 'filter_modify_strip_value', |
| 48 | + 'filter_modify_apply_label', |
| 49 | + 'filter_composing_chain', |
| 50 | + 'filter_composing_interleave', |
| 51 | + 'filter_composing_condition' |
| 52 | +]; |
| 53 | +if (!in_array($filter_type, $validFilterTypes)) { |
| 54 | + throw new Exception(sprintf( |
| 55 | + 'Invalid FILTER_TYPE %s, must be one of: %s', |
| 56 | + $filter_type, |
| 57 | + implode(', ', $validFilterTypes) |
| 58 | + )); |
| 59 | +} |
| 60 | + |
| 61 | +// [START bigtable_filters_limit_row_sample] |
| 62 | +// [START bigtable_filters_limit_row_regex] |
| 63 | +// [START bigtable_filters_limit_cells_per_col] |
| 64 | +// [START bigtable_filters_limit_cells_per_row] |
| 65 | +// [START bigtable_filters_limit_cells_per_row_offset] |
| 66 | +// [START bigtable_filters_limit_col_family_regex] |
| 67 | +// [START bigtable_filters_limit_col_qualifier_regex] |
| 68 | +// [START bigtable_filters_limit_col_range] |
| 69 | +// [START bigtable_filters_limit_value_range] |
| 70 | +// [START bigtable_filters_limit_value_regex] |
| 71 | +// [START bigtable_filters_limit_timestamp_range] |
| 72 | +// [START bigtable_filters_limit_block_all] |
| 73 | +// [START bigtable_filters_limit_pass_all] |
| 74 | +// [START bigtable_filters_modify_strip_value] |
| 75 | +// [START bigtable_filters_modify_apply_label] |
| 76 | +// [START bigtable_filters_composing_chain] |
| 77 | +// [START bigtable_filters_composing_interleave] |
| 78 | +// [START bigtable_filters_composing_condition] |
| 79 | + |
| 80 | +use Google\Cloud\Bigtable\BigtableClient; |
| 81 | +use Google\Cloud\Bigtable\Filter; |
| 82 | + |
| 83 | +/** Uncomment and populate these variables in your code */ |
| 84 | +// $project_id = 'The Google project ID'; |
| 85 | +// $instance_id = 'The Bigtable instance ID'; |
| 86 | +// $table_id = 'mobile-time-series'; |
| 87 | + |
| 88 | +// Connect to an existing table with an existing instance. |
| 89 | +$dataClient = new BigtableClient([ |
| 90 | + 'projectId' => $project_id, |
| 91 | +]); |
| 92 | +$table = $dataClient->table($instance_id, $table_id); |
| 93 | + |
| 94 | +// Helper function for printing the row data |
| 95 | +function print_row($key, $row) |
| 96 | +{ |
| 97 | + printf('Reading data for row %s' . PHP_EOL, $key); |
| 98 | + foreach ((array)$row as $family => $cols) { |
| 99 | + printf('Column Family %s' . PHP_EOL, $family); |
| 100 | + foreach ($cols as $col => $data) { |
| 101 | + for ($i = 0; $i < count($data); $i++) { |
| 102 | + printf( |
| 103 | + "\t%s: %s @%s%s" . PHP_EOL, |
| 104 | + $col, |
| 105 | + $data[$i]['value'], |
| 106 | + $data[$i]['timeStamp'], |
| 107 | + $data[$i]['labels'] ? sprintf(" [%s]", $data[$i]['labels']) : '' |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + print(PHP_EOL); |
| 113 | +} |
| 114 | + |
| 115 | +function read_filter($table, $filter) |
| 116 | +{ |
| 117 | + $rows = $table->readRows([ |
| 118 | + 'filter' => $filter |
| 119 | + ]); |
| 120 | + |
| 121 | + foreach ($rows as $key => $row) { |
| 122 | + print_row($key, $row); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// [END bigtable_filters_limit_row_sample] |
| 127 | +// [END bigtable_filters_limit_row_regex] |
| 128 | +// [END bigtable_filters_limit_cells_per_col] |
| 129 | +// [END bigtable_filters_limit_cells_per_row] |
| 130 | +// [END bigtable_filters_limit_cells_per_row_offset] |
| 131 | +// [END bigtable_filters_limit_col_family_regex] |
| 132 | +// [END bigtable_filters_limit_col_qualifier_regex] |
| 133 | +// [END bigtable_filters_limit_col_range] |
| 134 | +// [END bigtable_filters_limit_value_range] |
| 135 | +// [END bigtable_filters_limit_value_regex] |
| 136 | +// [END bigtable_filters_limit_timestamp_range] |
| 137 | +// [END bigtable_filters_limit_block_all] |
| 138 | +// [END bigtable_filters_limit_pass_all] |
| 139 | +// [END bigtable_filters_modify_strip_value] |
| 140 | +// [END bigtable_filters_modify_apply_label] |
| 141 | +// [END bigtable_filters_composing_chain] |
| 142 | +// [END bigtable_filters_composing_interleave] |
| 143 | +// [END bigtable_filters_composing_condition] |
| 144 | + |
| 145 | + |
| 146 | +function filter_limit_row_sample($table) |
| 147 | +{ |
| 148 | + // [START bigtable_filters_limit_row_sample] |
| 149 | + $filter = Filter::key()->sample(.75); |
| 150 | + read_filter($table, $filter); |
| 151 | + // [END bigtable_filters_limit_row_sample] |
| 152 | +} |
| 153 | + |
| 154 | +function filter_limit_row_regex($table) |
| 155 | +{ |
| 156 | + // [START bigtable_filters_limit_row_regex] |
| 157 | + $filter = Filter::key()->regex(".*#20190501$"); |
| 158 | + read_filter($table, $filter); |
| 159 | + // [END bigtable_filters_limit_row_regex] |
| 160 | +} |
| 161 | + |
| 162 | +function filter_limit_cells_per_col($table) |
| 163 | +{ |
| 164 | + // [START bigtable_filters_limit_cells_per_col] |
| 165 | + $filter = Filter::limit()->cellsPerColumn(2); |
| 166 | + read_filter($table, $filter); |
| 167 | + // [END bigtable_filters_limit_cells_per_col] |
| 168 | +} |
| 169 | + |
| 170 | +function filter_limit_cells_per_row($table) |
| 171 | +{ |
| 172 | + // [START bigtable_filters_limit_cells_per_row] |
| 173 | + $filter = Filter::limit()->cellsPerRow(2); |
| 174 | + read_filter($table, $filter); |
| 175 | + // [END bigtable_filters_limit_cells_per_row] |
| 176 | +} |
| 177 | + |
| 178 | +function filter_limit_cells_per_row_offset($table) |
| 179 | +{ |
| 180 | + // [START bigtable_filters_limit_cells_per_row_offset] |
| 181 | + $filter = Filter::offset()->cellsPerRow(2); |
| 182 | + read_filter($table, $filter); |
| 183 | + // [END bigtable_filters_limit_cells_per_row_offset] |
| 184 | +} |
| 185 | + |
| 186 | +function filter_limit_col_family_regex($table) |
| 187 | +{ |
| 188 | + // [START bigtable_filters_limit_col_family_regex] |
| 189 | + $filter = Filter::family()->regex("stats_.*$"); |
| 190 | + read_filter($table, $filter); |
| 191 | + // [END bigtable_filters_limit_col_family_regex] |
| 192 | +} |
| 193 | + |
| 194 | +function filter_limit_col_qualifier_regex($table) |
| 195 | +{ |
| 196 | + // [START bigtable_filters_limit_col_qualifier_regex] |
| 197 | + $filter = Filter::qualifier()->regex("connected_.*$"); |
| 198 | + read_filter($table, $filter); |
| 199 | + // [END bigtable_filters_limit_col_qualifier_regex] |
| 200 | +} |
| 201 | + |
| 202 | +function filter_limit_col_range($table) |
| 203 | +{ |
| 204 | + // [START bigtable_filters_limit_col_range] |
| 205 | + $filter = Filter::qualifier() |
| 206 | + ->rangeWithinFamily("cell_plan") |
| 207 | + ->startClosed("data_plan_01gb") |
| 208 | + ->endOpen("data_plan_10gb"); |
| 209 | + read_filter($table, $filter); |
| 210 | + // [END bigtable_filters_limit_col_range] |
| 211 | +} |
| 212 | + |
| 213 | +function filter_limit_value_range($table) |
| 214 | +{ |
| 215 | + // [START bigtable_filters_limit_value_range] |
| 216 | + $filter = Filter::value() |
| 217 | + ->range() |
| 218 | + ->startClosed("PQ2A.190405") |
| 219 | + ->endOpen("PQ2A.190406"); |
| 220 | + read_filter($table, $filter); |
| 221 | + // [END bigtable_filters_limit_value_range] |
| 222 | +} |
| 223 | + |
| 224 | +function filter_limit_value_regex($table) |
| 225 | +{ |
| 226 | + // [START bigtable_filters_limit_value_regex] |
| 227 | + $filter = Filter::value()->regex("PQ2A.*$"); |
| 228 | + read_filter($table, $filter); |
| 229 | + // [END bigtable_filters_limit_value_regex] |
| 230 | +} |
| 231 | + |
| 232 | +function filter_limit_timestamp_range($table) |
| 233 | +{ |
| 234 | + // [START bigtable_filters_limit_timestamp_range] |
| 235 | + $start = 0; |
| 236 | + $end = (time() - 60 * 60) * 1000 * 1000; |
| 237 | + $filter = Filter::timestamp() |
| 238 | + ->range() |
| 239 | + ->startClosed($start) |
| 240 | + ->endOpen($end); |
| 241 | + read_filter($table, $filter); |
| 242 | + // [END bigtable_filters_limit_timestamp_range] |
| 243 | +} |
| 244 | + |
| 245 | +function filter_limit_block_all($table) |
| 246 | +{ |
| 247 | + // [START bigtable_filters_limit_block_all] |
| 248 | + $filter = Filter::block(); |
| 249 | + read_filter($table, $filter); |
| 250 | + // [END bigtable_filters_limit_block_all] |
| 251 | +} |
| 252 | + |
| 253 | +function filter_limit_pass_all($table) |
| 254 | +{ |
| 255 | + // [START bigtable_filters_limit_pass_all] |
| 256 | + $filter = Filter::pass(); |
| 257 | + read_filter($table, $filter); |
| 258 | + // [END bigtable_filters_limit_pass_all] |
| 259 | +} |
| 260 | + |
| 261 | +function filter_modify_strip_value($table) |
| 262 | +{ |
| 263 | + // [START bigtable_filters_modify_strip_value] |
| 264 | + $filter = Filter::value()->strip(); |
| 265 | + read_filter($table, $filter); |
| 266 | + // [END bigtable_filters_modify_strip_value] |
| 267 | +} |
| 268 | + |
| 269 | +function filter_modify_apply_label($table) |
| 270 | +{ |
| 271 | + // [START bigtable_filters_modify_apply_label] |
| 272 | + $filter = Filter::label("labelled"); |
| 273 | + read_filter($table, $filter); |
| 274 | + // [END bigtable_filters_modify_apply_label] |
| 275 | +} |
| 276 | + |
| 277 | +function filter_composing_chain($table) |
| 278 | +{ |
| 279 | + // [START bigtable_filters_composing_chain] |
| 280 | + $filter = Filter::chain() |
| 281 | + ->addFilter(Filter::limit()->cellsPerColumn(1)) |
| 282 | + ->addFilter(Filter::family()->exactMatch("cell_plan")); |
| 283 | + read_filter($table, $filter); |
| 284 | + // [END bigtable_filters_composing_chain] |
| 285 | +} |
| 286 | + |
| 287 | +function filter_composing_interleave($table) |
| 288 | +{ |
| 289 | + // [START bigtable_filters_composing_interleave] |
| 290 | + $filter = Filter::interleave() |
| 291 | + ->addFilter(Filter::value()->exactMatch(unpack('C*', 1))) |
| 292 | + ->addFilter(Filter::qualifier()->exactMatch("os_build")); |
| 293 | + read_filter($table, $filter); |
| 294 | + // [END bigtable_filters_composing_interleave] |
| 295 | +} |
| 296 | + |
| 297 | +function filter_composing_condition($table) |
| 298 | +{ |
| 299 | + // [START bigtable_filters_composing_condition] |
| 300 | + $filter = Filter::condition( |
| 301 | + Filter::chain() |
| 302 | + ->addFilter(Filter::value()->exactMatch(unpack('C*', 1))) |
| 303 | + ->addFilter(Filter::qualifier()->exactMatch("data_plan_10gb")) |
| 304 | + ) |
| 305 | + ->then(Filter::label("passed-filter")) |
| 306 | + ->otherwise(Filter::label("filtered-out")); |
| 307 | + read_filter($table, $filter); |
| 308 | + // [END bigtable_filters_composing_condition] |
| 309 | +} |
| 310 | + |
| 311 | + |
| 312 | +// Call the function for the supplied READ_TYPE |
| 313 | +call_user_func($filter_type, $table); |
0 commit comments