1+ <?php
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