Hyper API for C++ 0.0.20746
Hyper client library for C++ applications
Loading...
Searching...
No Matches
create_hyper_file_from_csv.cpp
1
8#include <iostream>
9#include <string>
10
11static const hyperapi::TableDefinition customerTable{
12 "Customer", // Since the table name is not prefixed with an explicit schema name, the table will reside in the default "public" namespace.
17
18// An example demonstrating loading data from a csv into a new Hyper file
19// For more details, see https://tableau.github.io/hyper-db/docs/guides/hyper_file/insert_csv
20static void runCreateHyperFileFromCSV() {
21 std::cout << "EXAMPLE - Load data from CSV into table in new Hyper file" << std::endl;
22 const std::string pathToDatabase = "data/customer.hyper";
23
24 // Starts the Hyper Process with telemetry enabled to send data to Tableau.
25 // To opt out, simply set telemetry=hyperapi::Telemetry::DoNotSendUsageDataToTableau.
26 {
27 // Optional process parameters. They are documented in the Tableau Hyper documentation, chapter "Process Settings"
28 // (https://tableau.github.io/hyper-db/docs/hyper-api/hyper_process#process-settings).
29 std::unordered_map<std::string, std::string> processParameters = {
30 // Limits the number of Hyper event log files to two.
31 {"log_file_max_count", "2"},
32 // Limits the size of Hyper event log files to 100 megabytes.
33 {"log_file_size_limit", "100M"}};
34
36 // Creates new Hyper file "customer.hyper".
37 // Replaces existing file with hyperapi::CreateMode::CreateAndReplace if it already exists.
38 {
39 // Optional connection parameters. They are documented in the Tableau Hyper documentation, chapter "Connection Settings"
40 // (https://tableau.github.io/hyper-db/docs/hyper-api/connection#connection-settings).
41 std::unordered_map<std::string, std::string> connectionParameters = {{"lc_time", "en_US"}};
42
43 hyperapi::Connection connection(hyper.getEndpoint(), pathToDatabase, hyperapi::CreateMode::CreateAndReplace, connectionParameters);
44 const hyperapi::Catalog& catalog = connection.getCatalog();
45
46 catalog.createTable(customerTable);
47
48 // Using path to current file, create a path that locates CSV file packaged with these examples.
49 std::string pathToCSV = "data/customers.csv";
50
51 // Load all rows into "Customers" table from the CSV file.
52 // `executeCommand` executes a SQL statement and returns the impacted row count.
53 //
54 // Note:
55 // You might have to adjust the COPY parameters to the format of your specific csv file.
56 // The example assumes that your columns are separated with the ',' character
57 // and that NULL values are encoded via the string 'NULL'.
58 // Also be aware that the `header` option is used in this example:
59 // It treats the first line of the csv file as a header and does not import it.
60 //
61 // The parameters of the COPY command are documented in the Tableau Hyper SQL documentation
62 // (https://tableau.github.io/hyper-db/docs/sql/command/copy_from).
63 std::cout << "Issuing the SQL COPY command to load the csv file into the table. Since the first line" << std::endl;
64 std::cout << "of our csv file contains the column names, we use the `header` option to skip it." << std::endl;
65 int64_t rowCount = connection.executeCommand(
66 "COPY " + customerTable.getTableName().toString() + " from " + hyperapi::escapeStringLiteral(pathToCSV) +
67 " with (format csv, NULL 'NULL', delimiter ',', header)");
68
69 std::cout << "The number of rows in table " << customerTable.getTableName() << " is " << rowCount << "." << std::endl;
70 }
71 std::cout << "The connection to the Hyper file has been closed." << std::endl;
72 }
73 std::cout << "The Hyper Process has been shut down." << std::endl;
74}
75
76int main() {
77 try {
78 runCreateHyperFileFromCSV();
79 } catch (const hyperapi::HyperException& e) {
80 std::cout << e.toString() << std::endl;
81 return 1;
82 }
83 return 0;
84}
The catalog class gives access to the metadata of the attached databases of a connection.
Definition Catalog.hpp:31
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.
static SqlType bigInt() noexcept
Returns the BIG INTEGER SQL type.
Definition SqlType.hpp:101
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 escapeStringLiteral(string_view input)
Escapes the given string for safe usage in SQL query or command strings as a string literal.