Hyper API for C++ 0.0.20746
Hyper client library for C++ applications
Loading...
Searching...
No Matches
insert_spatial_data_to_a_hyper_file.cpp
1
8#include <iostream>
9#include <string>
10#include <unordered_set>
11
12// The table is called "Extract" and will be created in the "Extract" schema.
13// This has historically been the default table name and schema for extracts created by Tableau.
14static const hyperapi::TableDefinition extractTable{
15 {"Extract", "Extract"},
18
19static void runInsertSpatialDataToAHyperFile() {
20 std::cout << "EXAMPLE - Insert spatial data into a single table within a new Hyper file" << std::endl;
21 const std::string pathToDatabase = "data/spatial_data.hyper";
22
23 // Starts the Hyper Process with telemetry enabled to send data to Tableau.
24 // To opt out, simply set telemetry=hyperapi::Telemetry::DoNotSendUsageDataToTableau.
25 {
27 // Creates new Hyper file "spatial_data.hyper".
28 // Replaces existing file with hyperapi::CreateMode::CreateAndReplace if it already exists.
29 {
30 hyperapi::Connection connection(hyper.getEndpoint(), pathToDatabase, hyperapi::CreateMode::CreateAndReplace);
31 const hyperapi::Catalog& catalog = connection.getCatalog();
32
33 // Create the schema and the table.
34 catalog.createSchema("Extract");
35 catalog.createTable(extractTable);
36
37 // Hyper API's Inserter allows users to transform data during insertion.
38 // To make use of data transformation during insertion, the inserter requires the following inputs
39 // 1. The connection to the Hyper instance containing the table
40 // 2. The table name or table defintion into which data is inserted
41 // 3. List of hyperapi::Inserter::ColumnMapping.
42 // This list informs the inserter how each column in the target table is tranformed.
43 // The list must contain all the columns into which data is inserted.
44 // "hyperapi::Inserter::ColumnMapping" maps a valid SQL expression (if any) to a column in the target table
45 // For example hyperapi::Inserter::ColumnMapping("target_column", hyperapi::escapeName("colA") + "*" + hyperapi::escapeName("colB"))
46 // The column "target_column" contains the product of "colA" and "colB" after successful insertion.
47 // SQL expression string is optional in Inserter.ColumnMapping.
48 // For a column without any transformation only the column name is required.
49 // For example hyperapi::Inserter::ColumnMapping{"no_data_transformation_column"}
50 // 4. Inserter Definition, a list of column definitions for all the input values provided during insertion.
51
52 // Inserter definition contains the column definition for the values that are inserted.
53 // The data input has two text values Name and Location_as_text.
54 std::vector<hyperapi::TableDefinition::Column> inserterDefinition{
57
58 // Column 'Name' is inserted into "Extract"."Extract" as-is.
59 // Column 'Location' in "Extract"."Extract" of geography type is computed from Column 'Location_as_text' of text type
60 // using the expression 'CAST("Location_as_text") AS GEOGRAPHY'.
61 // hyperapi::Inserter::ColumnMapping is used for mapping the CAST expression to Column 'Location'.
62 std::string textToGeographyCastExpression = "CAST(" + hyperapi::escapeName("Location_as_text") + " AS GEOGRAPHY)";
63 std::vector<hyperapi::Inserter::ColumnMapping> columnMappings{
65 hyperapi::Inserter::ColumnMapping{"Location", textToGeographyCastExpression}};
66
67 // Insert spatial data into the "Extract"."Extract" table using CAST expression.
68 {
69 hyperapi::Inserter inserter(connection, extractTable, columnMappings, inserterDefinition);
70 inserter.addRow("Seattle", "point(-122.338083 47.647528)");
71 inserter.addRow("Munich", "point(11.584329 48.139257)");
72 inserter.execute();
73 }
74
75 // Number of rows in the "Extract"."Extract" table.
76 // `executeScalarQuery` is for executing a query that returns exactly one row with one column.
77 int64_t rowCount = connection.executeScalarQuery<int64_t>("SELECT COUNT(*) FROM " + extractTable.getTableName().toString());
78 std::cout << "The number of rows in table " << extractTable.getTableName() << " is " << rowCount << "." << std::endl;
79 }
80 std::cout << "The connection to the Hyper file has been closed." << std::endl;
81 }
82 std::cout << "The Hyper Process has been shut down." << std::endl;
83}
84
85int main() {
86 try {
87 runInsertSpatialDataToAHyperFile();
88 } catch (const hyperapi::HyperException& e) {
89 std::cout << e.toString() << std::endl;
90 return 1;
91 }
92 return 0;
93}
The catalog class gives access to the metadata of the attached databases of a connection.
Definition Catalog.hpp:31
void createSchema(const SchemaName &schemaName) const
Creates a SQL schema with the given name.
void createTable(const hyperapi::TableDefinition &table_definition) const
Creates a SQL table with the given table definition.
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.
Maps an expression to a column
Definition Inserter.hpp:27
An inserter.
Definition Inserter.hpp:24
static SqlType geography() noexcept
Returns the GEOGRAPHY SQL type.
Definition SqlType.hpp:190
static SqlType text() noexcept
Returns the TEXT SQL type.
Definition SqlType.hpp:143
A Column of a table definition.
const TableName & getTableName() const noexcept
Returns the name of the table.
std::string toString() const
The main header of the Hyper API for C++.
@ NotNullable
The column cannot contain NULL values.
@ SendUsageDataToTableau
Telemetry data will be sent to tableau to help improve the Hyper API.
@ CreateAndReplace
Create the database. If it already exists, drop the old one first.
std::string escapeName(string_view input)
Escapes the given string for safe usage in SQL query or command strings as an identifier.