Skip to content

Commit 659a261

Browse files
authored
impl(bigtable): add snippet demonstrating query execution (#15753)
1 parent 6d79425 commit 659a261

File tree

4 files changed

+68
-30
lines changed

4 files changed

+68
-30
lines changed

google/cloud/bigtable/client.h

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,7 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
4949
* For this reason, it is recommended to reuse `Client` objects when possible.
5050
*
5151
* @par Example
52-
* @code
53-
* #include "google/cloud/bigtable/client.h"
54-
* #include "google/cloud/project.h"
55-
* #include <iostream>
56-
*
57-
* int main() {
58-
* namespace cbt = google::cloud::bigtable;
59-
* cbt::Client client(cbt::MakeDataConnection());
60-
* cbt::InstanceResource instance(google::cloud::Project("my-project"),
61-
* "my-instance");
62-
*
63-
* // Declare a parameter with a type, but no value.
64-
* cbt::SqlStatement statement(
65-
* "SELECT _key, CAST(family['qual'] AS STRING) AS value "
66-
* "FROM my-table WHERE _key = @key",
67-
* {{"key", cbt::Value(cbt::Bytes())}});
68-
*
69-
* google::cloud::StatusOr<cbt::PreparedQuery> prepared_query =
70-
* client.PrepareQuery(instance, statement);
71-
* if (!prepared_query) throw std::move(prepared_query).status();
72-
*
73-
* auto bound_query = prepared_query->BindParameters(
74-
* {{"key", cbt::Value("row-key-2")}});
75-
*
76-
* RowStream results =
77-
* client.ExecuteQuery(std::move(bound_query));
78-
*
79-
* ... // process rows
80-
* }
81-
* @endcode
52+
* @snippet data_snippets.cc prepare-and-execute-query
8253
*/
8354
class Client {
8455
public:

google/cloud/bigtable/examples/bigtable_examples_common.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,24 @@ Commands::value_type MakeCommandEntry(std::string const& name,
129129
return Commands::value_type{name, std::move(adapter)};
130130
}
131131

132+
Commands::value_type MakeCommandEntry(std::string const& name,
133+
std::vector<std::string> const& args,
134+
ClientCommandType const& function) {
135+
auto command = [=](std::vector<std::string> argv) {
136+
auto constexpr kFixedArguments = 0;
137+
if ((argv.size() == 1 && argv[0] == "--help") ||
138+
argv.size() != args.size() + kFixedArguments) {
139+
std::ostringstream os;
140+
os << name;
141+
if (!args.empty()) os << " " << absl::StrJoin(args, " ");
142+
throw Usage{std::move(os).str()};
143+
}
144+
auto client = bigtable::Client(bigtable::MakeDataConnection());
145+
function(client, argv);
146+
};
147+
return {name, command};
148+
}
149+
132150
} // namespace examples
133151
} // namespace bigtable
134152
} // namespace cloud

google/cloud/bigtable/examples/bigtable_examples_common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "google/cloud/bigtable/admin/bigtable_instance_admin_client.h"
1919
#include "google/cloud/bigtable/admin/bigtable_table_admin_client.h"
20+
#include "google/cloud/bigtable/client.h"
2021
#include "google/cloud/bigtable/table.h"
2122
#include "google/cloud/internal/random.h"
2223
#include "google/cloud/testing_util/example_driver.h"
@@ -85,6 +86,13 @@ Commands::value_type MakeCommandEntry(std::string const& name,
8586
std::vector<std::string> const& args,
8687
TableAsyncCommandType const& command);
8788

89+
using ClientCommandType = std::function<void(google::cloud::bigtable::Client,
90+
std::vector<std::string>)>;
91+
92+
Commands::value_type MakeCommandEntry(std::string const& name,
93+
std::vector<std::string> const& args,
94+
ClientCommandType const& function);
95+
8896
} // namespace examples
8997
} // namespace bigtable
9098
} // namespace cloud

google/cloud/bigtable/examples/data_snippets.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "google/cloud/bigtable/testing/cleanup_stale_resources.h"
1919
#include "google/cloud/bigtable/testing/random_names.h"
2020
//! [bigtable includes]
21+
#include "google/cloud/bigtable/client.h"
2122
#include "google/cloud/bigtable/table.h"
2223
//! [bigtable includes]
2324
#include "google/cloud/internal/getenv.h"
@@ -778,6 +779,38 @@ void RunWriteExamples(
778779
admin.DeleteTable(schema->name());
779780
}
780781

782+
void PrepareAndExecuteQuery(google::cloud::bigtable::Client client,
783+
std::vector<std::string> const& args) {
784+
// [prepare-and-execute-query] [START bigtable_api_execute_query]
785+
namespace cbt = ::google::cloud::bigtable;
786+
[](cbt::Client client, std::string const& project_id,
787+
std::string const& instance_id, std::string const& table_id) {
788+
cbt::InstanceResource instance(google::cloud::Project(project_id),
789+
instance_id);
790+
cbt::SqlStatement statement(
791+
"SELECT _key, CAST(fam['column0'] AS STRING) as value0 FROM `" +
792+
table_id + "` WHERE _key=@key",
793+
{{"key", cbt::Parameter(cbt::Bytes())}});
794+
795+
auto prepared_query = client.PrepareQuery(instance, statement);
796+
if (!prepared_query) throw std::move(prepared_query).status();
797+
798+
auto bound_query = prepared_query->BindParameters(
799+
{{"key", cbt::Value(cbt::Bytes("test-key-for-apply"))}});
800+
801+
auto results = client.ExecuteQuery(std::move(bound_query));
802+
803+
using RowType = std::tuple<cbt::Bytes, absl::optional<std::string>>;
804+
for (auto& row : cbt::StreamOf<RowType>(results)) {
805+
if (!row.ok()) throw std::move(row.status());
806+
auto v = std::get<1>(*row);
807+
std::cout << std::get<0>(*row) << "; " << (v ? *v : "null") << std::endl;
808+
}
809+
}
810+
// [prepare-and-execute-query] [END bigtable_api_execute_query]
811+
(std::move(client), args.at(0), args.at(1), args.at(2));
812+
}
813+
781814
void RunDataExamples(
782815
google::cloud::bigtable_admin::BigtableTableAdminClient admin,
783816
google::cloud::internal::DefaultPRNG& generator,
@@ -879,6 +912,11 @@ void RunDataExamples(
879912
std::cout << "Running ReadModifyWrite() example [3]" << std::endl;
880913
ReadModifyWrite(table, {"read-modify-write"});
881914

915+
if (!google::cloud::bigtable::examples::UsingEmulator()) {
916+
auto client = cbt::Client(cbt::MakeDataConnection());
917+
std::cout << "Running PrepareAndExecuteQuery() example" << std::endl;
918+
PrepareAndExecuteQuery(client, {project_id, instance_id, table_id});
919+
}
882920
admin.DeleteTable(schema->name());
883921
}
884922

@@ -946,6 +984,9 @@ int main(int argc, char* argv[]) try {
946984
MakeCommandEntry("write-batch", {}, WriteBatch),
947985
MakeCommandEntry("write-increment", {}, WriteIncrement),
948986
MakeCommandEntry("write-conditional", {}, WriteConditionally),
987+
MakeCommandEntry("prepare-and-execute-query",
988+
{"<project_id>", "<instance_id>", "<table_id>"},
989+
PrepareAndExecuteQuery),
949990
{"auto", RunAll},
950991
};
951992

0 commit comments

Comments
 (0)