MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/cpp-samples / main

Function main

bigquery/read/arrow/arrow_read.cc:118–182  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

116} // namespace
117
118int main(int argc, char* argv[]) try {
119 if (argc != 4) {
120 std::cerr << "Usage: " << argv[0]
121 << " <project-id> <dataset-name> <table-name>\n";
122 return 1;
123 }
124
125 std::string const project_id = argv[1];
126 std::string const dataset_name = argv[2];
127 std::string const table_name = argv[3];
128
129 std::string const table_id = "projects/" + project_id + "/datasets/" +
130 dataset_name + "/tables/" + table_name;
131
132 // Create a namespace alias to make the code easier to read.
133 namespace bigquery_storage = ::google::cloud::bigquery_storage_v1;
134 constexpr int kMaxReadStreams = 1;
135 // Create the ReadSession.
136 auto client = bigquery_storage::BigQueryReadClient(
137 bigquery_storage::MakeBigQueryReadConnection());
138 ::google::cloud::bigquery::storage::v1::ReadSession read_session;
139 read_session.set_data_format(
140 google::cloud::bigquery::storage::v1::DataFormat::ARROW);
141 read_session.set_table(table_id);
142 auto session =
143 client.CreateReadSession(google::cloud::Project(project_id).FullName(),
144 read_session, kMaxReadStreams);
145 if (!session) throw std::move(session).status();
146
147 // Get schema.
148 std::shared_ptr<arrow::Schema> schema =
149 GetArrowSchema(session->arrow_schema());
150
151 // Read rows from the ReadSession.
152 constexpr int kRowOffset = 0;
153 auto read_rows = client.ReadRows(session->streams(0).name(), kRowOffset);
154
155 std::int64_t num_rows = 0;
156 std::int64_t record_batch_count = 0;
157 for (auto const& read_rows_response : read_rows) {
158 if (read_rows_response.ok()) {
159 std::shared_ptr<arrow::RecordBatch> record_batch =
160 GetArrowRecordBatch(read_rows_response->arrow_record_batch(), schema);
161
162 if (record_batch_count == 0) {
163 PrintColumnNames(record_batch);
164 }
165
166 ProcessRecordBatch(schema, record_batch, num_rows);
167 num_rows += read_rows_response->row_count();
168 ++record_batch_count;
169 }
170 }
171
172 std::cout << std::format(
173 "Read {} record batch(es) and {} total row(s) from table: {}\n",
174 record_batch_count, num_rows, table_id);
175 return 0;

Callers

nothing calls this directly

Calls 4

GetArrowSchemaFunction · 0.85
GetArrowRecordBatchFunction · 0.85
PrintColumnNamesFunction · 0.85
ProcessRecordBatchFunction · 0.85

Tested by

no test coverage detected