Skip to content

Commit 11ee0b2

Browse files
chore: bigtable read_snippets.php split to individual samples in the new format (GoogleCloudPlatform#1451)
1 parent 0965373 commit 11ee0b2

File tree

9 files changed

+604
-217
lines changed

9 files changed

+604
-217
lines changed

bigtable/src/read_filter.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
3+
namespace Google\Cloud\Samples\Bigtable;
4+
5+
/**
6+
* Copyright 2019 Google LLC.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/**
22+
* For instructions on how to run the full sample:
23+
*
24+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
25+
*/
26+
27+
// [START bigtable_reads_filter]
28+
use Google\Cloud\Bigtable\BigtableClient;
29+
use Google\Cloud\Bigtable\Filter;
30+
31+
/**
32+
* Read using a filter
33+
* @param string $projectId The Google Cloud project ID
34+
* @param string $instanceId The ID of the Bigtable instance
35+
* @param string $tableId The ID of the table to read from
36+
*/
37+
function read_filter(
38+
string $projectId,
39+
string $instanceId,
40+
string $tableId
41+
): void {
42+
// Connect to an existing table with an existing instance.
43+
$dataClient = new BigtableClient([
44+
'projectId' => $projectId,
45+
]);
46+
$table = $dataClient->table($instanceId, $tableId);
47+
48+
$rowFilter = Filter::value()->regex('PQ2A.*$');
49+
50+
$rows = $table->readRows([
51+
'filter' => $rowFilter
52+
]);
53+
54+
foreach ($rows as $key => $row) {
55+
print_row($key, $row);
56+
}
57+
}
58+
// [END bigtable_reads_filter]
59+
60+
// Helper function for printing the row data
61+
function print_row($key, $row)
62+
{
63+
printf('Reading data for row %s' . PHP_EOL, $key);
64+
foreach ((array) $row as $family => $cols) {
65+
printf('Column Family %s' . PHP_EOL, $family);
66+
foreach ($cols as $col => $data) {
67+
for ($i = 0; $i < count($data); $i++) {
68+
printf(
69+
"\t%s: %s @%s%s" . PHP_EOL,
70+
$col,
71+
$data[$i]['value'],
72+
$data[$i]['timeStamp'],
73+
$data[$i]['labels'] ? sprintf(' [%s]', $data[$i]['labels']) : ''
74+
);
75+
}
76+
}
77+
}
78+
print(PHP_EOL);
79+
}
80+
81+
// The following 2 lines are only needed to run the samples
82+
require_once __DIR__ . '/../../testing/sample_helpers.php';
83+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/read_prefix.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
3+
namespace Google\Cloud\Samples\Bigtable;
4+
5+
/**
6+
* Copyright 2019 Google LLC.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/**
22+
* For instructions on how to run the full sample:
23+
*
24+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
25+
*/
26+
27+
// [START bigtable_reads_prefix]
28+
use Google\Cloud\Bigtable\BigtableClient;
29+
30+
/**
31+
* Read using a row key prefix
32+
* @param string $projectId The Google Cloud project ID
33+
* @param string $instanceId The ID of the Bigtable instance
34+
* @param string $tableId The ID of the table to read from
35+
*/
36+
function read_prefix(
37+
string $projectId,
38+
string $instanceId,
39+
string $tableId
40+
): void {
41+
// Connect to an existing table with an existing instance.
42+
$dataClient = new BigtableClient([
43+
'projectId' => $projectId,
44+
]);
45+
$table = $dataClient->table($instanceId, $tableId);
46+
47+
$prefix = 'phone#';
48+
$end = $prefix;
49+
// Increment the last character of the prefix so the filter matches everything in between
50+
$end[-1] = chr(
51+
ord($end[-1]) + 1
52+
);
53+
54+
$rows = $table->readRows([
55+
'rowRanges' => [
56+
[
57+
'startKeyClosed' => $prefix,
58+
'endKeyClosed' => $end,
59+
]
60+
]
61+
]);
62+
63+
foreach ($rows as $key => $row) {
64+
print_row($key, $row);
65+
}
66+
}
67+
// [END bigtable_reads_prefix]
68+
69+
// Helper function for printing the row data
70+
function print_row($key, $row)
71+
{
72+
printf('Reading data for row %s' . PHP_EOL, $key);
73+
foreach ((array) $row as $family => $cols) {
74+
printf('Column Family %s' . PHP_EOL, $family);
75+
foreach ($cols as $col => $data) {
76+
for ($i = 0; $i < count($data); $i++) {
77+
printf(
78+
"\t%s: %s @%s%s" . PHP_EOL,
79+
$col,
80+
$data[$i]['value'],
81+
$data[$i]['timeStamp'],
82+
$data[$i]['labels'] ? sprintf(' [%s]', $data[$i]['labels']) : ''
83+
);
84+
}
85+
}
86+
}
87+
print(PHP_EOL);
88+
}
89+
90+
// The following 2 lines are only needed to run the samples
91+
require_once __DIR__ . '/../../testing/sample_helpers.php';
92+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/read_row.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
3+
namespace Google\Cloud\Samples\Bigtable;
4+
5+
/**
6+
* Copyright 2019 Google LLC.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/**
22+
* For instructions on how to run the full sample:
23+
*
24+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
25+
*/
26+
27+
// [START bigtable_reads_row]
28+
use Google\Cloud\Bigtable\BigtableClient;
29+
30+
/**
31+
* Read a row using the row key
32+
* @param string $projectId The Google Cloud project ID
33+
* @param string $instanceId The ID of the Bigtable instance
34+
* @param string $tableId The ID of the table to read from
35+
*/
36+
function read_row(
37+
string $projectId,
38+
string $instanceId,
39+
string $tableId
40+
): void {
41+
// Connect to an existing table with an existing instance.
42+
$dataClient = new BigtableClient([
43+
'projectId' => $projectId,
44+
]);
45+
$table = $dataClient->table($instanceId, $tableId);
46+
47+
$rowkey = 'phone#4c410523#20190501';
48+
$row = $table->readRow($rowkey);
49+
50+
print_row($rowkey, $row);
51+
}
52+
// [END bigtable_reads_row]
53+
54+
// [START bigtable_reads_print]
55+
// Helper function for printing the row data
56+
function print_row($key, $row)
57+
{
58+
printf('Reading data for row %s' . PHP_EOL, $key);
59+
foreach ((array) $row as $family => $cols) {
60+
printf('Column Family %s' . PHP_EOL, $family);
61+
foreach ($cols as $col => $data) {
62+
for ($i = 0; $i < count($data); $i++) {
63+
printf(
64+
"\t%s: %s @%s%s" . PHP_EOL,
65+
$col,
66+
$data[$i]['value'],
67+
$data[$i]['timeStamp'],
68+
$data[$i]['labels'] ? sprintf(' [%s]', $data[$i]['labels']) : ''
69+
);
70+
}
71+
}
72+
}
73+
print(PHP_EOL);
74+
}
75+
// [END bigtable_reads_print]
76+
77+
// The following 2 lines are only needed to run the samples
78+
require_once __DIR__ . '/../../testing/sample_helpers.php';
79+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/read_row_partial.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
3+
namespace Google\Cloud\Samples\Bigtable;
4+
5+
/**
6+
* Copyright 2019 Google LLC.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/**
22+
* For instructions on how to run the full sample:
23+
*
24+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
25+
*/
26+
27+
// [START bigtable_reads_row_partial]
28+
use Google\Cloud\Bigtable\BigtableClient;
29+
use Google\Cloud\Bigtable\Filter;
30+
31+
/**
32+
* Read partial row data
33+
* @param string $projectId The Google Cloud project ID
34+
* @param string $instanceId The ID of the Bigtable instance
35+
* @param string $tableId The ID of the table to read from
36+
*/
37+
function read_row_partial(
38+
string $projectId,
39+
string $instanceId,
40+
string $tableId
41+
): void {
42+
// Connect to an existing table with an existing instance.
43+
$dataClient = new BigtableClient([
44+
'projectId' => $projectId,
45+
]);
46+
$table = $dataClient->table($instanceId, $tableId);
47+
48+
$rowkey = 'phone#4c410523#20190501';
49+
$rowFilter = Filter::qualifier()->regex('os_build');
50+
$row = $table->readRow($rowkey, ['filter' => $rowFilter]);
51+
52+
print_row($rowkey, $row);
53+
}
54+
// [END bigtable_reads_row_partial]
55+
56+
// Helper function for printing the row data
57+
function print_row($key, $row)
58+
{
59+
printf('Reading data for row %s' . PHP_EOL, $key);
60+
foreach ((array) $row as $family => $cols) {
61+
printf('Column Family %s' . PHP_EOL, $family);
62+
foreach ($cols as $col => $data) {
63+
for ($i = 0; $i < count($data); $i++) {
64+
printf(
65+
"\t%s: %s @%s%s" . PHP_EOL,
66+
$col,
67+
$data[$i]['value'],
68+
$data[$i]['timeStamp'],
69+
$data[$i]['labels'] ? sprintf(' [%s]', $data[$i]['labels']) : ''
70+
);
71+
}
72+
}
73+
}
74+
print(PHP_EOL);
75+
}
76+
77+
// The following 2 lines are only needed to run the samples
78+
require_once __DIR__ . '/../../testing/sample_helpers.php';
79+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)