feat: implement code for Cloud Bigtable tutorial by coryan · Pull Request #238 · GoogleCloudPlatform/functions-framework-cpp · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion ci/README.md
13 changes: 8 additions & 5 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
# limitations under the License.
# ~~~

find_package(google_cloud_cpp_storage REQUIRED)
find_package(google_cloud_cpp_pubsub REQUIRED)
find_package(fmt REQUIRED)
find_package(Boost REQUIRED COMPONENTS log)
find_package(fmt REQUIRED)
find_package(google_cloud_cpp_bigtable REQUIRED)
find_package(google_cloud_cpp_pubsub REQUIRED)
find_package(google_cloud_cpp_storage REQUIRED)

add_library(
functions_framework_examples # cmake-format: sort
Expand Down Expand Up @@ -49,7 +50,8 @@ add_library(
site/tips_infinite_retries/tips_infinite_retries.cc
site/tips_lazy_globals/tips_lazy_globals.cc
site/tips_retry/tips_retry.cc
site/tips_scopes/tips_scopes.cc)
site/tips_scopes/tips_scopes.cc
site/tutorial_cloud_bigtable/tutorial_cloud_bigtable.cc)
functions_framework_cpp_add_common_options(functions_framework_examples)
if (MSVC)
set_property(
Expand All @@ -59,7 +61,8 @@ if (MSVC)
endif ()
target_link_libraries(
functions_framework_examples fmt::fmt functions-framework-cpp::framework
google-cloud-cpp::pubsub google-cloud-cpp::storage)
google-cloud-cpp::bigtable google-cloud-cpp::pubsub
google-cloud-cpp::storage)

if (BUILD_TESTING)
find_package(GTest CONFIG REQUIRED)
Expand Down
23 changes: 23 additions & 0 deletions examples/site/tutorial_cloud_bigtable/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ~~~
# Copyright 2021 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.
# ~~~

find_package(google_cloud_cpp_bigtable REQUIRED)
find_package(functions_framework_cpp REQUIRED)

add_library(functions_framework_cpp_function tutorial_cloud_bigtable.cc)
target_link_libraries(
functions_framework_cpp_function functions-framework-cpp::framework
google-cloud-cpp::bigtable)
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2021 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.

// [START bigtable_functions_quickstart]
#include <google/cloud/bigtable/table.h>
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <algorithm>
#include <cctype>
#include <mutex>
#include <sstream>
#include <string>

namespace gcf = ::google::cloud::functions;
namespace cbt = ::google::cloud::bigtable;

cbt::Table get_table_client(std::string project_id, std::string instance_id,
std::string const& table_id) {
static std::mutex mu;
static std::unique_ptr<cbt::Table> table;
std::lock_guard lk(mu);
if (table == nullptr || table->table_id() != table_id ||
table->instance_id() != instance_id ||
table->project_id() != project_id) {
table = std::make_unique<cbt::Table>(
cbt::CreateDefaultDataClient(std::move(project_id),
std::move(instance_id),
cbt::ClientOptions{}),
table_id);
}
return *table;
}

gcf::HttpResponse tutorial_cloud_bigtable(gcf::HttpRequest request) { // NOLINT
auto get_header = [h = request.headers()](std::string key) {
std::transform(key.begin(), key.end(), key.begin(),
[](auto x) { return static_cast<char>(std::tolower(x)); });
auto l = h.find(key);
if (l == h.end()) throw std::invalid_argument("Missing header: " + key);
return l->second;
};

auto project_id = get_header("projectID");
auto instance_id = get_header("instanceID");
auto table_id = get_header("tableID");
auto table =
get_table_client(std::move(project_id), std::move(instance_id), table_id);

std::ostringstream os;
auto filter =
cbt::Filter::Chain(cbt::Filter::Latest(1),
cbt::Filter::ColumnName("stats_summary", "os_build"));
for (auto row : table.ReadRows(cbt::RowRange::Prefix("phone#"), filter)) {
if (!row) {
std::ostringstream err;
err << "Error reading from bigtable: " << row.status();
throw std::runtime_error(std::move(err).str());
}
for (auto const& cell : row->cells()) {
os << "Rowkey: " << cell.row_key() << ", os_build: " << cell.value()
<< "\n";
}
}

gcf::HttpResponse response;
response.set_payload(std::move(os).str());
response.set_header("content-type", "text/plain");
return response;
}
// [END bigtable_functions_quickstart]
9 changes: 9 additions & 0 deletions examples/site/tutorial_cloud_bigtable/vcpkg.json