From 2171de7b59c5262acd0928996935803e5e21bd46 Mon Sep 17 00:00:00 2001 From: Scott Hart Date: Fri, 28 Jun 2024 17:56:15 -0400 Subject: [PATCH] add quickstart --- .../cloud/bigquery/quickstart/CMakeLists.txt | 7 +++ google/cloud/bigquery/quickstart/Makefile | 8 ++++ .../bigquery/quickstart/quickstart_job.cc | 47 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 google/cloud/bigquery/quickstart/quickstart_job.cc diff --git a/google/cloud/bigquery/quickstart/CMakeLists.txt b/google/cloud/bigquery/quickstart/CMakeLists.txt index 7d2f85070ece2..75ce489c32643 100644 --- a/google/cloud/bigquery/quickstart/CMakeLists.txt +++ b/google/cloud/bigquery/quickstart/CMakeLists.txt @@ -30,3 +30,10 @@ endif () # Once the bigquery_client package is found, define new targets. add_executable(quickstart quickstart.cc) target_link_libraries(quickstart google-cloud-cpp::bigquery) + +find_package(google_cloud_cpp_bigquery_job) +if (google_cloud_cpp_bigquery_job_FOUND) + add_executable(quickstart_job quickstart_job.cc) + target_link_libraries(quickstart_job + google-cloud-cpp::experimental-bigquery_job) +endif () diff --git a/google/cloud/bigquery/quickstart/Makefile b/google/cloud/bigquery/quickstart/Makefile index 636af4633ae7b..d2787d8176df9 100644 --- a/google/cloud/bigquery/quickstart/Makefile +++ b/google/cloud/bigquery/quickstart/Makefile @@ -34,3 +34,11 @@ BIGQUERY_LIBS := $(shell pkg-config $(BIGQUERY_DEPS) --libs-only-l) # A target using the Cloud BigQuery C++ client library. $(BIN)/quickstart: quickstart.cc $(CXXLD) $(CXXFLAGS) $(BIGQUERY_CXXFLAGS) $(BIGQUERY_CXXLDFLAGS) -o $@ $^ $(BIGQUERY_LIBS) + +BIGQUERY_JOB_DEPS := google_cloud_cpp_bigquery_job +BIGQUERY_JOB_CXXFLAGS := $(shell pkg-config $(BIGQUERY_JOB_DEPS) --cflags) +BIGQUERY_JOB_CXXLDFLAGS := $(shell pkg-config $(BIGQUERY_JOB_DEPS) --libs-only-L) +BIGQUERY_JOB_LIBS := $(shell pkg-config $(BIGQUERY_JOB_DEPS) --libs-only-l) + +$(BIN)/quickstart_job: quickstart_job.cc + $(CXXLD) $(CXXFLAGS) $(BIGQUERY_JOB_CXXFLAGS) $(BIGQUERY_JOB_CXXLDFLAGS) -o $@ $^ $(BIGQUERY_JOB_LIBS) diff --git a/google/cloud/bigquery/quickstart/quickstart_job.cc b/google/cloud/bigquery/quickstart/quickstart_job.cc new file mode 100644 index 0000000000000..635b21c4ad48c --- /dev/null +++ b/google/cloud/bigquery/quickstart/quickstart_job.cc @@ -0,0 +1,47 @@ +// Copyright 2024 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 +// +// https://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 bigqueryjob_quickstart] [all] +#include "google/cloud/bigquery/job/v2/job_client.h" +#include + +namespace {} // namespace + +int main(int argc, char* argv[]) try { + if (argc != 2) { + std::cerr << "Usage: " << argv[0] << " \n"; + return 1; + } + + std::string const project_id = std::string(argv[1]); + + namespace bigquery_proto = google::cloud::bigquery::v2; + namespace bigquery_job = google::cloud::bigquery_job_v2; + auto client = bigquery_job::JobServiceClient( + bigquery_job::MakeJobServiceConnectionRest()); + + bigquery_proto::ListJobsRequest list_request; + list_request.set_project_id(project_id); + std::vector job_ids; + for (auto job : client.ListJobs(list_request)) { + if (!job) throw std::move(job).status(); + std::cout << job->DebugString() << "\n"; + } + + return 0; +} catch (google::cloud::Status const& status) { + std::cerr << "google::cloud::Status thrown: " << status << "\n"; + return 1; +} +//! [END bigqueryjob_quickstart] [all]