Hyper API for C++ 0.0.20746
Hyper client library for C++ applications
Loading...
Searching...
No Matches
read_and_print_data_from_existing_hyper_file.cpp
1
7#include <fstream>
9#include <iostream>
10#include <string>
11#include <unordered_set>
12
16static void copy(const std::string& sourcePath, const std::string& destinationPath) {
17 std::ifstream source(sourcePath, std::ios::binary);
18 std::ofstream destination(destinationPath, std::ios::binary);
19 destination << source.rdbuf();
20 source.close();
21 destination.close();
22}
23
24static void runReadAndPrintDataFromExistingHyperFile() {
25 std::cout << "EXAMPLE - Read data from an existing Hyper file" << std::endl;
26
27 // Path to a Hyper file containing all data inserted into "Extract"."Extract" table
28 // See "insert_data_into_single_table.cpp" for an example that works with the complete schema.
29 const std::string pathToSourceDatabase = "data/superstore_sample_denormalized.hyper";
30
31 // Make a copy of the superstore example Hyper file.
32 const std::string pathToDatabase = "data/superstore_sample_denormalized_read.hyper";
33 copy(pathToSourceDatabase, pathToDatabase);
34
35 // Starts the Hyper Process with telemetry enabled to send data to Tableau.
36 // To opt out, simply set telemetry=hyperapi::Telemetry::DoNotSendUsageDataToTableau.
37 {
39
40 // Connect to existing Hyper file "superstore_sample_denormalized_read.hyper".
41 {
42 hyperapi::Connection connection(hyper.getEndpoint(), pathToDatabase);
43 const hyperapi::Catalog& catalog = connection.getCatalog();
44
45 // The table names in the "Extract" schema.
46 std::unordered_set<hyperapi::TableName> tableNames = catalog.getTableNames("Extract");
47 for (auto& tableName : tableNames) {
48 hyperapi::TableDefinition tableDefinition = catalog.getTableDefinition(tableName);
49 std::cout << "Table " << tableName << " has qualified name: " << tableDefinition.getTableName() << std::endl;
50 for (auto& column : tableDefinition.getColumns()) {
51 std::cout << "\t Column " << column.getName() << " has type " << column.getType() << " and nullability " << column.getNullability()
52 << std::endl;
53 }
54 std::cout << std::endl;
55 }
56
57 // Print all rows from the "Extract"."Extract" table.
58 hyperapi::TableName extractTable("Extract", "Extract");
59 std::cout << "These are all rows in the table " << extractTable.toString() << ":" << std::endl;
60
61 hyperapi::Result rowsInTable = connection.executeQuery("SELECT * FROM " + extractTable.toString());
62 for (const hyperapi::Row& row : rowsInTable) {
63 for (const hyperapi::Value& value : row) {
64 std::cout << value << '\t';
65 }
66 std::cout << '\n';
67 }
68 }
69 std::cout << "The connection to the Hyper file has been closed." << std::endl;
70 }
71 std::cout << "The Hyper Process has been shut down." << std::endl;
72}
73
74int main() {
75 try {
76 runReadAndPrintDataFromExistingHyperFile();
77 } catch (const hyperapi::HyperException& e) {
78 std::cout << e.toString() << std::endl;
79 return 1;
80 }
81 return 0;
82}
The catalog class gives access to the metadata of the attached databases of a connection.
Definition Catalog.hpp:31
std::unordered_set< TableName > getTableNames(const SchemaName &schema) const
Gets the names of all tables in the given schema.
TableDefinition getTableDefinition(const TableName &tableName) const
Gets the table definition for an existing table.
Defines a Hyper connection.
Defines an exception object that is thrown on failure by the functions in the Hyper API C++ library.
std::string toString() const
Returns a formatted string containing the message and hint of the error and all causes.
Defines a Hyper process.
Base class for a result of a query.
Definition Result.hpp:326
A Row inside a chunk.
Definition Result.hpp:210
const TableName & getTableName() const noexcept
Returns the name of the table.
Represents an escaped SQL table name.
Definition TableName.hpp:15
const Name & getName() const noexcept
Definition TableName.hpp:39
A value inside a row.
Definition Result.hpp:44
The main header of the Hyper API for C++.
@ SendUsageDataToTableau
Telemetry data will be sent to tableau to help improve the Hyper API.