Skip to content

Commit 31921b5

Browse files
authored
Model Hello World examples like other languages. (#364)
Refactor Hello World example to look like other languages. Avoid confusion by using .dox for Doxygen documents.
1 parent 27da1c1 commit 31921b5

11 files changed

Lines changed: 306 additions & 246 deletions

bigtable/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ else ()
132132
set(DOXYGEN_PROJECT_BRIEF "A C++ Client Library for Google Cloud Bigtable")
133133
set(DOXYGEN_PROJECT_NUMBER
134134
"${BIGTABLE_CLIENT_VERSION_MAJOR}.${BIGTABLE_CLIENT_VERSION_MINOR}.${BIGTABLE_CLIENT_VERSION_PATCH}")
135-
set(DOXYGEN_USE_MDFILE_AS_MAINPAGE ./doc/bigtable-main.md)
136135
set(DOXYGEN_EXAMPLE_PATH examples)
137136
set(DOXYGEN_PREDEFINED "BIGTABLE_CLIENT_NS=v${BIGTABLE_CLIENT_VERSION_MAJOR}")
138137
set(DOXYGEN_EXCLUDE_PATTERNS "*/client/internal/*;*/client/testing/*;*/*_test.cc")
@@ -160,7 +159,7 @@ add_subdirectory(tests)
160159

161160
if (GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
162161
add_subdirectory(benchmarks)
163-
add_subdirectory(examples/quick_start)
162+
add_subdirectory(examples)
164163
endif (GOOGLE_CLOUD_CPP_ENABLE_CXX_EXCEPTIONS)
165164

166165
option(BIGTABLE_CLIENT_CLANG_TIDY
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*!
2+
@page bigtable-hello-world Example: C++ Hello World Application
3+
4+
This example is a very simple "hello world" application, written in C++, that
5+
illustrates how to:
6+
7+
- Connect to a Cloud Bigtable instance.
8+
- Create a new table.
9+
- Write data to the table.
10+
- Read the data back.
11+
- Delete the table.
12+
13+
## Running the example
14+
15+
This example uses the
16+
[Cloud Bigtable C++ Client Library](https://GoogleCloudPlatform.github.io/google-cloud-cpp)
17+
to communicate with Cloud Bigtable.
18+
19+
To run the example program, follow the
20+
[instructions](https://github.com/GoogleCloudPlatform/google-cloud-cpp/tree/master/bigtable/examples/)
21+
for the example on GitHub.
22+
23+
### Including the Necessary Headers
24+
25+
The example uses the following headers:
26+
27+
@snippet bigtable_hello_world.cc bigtable includes
28+
29+
### Connecting to Cloud Bigtable to manage tables
30+
31+
To manage tables, connect to Cloud Bigtable using
32+
`bigtable::CreateDefaultAdminClient()`:
33+
34+
@snippet bigtable_hello_world.cc connect admin
35+
36+
### Creating a table
37+
38+
Create a table with `bigtable::TableAdmin::CreateTable()`:
39+
40+
@snippet bigtable_hello_world.cc create table
41+
42+
@see `bigtable::v0::TableAdmin` for additional operations to list, read, modify,
43+
and delete tables.
44+
45+
### Connecting to Cloud Bigtable to manage data
46+
47+
To manage data, connect to Cloud Bigtable using
48+
`bigtable::CreateDefaultDataClient()`:
49+
50+
@snippet bigtable_hello_world.cc connect data
51+
52+
### Writing Rows to a table
53+
54+
Then write the data:
55+
56+
@snippet bigtable_hello_world.cc write rows
57+
58+
@see `bigtable::v0::Table::BulkApply()` to modify multiple rows in a single
59+
operation. In addition to `SetCell()` there
60+
are functions to delete columns (e.g., `DeleteFromFamily()`) or to delete the
61+
whole row (`DeleteFromRow()`).
62+
63+
### Reading a row by its key
64+
65+
Get a row directly using its key with `bigtable::Table::ReadRow()`:
66+
67+
@snippet bigtable_hello_world.cc read row
68+
69+
@see `bigtable::v0::Filter` to filter the column families, columns, and even
70+
the timestamps returned by `ReadRow()`.
71+
@see `bigtable::v0::Table::ReadRows()` to iterate over multiple rows.
72+
73+
### Scanning all table rows
74+
75+
Use `bigtable::Table::ReadRows()` to scan all of the rows in a table.
76+
77+
@snippet bigtable_hello_world.cc scan all
78+
79+
@see `bigtable::v0::RowRange` to scan only a portion of the table.
80+
81+
### Deleting a table
82+
83+
Delete a table with `TableAdmin::DeleteTable`.
84+
85+
@snippet bigtable_hello_world.cc delete table
86+
87+
## Putting it all together
88+
89+
Here is the full example
90+
91+
@snippet bigtable_hello_world.cc all code
92+
*/

bigtable/doc/bigtable-main.dox

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*!
2+
@mainpage Cloud Bigtable C++ Client Library
3+
4+
This page contains the reference guide to the Cloud Bigtable C++ Client Library.
5+
6+
## Examples
7+
8+
* @ref bigtable-hello-world "Hello World Example"
9+
*/

bigtable/doc/bigtable-main.md

Lines changed: 0 additions & 68 deletions
This file was deleted.

bigtable/examples/quick_start/CMakeLists.txt renamed to bigtable/examples/CMakeLists.txt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,5 @@
1616
# to keep this particular file as simple as possible, as it is intended to be
1717
# part of the examples.
1818

19-
add_executable(bigtable_create_table bigtable_create_table.cc)
20-
target_link_libraries(bigtable_create_table bigtable_client)
21-
22-
add_executable(bigtable_write_row bigtable_write_row.cc)
23-
target_link_libraries(bigtable_write_row bigtable_client)
24-
25-
add_executable(bigtable_read_row bigtable_read_row.cc)
26-
target_link_libraries(bigtable_read_row bigtable_client)
19+
add_executable(bigtable_hello_world bigtable_hello_world.cc)
20+
target_link_libraries(bigtable_hello_world bigtable_client)

bigtable/examples/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Cloud Bigtable C++ Client Quick Start Code Samples
2+
3+
This directory contains minimal examples to get started quickly, and are
4+
referenced from the Doxygen landing page as "Hello World".
5+
6+
## Compiling the Examples
7+
8+
These examples are compiled as part of the build for the Cloud Bigtable C++
9+
Client. The instructions on how to compile the code are in the
10+
[top-level README](../../README.md) file.
11+
12+
## Running the examples against the Cloud Bigtable Emulator
13+
14+
The easiest way to run these examples is to use the Cloud Bigtable Emulator.
15+
This emulator is included as part of the
16+
[Google Cloud SDK](https://cloud.google.com/sdk/).
17+
18+
### Installing the Emulator
19+
20+
Follow the relevant
21+
[documentation](https://cloud.google.com/bigtable/docs/emulator) to install
22+
the emulator.
23+
24+
### Running the Emulator
25+
26+
Start the emulator as described in the documentation:
27+
28+
```console
29+
$ gcloud beta emulators bigtable start
30+
```
31+
32+
On a separate shell setup the necessary environment variables:
33+
34+
```console
35+
$ $(gcloud beta emulators bigtable env-init)
36+
```
37+
38+
## Running the examples against a production version of Cloud Bigtable
39+
40+
You first need to create a production instance, the easiest way is to
41+
follow the
42+
[instructions](https://cloud.google.com/bigtable/docs/creating-instance)
43+
on the Cloud Bigtable site.
44+
45+
### Configuring gRPC Root Certificates
46+
47+
You may need to configure gRPC to accept the Google server certificates.
48+
gRPC expects to find a
49+
[PEM](https://en.wikipedia.org/wiki/Privacy-enhanced_Electronic_Mail) file
50+
with the list of trusted root certificates in `/usr/share/grpc/roots.pem`
51+
(or `${PREFIX}/share/grpc/roots.pem` if you installed gRPC with a different
52+
`PREFIX`, e.g. `/usr/local`). If your system does not have this file installed
53+
you need to set the `GRPC_DEFAULT_SSL_ROOTS_FILE_PATH` to the location of this
54+
file.
55+
56+
The gRPC source, included as a submodule of `google-cloud-cpp`, contains
57+
a version of this file. If you want to use that version use:
58+
59+
```console
60+
$ GRPC_DEFAULT_SSL_ROOTS_FILE_PATH=$HOME/google-cloud-cpp/third_party/grpc/etc/roots.pem
61+
$ export GRPC_DEFAULT_SSL_ROOTS_FILE_PATH
62+
```
63+
64+
### Authenticate with Google Cloud
65+
66+
You may need to authenticate with Google Cloud Platform. The Google Cloud SDK
67+
offers a simple way to do so:
68+
69+
```console
70+
$ gcloud auth login
71+
```
72+
73+
## Running the Quick Start Examples using a Cloud Bigtable Instance
74+
75+
After configuring gRPC, you can run the examples using:
76+
77+
```console
78+
$ export PROJECT_ID=... # The name of your project
79+
$ export INSTANCE_ID=... # THe name of your instance
80+
$ ./bigtable_hello_world ${PROJECT_ID} ${INSTANCE_ID} example-table
81+
```

0 commit comments

Comments
 (0)