|
18 | 18 | #include "google/cloud/bigtable/testing/cleanup_stale_resources.h" |
19 | 19 | #include "google/cloud/bigtable/testing/random_names.h" |
20 | 20 | //! [bigtable includes] |
| 21 | +#include "google/cloud/bigtable/client.h" |
21 | 22 | #include "google/cloud/bigtable/table.h" |
22 | 23 | //! [bigtable includes] |
23 | 24 | #include "google/cloud/internal/getenv.h" |
@@ -778,6 +779,38 @@ void RunWriteExamples( |
778 | 779 | admin.DeleteTable(schema->name()); |
779 | 780 | } |
780 | 781 |
|
| 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 | + |
781 | 814 | void RunDataExamples( |
782 | 815 | google::cloud::bigtable_admin::BigtableTableAdminClient admin, |
783 | 816 | google::cloud::internal::DefaultPRNG& generator, |
@@ -879,6 +912,11 @@ void RunDataExamples( |
879 | 912 | std::cout << "Running ReadModifyWrite() example [3]" << std::endl; |
880 | 913 | ReadModifyWrite(table, {"read-modify-write"}); |
881 | 914 |
|
| 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 | + } |
882 | 920 | admin.DeleteTable(schema->name()); |
883 | 921 | } |
884 | 922 |
|
@@ -946,6 +984,9 @@ int main(int argc, char* argv[]) try { |
946 | 984 | MakeCommandEntry("write-batch", {}, WriteBatch), |
947 | 985 | MakeCommandEntry("write-increment", {}, WriteIncrement), |
948 | 986 | MakeCommandEntry("write-conditional", {}, WriteConditionally), |
| 987 | + MakeCommandEntry("prepare-and-execute-query", |
| 988 | + {"<project_id>", "<instance_id>", "<table_id>"}, |
| 989 | + PrepareAndExecuteQuery), |
949 | 990 | {"auto", RunAll}, |
950 | 991 | }; |
951 | 992 |
|
|
0 commit comments