Skip to content

Commit 7d639be

Browse files
authored
Implement the Quickstart example. (#944)
This fixes #615 for realsies.
1 parent 36823b7 commit 7d639be

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

google/cloud/bigtable/doc/bigtable-main.dox

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This page contains the reference guide to the Cloud Bigtable C++ Client Library.
55

66
## Examples
77

8+
* @ref bigtable-quickstart "Quickstart Example"
9+
810
* @ref bigtable-hello-world "Hello World Example"
911

1012
* @ref bigtable-samples-instance-admin "Instance Admin Example"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*!
2+
@page bigtable-quickstart Example: C++ Quickstart Application
3+
4+
Quickstart is a sample program demonstrating use of the Cloud Bigtable client
5+
library to read a row from an existing table.
6+
7+
## Running the example
8+
9+
This example uses the
10+
[Cloud Bigtable C++ Client Library](https://GoogleCloudPlatform.github.io/google-cloud-cpp)
11+
to communicate with Cloud Bigtable.
12+
13+
To run the example program, follow the
14+
[instructions](https://github.com/GoogleCloudPlatform/google-cloud-cpp/tree/master/bigtable/examples/)
15+
for the example on GitHub.
16+
17+
## The Code
18+
19+
Here is the full example
20+
21+
@snippet bigtable_quickstart.cc all code
22+
23+
*/

google/cloud/bigtable/examples/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ cc_binary(
2828
deps = ["//google/cloud/bigtable:bigtable_client"],
2929
)
3030

31+
cc_binary(
32+
name = "bigtable_quickstart",
33+
srcs = ["bigtable_quickstart.cc"],
34+
deps = ["//google/cloud/bigtable:bigtable_client"],
35+
)
36+
3137
cc_binary(
3238
name = "bigtable_samples",
3339
srcs = ["bigtable_samples.cc"],

google/cloud/bigtable/examples/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ target_link_libraries(bigtable_hello_world bigtable_client)
2424
add_executable(bigtable_hello_app_profile bigtable_hello_app_profile.cc)
2525
target_link_libraries(bigtable_hello_app_profile bigtable_client)
2626

27+
add_executable(bigtable_quickstart bigtable_quickstart.cc)
28+
target_link_libraries(bigtable_quickstart bigtable_client)
29+
2730
add_executable(bigtable_samples bigtable_samples.cc)
2831
target_link_libraries(bigtable_samples bigtable_client)
2932

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2018 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//! [all code] [START bigtable_quickstart]
16+
17+
#include "google/cloud/bigtable/table.h"
18+
19+
int main(int argc, char* argv[]) try {
20+
if (argc != 4) {
21+
std::string const cmd = argv[0];
22+
auto last_slash = std::string(cmd).find_last_of('/');
23+
std::cerr << "Usage: " << cmd.substr(last_slash + 1)
24+
<< " <project_id> <instance_id> <table_id>" << std::endl;
25+
return 1;
26+
}
27+
28+
std::string const project_id = argv[1];
29+
std::string const instance_id = argv[2];
30+
std::string const table_id = argv[3];
31+
32+
google::cloud::bigtable::Table table(
33+
google::cloud::bigtable::CreateDefaultDataClient(
34+
project_id, instance_id, google::cloud::bigtable::ClientOptions()),
35+
table_id);
36+
37+
std::string row_key = "r1";
38+
std::string column_family = "cf1";
39+
40+
std::cout << "Getting a single row by row key:" << std::flush;
41+
auto result = table.ReadRow(
42+
row_key, google::cloud::bigtable::Filter::FamilyRegex(column_family));
43+
if (not result.first) {
44+
std::cout << "Cannot find row " << row_key << " in the table: " << table_id
45+
<< std::endl;
46+
return 0;
47+
}
48+
49+
auto const& cell = result.second.cells().front();
50+
std::cout << cell.family_name() << ":" << cell.column_qualifier() << " @ "
51+
<< cell.timestamp().count() << "us\n"
52+
<< '"' << cell.value() << '"' << std::endl;
53+
54+
return 0;
55+
} catch (std::exception const& ex) {
56+
std::cerr << "Standard C++ exception raised: " << ex.what() << std::endl;
57+
return 1;
58+
}
59+
//! [all code] [END bigtable_quickstart]

0 commit comments

Comments
 (0)