Skip to content

Commit 000a10f

Browse files
author
Juan Rael
committed
Read Samples
1 parent 6365ab2 commit 000a10f

File tree

6 files changed

+420
-0
lines changed

6 files changed

+420
-0
lines changed

bigtable/api/src/getting_a_row.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
3+
/**
4+
* Copyright 2018 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/api/README.md
23+
*/
24+
25+
// Include Google Cloud dependendencies using Composer
26+
require_once __DIR__ . '/../vendor/autoload.php';
27+
28+
if (count($argv) < 3 || count($argv) > 5) {
29+
return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID" . PHP_EOL, __FILE__);
30+
}
31+
list($_, $project_id, $instance_id, $table_id) = $argv;
32+
33+
// [START getting_a_row]
34+
35+
use Google\Cloud\Bigtable\BigtableClient;
36+
use Google\Cloud\Bigtable\Table;
37+
use Google\Cloud\Bigtable\Admin\V2\Cluster;
38+
use Google\Cloud\Bigtable\Admin\V2\StorageType;
39+
use Google\ApiCore\ApiException;
40+
use Google\Cloud\Bigtable\V2\RowFilter;
41+
42+
/** Uncomment and populate these variables in your code */
43+
// $project_id = 'The Google project ID';
44+
// $instance_id = 'The Bigtable instance ID';
45+
// $cluster_id = 'The Bigtable cluster ID';
46+
// $table_id = 'The Bigtable table ID';
47+
// $location_id = 'The Bigtable region ID';
48+
49+
50+
// Connect to an existing table with an existing instance.
51+
$dataClient = new BigtableClient([
52+
'projectId' => $project_id,
53+
]);
54+
$table = $dataClient->table($instance_id, $table_id);
55+
56+
printf('Getting a single greeting by row key.' . PHP_EOL);
57+
$key = 'greeting0';
58+
// Only retrieve the most recent version of the cell.
59+
$row_filter = (new RowFilter)->setCellsPerColumnLimitFilter(1);
60+
61+
$column = 'greeting';
62+
$columnFamilyId = 'cf1';
63+
64+
$row = $table->readRow($key, [
65+
'rowFilter' => $row_filter
66+
]);
67+
printf('%s' . PHP_EOL, $row[$columnFamilyId][$column][0]['value']);
68+
// [END getting_a_row]

bigtable/api/src/insert.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
/**
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
require __DIR__ . '/../vendor/autoload.php';
18+
19+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
20+
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
21+
use Google\Cloud\Bigtable\Admin\V2\ColumnFamily;
22+
use Google\Cloud\Bigtable\BigtableClient;
23+
use Google\Cloud\Bigtable\Table;
24+
use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest\Modification;
25+
use Google\Cloud\Bigtable\Admin\V2\Table as TableClass;
26+
use Google\ApiCore\ApiException;
27+
$instance_id = 'quickstart-instance-php'; # instance-id
28+
$table_id = 'bigtable-php-table'; # my-table
29+
$project_id = getenv('PROJECT_ID');
30+
$dataClient = new BigtableClient([
31+
'projectId' => $project_id,
32+
]);
33+
34+
$instanceAdminClient = new BigtableInstanceAdminClient();
35+
$tableAdminClient = new BigtableTableAdminClient();
36+
37+
$instanceName = $instanceAdminClient->instanceName($project_id, $instance_id);
38+
$tableName = $tableAdminClient->tableName($project_id, $instance_id, $table_id);
39+
40+
$table = new TableClass();
41+
42+
$tableAdminClient->createtable(
43+
$instanceName,
44+
$table_id,
45+
$table
46+
);
47+
48+
$table = $dataClient->table($instance_id,$table_id);
49+
50+
$columnFamily4 = new ColumnFamily();
51+
$columnModification = new Modification();
52+
$columnModification->setId('cf1');
53+
$columnModification->setCreate($columnFamily4);
54+
$tableAdminClient->modifyColumnFamilies($tableName, [$columnModification]);
55+
56+
function time_in_microseconds(){
57+
$mt = microtime(true);
58+
$mt = sprintf('%.03f',$mt);
59+
return (float)$mt*1000000;
60+
}
61+
$insertRows = [
62+
'rk5' => [
63+
'cf1' => [
64+
'cq5' => [
65+
'value' => "Value5",
66+
'timeStamp' => time_in_microseconds()
67+
]
68+
]
69+
]
70+
];
71+
$table->upsert($insertRows);

bigtable/api/src/quickstart.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
3+
/**
4+
* Copyright 2018 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/api/README.md
23+
*/
24+
25+
// Include Google Cloud dependendencies using Composer
26+
require_once __DIR__ . '/../vendor/autoload.php';
27+
28+
if (count($argv) < 3 || count($argv) > 5) {
29+
return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID [LOCATION_ID]" . PHP_EOL, __FILE__);
30+
}
31+
list($_, $project_id, $instance_id, $table_id) = $argv;
32+
$location_id = isset($argv[5]) ? $argv[5] : 'us-east1-b';
33+
34+
// [START bigtable_quickstart]
35+
36+
use Google\Cloud\Bigtable\BigtableClient;
37+
use Google\Cloud\Bigtable\Table;
38+
use Google\Cloud\Bigtable\Admin\V2\Cluster;
39+
use Google\Cloud\Bigtable\Admin\V2\StorageType;
40+
use Google\ApiCore\ApiException;
41+
42+
43+
/** Uncomment and populate these variables in your code */
44+
// $project_id = 'The Google project ID';
45+
// $instance_id = 'The Bigtable instance ID';
46+
// $cluster_id = 'The Bigtable cluster ID';
47+
// $table_id = 'The Bigtable table ID';
48+
// $location_id = 'The Bigtable region ID';
49+
50+
51+
// Connect to an existing table with an existing instance.
52+
$dataClient = new BigtableClient([
53+
'projectId' => $project_id,
54+
]);
55+
$table = $dataClient->table($instance_id, $table_id);
56+
$key = 'rk5';
57+
// Read a row from my-table using a row key
58+
$row = $table->readRow($key);
59+
60+
$column_family_id = 'cf1';
61+
$column_id = 'cq5';
62+
// Get the Value from the Row, using the column_family_id and column_id
63+
64+
$value = $row[$column_family_id][$column_id][0]['value'];
65+
66+
printf("Row key: %s\nData: %s\n", $key, $value);
67+
// [END bigtable_quickstart]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
3+
/**
4+
* Copyright 2018 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/api/README.md
23+
*/
24+
25+
// Include Google Cloud dependendencies using Composer
26+
require_once __DIR__ . '/../vendor/autoload.php';
27+
28+
if (count($argv) < 3 || count($argv) > 5) {
29+
return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID" . PHP_EOL, __FILE__);
30+
}
31+
list($_, $project_id, $instance_id, $table_id) = $argv;
32+
33+
// [START scanning_all_rows]
34+
35+
use Google\Cloud\Bigtable\Admin\V2\StorageType;
36+
use Google\Cloud\Bigtable\Admin\V2\Cluster;
37+
use Google\Cloud\Bigtable\BigtableClient;
38+
use Google\Cloud\Bigtable\Mutations;
39+
use Google\ApiCore\ApiException;
40+
use Google\Cloud\Bigtable\Table;
41+
42+
43+
/** Uncomment and populate these variables in your code */
44+
// $project_id = 'The Google project ID';
45+
// $instance_id = 'The Bigtable instance ID';
46+
// $cluster_id = 'The Bigtable cluster ID';
47+
// $table_id = 'The Bigtable table ID';
48+
// $location_id = 'The Bigtable region ID';
49+
50+
51+
// Connect to an existing table with an existing instance.
52+
$dataClient = new BigtableClient([
53+
'projectId' => $project_id,
54+
]);
55+
$table = $dataClient->table($instance_id, $table_id);
56+
$columnFamilyId = 'cf1';
57+
$column = 'greeting';
58+
printf('Scanning for all greetings:' . PHP_EOL);
59+
$partial_rows = $table->readRows([])->readAll();
60+
foreach ($partial_rows as $row) {
61+
//print_r( $row[$columnFamilyId][$column] );
62+
printf('%s' . PHP_EOL, $row[$columnFamilyId][$column][0]['value']);
63+
}
64+
// [END scanning_all_rows]

bigtable/api/src/writing_rows.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
3+
/**
4+
* Copyright 2018 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/api/README.md
23+
*/
24+
25+
// Include Google Cloud dependendencies using Composer
26+
require_once __DIR__ . '/../vendor/autoload.php';
27+
28+
if (count($argv) < 3 || count($argv) > 5) {
29+
return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID" . PHP_EOL, __FILE__);
30+
}
31+
list($_, $project_id, $instance_id, $table_id) = $argv;
32+
33+
// [START writing_rows]
34+
35+
use Google\Cloud\Bigtable\Admin\V2\StorageType;
36+
use Google\Cloud\Bigtable\Admin\V2\Cluster;
37+
use Google\Cloud\Bigtable\BigtableClient;
38+
use Google\Cloud\Bigtable\Mutations;
39+
use Google\ApiCore\ApiException;
40+
use Google\Cloud\Bigtable\Table;
41+
42+
43+
/** Uncomment and populate these variables in your code */
44+
// $project_id = 'The Google project ID';
45+
// $instance_id = 'The Bigtable instance ID';
46+
// $cluster_id = 'The Bigtable cluster ID';
47+
// $table_id = 'The Bigtable table ID';
48+
// $location_id = 'The Bigtable region ID';
49+
50+
51+
// Connect to an existing table with an existing instance.
52+
$dataClient = new BigtableClient([
53+
'projectId' => $project_id,
54+
]);
55+
$table = $dataClient->table($instance_id, $table_id);
56+
57+
printf('Writing some greetings to the table.' . PHP_EOL);
58+
$greetings = ['Hello World!', 'Hello Cloud Bigtable!', 'Hello PHP!'];
59+
$entries = [];
60+
$column = 'greeting';
61+
$columnFamilyId = 'cf1';
62+
foreach ($greetings as $i => $value) {
63+
$row_key = sprintf('greeting%s', $i);
64+
$rowMutation = new Mutations();
65+
$rowMutation->upsert($columnFamilyId, $column, $value, time() * 1000);
66+
$entries[$row_key] = $rowMutation;
67+
}
68+
$table->mutateRows($entries);
69+
// [END writing_rows]

0 commit comments

Comments
 (0)