Google Cloud Spanner C++ Client  1.15.0
A C++ Client Library for Google Cloud Spanner
Cloud Spanner C++ Client Library

The Cloud Spanner C++ Client library offers types and functions to use Cloud Spanner from C++11 applications.

This library requires a C++11 compiler, it is supported (and tested) on multiple Linux distributions.

Quickstart

The following instructions show you how to perform basic tasks in Cloud Spanner using the C++ client library.

Before you begin

  1. Select or create a Google Cloud Platform (GCP) project using the manage resource page. Make a note of the project id, you will need to use it later.
  2. Make sure that billing is enabled for your project.
  3. Learn about key terms and concepts for Cloud Spanner.
  4. Setup the authentication for the examples:

Setting up your repo

In order to use the Cloud Spanner C++ client library from your own code, you'll need to teach your build system how to fetch and compile the Cloud C++ client library. The Cloud Spanner C++ client library natively supports the Bazel and CMake build systems. We've created a minimal, "Hello world", quickstart repo that includes detailed instructions on how to compile the library for use in your application. You can fetch the source from GitHub as normal:

git clone https://github.com/googleapis/google-cloud-cpp.git
cd google-cloud-cpp/google/cloud/spanner/quickstart
Example: Hello World

The following shows the code that you'll run in the google/cloud/spanner/quickstart/ directory, which should give you a taste of the Cloud Spanner C++ client library API.

// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <iostream>
#include <stdexcept>
int main(int argc, char* argv[]) try {
if (argc != 4) {
std::cerr << "Usage: " << argv[0]
<< " project-id instance-id database-id\n";
return 1;
}
namespace spanner = ::google::cloud::spanner;
spanner::Client client(
spanner::MakeConnection(spanner::Database(argv[1], argv[2], argv[3])));
auto rows =
client.ExecuteQuery(spanner::SqlStatement("SELECT 'Hello World'"));
for (auto const& row : spanner::StreamOf<std::tuple<std::string>>(rows)) {
if (!row) throw std::runtime_error(row.status().message());
std::cout << std::get<0>(*row) << "\n";
}
return 0;
} catch (std::exception const& ex) {
std::cerr << "Standard exception raised: " << ex.what() << "\n";
return 1;
}

API Notes

The following are general notes about using the library.

Environment Variables

There are several environment variables that can be set to configure certain behaviors in the library.

  • GOOGLE_CLOUD_CPP_ENABLE_CLOG=yes turns on logging in the library, basically the library always "logs" but the logging infrastructure has no backend to actually print anything until the application sets a backend or they set this environment variable.
  • GOOGLE_CLOUD_CPP_ENABLE_TRACING=rpc turns on tracing for most gRPC calls, the library injects an additional Stub decorator that prints each gRPC request and response.
  • GOOGLE_CLOUD_CPP_ENABLE_TRACING=rpc-streams turns on tracing for streaming gRPC calls, such as Client::Read(), Client::ExecuteQuery(), and Client::ProfileQuery(). This can produce a lot of output, so use with caution!
  • GOOGLE_CLOUD_CPP_TRACING_OPTIONS=... modifies the behavior of gRPC tracing, including whether messages will be output on multiple lines, or whether string/bytes fields will be truncated.
  • GOOGLE_CLOUD_CPP_SPANNER_DEFAULT_ENDPOINT=... changes the default endpoint (spanner.googleapis.com) for the library.
  • GOOGLE_CLOUD_PROJECT=... is used in examples and integration tests to configure the GCP project.
  • GOOGLE_CLOUD_CPP_SPANNER_TEST_INSTANCE_ID=... is used in examples and integration tests to, well, configure the spanner instance.
  • GOOGLE_CLOUD_CPP_SPANNER_TEST_SERVICE_ACCOUNT=... is used in examples and integration tests to set the service account for testing IAM operations.
  • SPANNER_EMULATOR_HOST=host:port tells the library to connect to the specified emulator rather than the default endpoint.
  • SPANNER_OPTIMIZER_VERSION=n sets the default query optimizer version to use in Client::ExecuteQuery() calls.

Error Handling

This library never throws exceptions to signal error. In general, the library returns a StatusOr<T> if an error is possible. Some functions return objects that are not wrapped in a StatusOr<> but will themselves return a StatusOr<T> to signal an error. For example, wrappers for long running operations return future<StatusOr<T>>, and the methods on Client that read from a database return a RowStream, which iterates a stream of StatusOr<T>.

Example

Applications should check if the StatusOr<T> contains a value before using it, much like how you might check that a pointer is not null before dereferencing it. Indeed, a StatusOr<T> object can be used like a smart-pointer to T, with the main difference being that when it does not hold a T it will instead hold a Status object with extra information about the error.

You can check that a StatusOr<T> contains a value by calling the .ok() method, or by using operator bool() (like with other smart pointers). If there is no value, you can access the contained Status object using the .status() member. If there is a value, you may access it by dereferencing with operator*() or operator->(). As with all smart pointers, callers must first check that the StatusOr<T> contains a value before dereferencing and accessing the contained value. Alternatively, callers may instead use the .value() member function which is defined to throw a RuntimeStatusError if there is no value.

Note
If you're compiling with exceptions disabled, calling .value() on a StatusOr<T> that does not contain a value will terminate the program instead of throwing.
namespace spanner = ::google::cloud::spanner;
[](spanner::Client client) {
auto rows = client.Read("Albums", spanner::KeySet::All(), {"AlbumTitle"});
// The actual type of `row` is google::cloud::StatusOr<spanner::Row>, but
// we expect it'll most often be declared with auto like this.
for (auto const& row : rows) {
// Use `row` like a smart pointer; check it before dereferencing
if (!row) {
// `row` doesn't contain a value, so `.status()` will contain error info
std::cerr << row.status();
break;
}
// The actual type of `song` is google::cloud::StatusOr<std::string>, but
// again we expect it'll be commonly declared with auto as we show here.
auto song = row->get<std::string>("AlbumTitle");
// Instead of checking then dereferencing `song` as we did with `row`
// above, here we demonstrate use of the `.value()` member, which will
// return a reference to the contained `T` if it exists, otherwise it
// will throw an exception (or terminate if compiled without exceptions).
std::cout << "SongName: " << song.value() << "\n";
}
}

Retry, Backoff, and Idempotency Policies.

The library automatically retries requests that fail with transient errors, and follows the recommended practices with respect to backoff between retries. Application developers can override the default retry and backoff policies.

The default policies are to continue retrying for up to 15 minutes, and to use truncated (at 5 minutes) exponential backoff, doubling the maximum backoff period between retries.

namespace spanner = ::google::cloud::spanner;
[](std::string const& project_id, std::string const& instance_id,
std::string const& database_id) {
auto client = spanner::Client(spanner::MakeConnection(
spanner::Database(project_id, instance_id, database_id),
spanner::ConnectionOptions{}, spanner::SessionPoolOptions{},
// Retry for at most 25 minutes.
/*maximum_duration=*/std::chrono::minutes(25))
.clone(),
// Use an truncated exponential backoff with jitter to wait between
// retries:
// https://en.wikipedia.org/wiki/Exponential_backoff
// https://cloud.google.com/storage/docs/exponential-backoff
/*initial_delay=*/std::chrono::seconds(2),
/*maximum_delay=*/std::chrono::minutes(10),
/*scaling=*/1.5)
.clone()));
auto rows =
client.ExecuteQuery(spanner::SqlStatement("SELECT 'Hello World'"));
for (auto const& row : spanner::StreamOf<std::tuple<std::string>>(rows)) {
if (!row) throw std::runtime_error(row.status().message());
std::cout << std::get<0>(*row) << "\n";
}
}
See also
LimitedTimeRetryPolicy and LimitedErrorCountRetryPolicy for alternative retry policies.
ExponentialBackoffPolicy to configure different parameters for the exponential backoff policy.

Next Steps

Mocking the Cloud Spanner C++ Client with Google Mock