Skip to content

Commit

Permalink
adding files
Browse files Browse the repository at this point in the history
  • Loading branch information
tsjoblad committed Apr 29, 2020
1 parent 13518bd commit dbd2c52
Show file tree
Hide file tree
Showing 79 changed files with 195,058 additions and 0 deletions.
147 changes: 147 additions & 0 deletions CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# -----------------------------------------------------------------------------
#
# This file is the copyrighted property of Tableau Software and is protected
# by registered patents and other applicable U.S. and international laws and
# regulations.
#
# Unlicensed use of the contents of this file is prohibited. Please refer to
# the NOTICES.txt file for further details.
#
# -----------------------------------------------------------------------------

# This is an example CMake project that builds and tests the included C++ example code.
#
# To configure this project, run
#
# cmake <optional CMake arguments> -S "<directory of this file>"
#
# You can optionally override the directory where the Tableau Hyper API for C++ is searched by specifying
#
# -D CMAKE_PREFIX_PATH:PATH="<directory of Tableau Hyper API for C++>/share/cmake"
#
# By default, the `hyperd` executable is searched in the `bin/hyper/` directory of the `tableauhyperapi-cxx` package.
# You can override the directory by specifying
#
# -D HYPER_PATH:PATH="<directory of hyperd>"
#
# To build and test the examples, run
#
# cmake --build .
# ctest

cmake_minimum_required(VERSION 3.1)

# If CMAKE_PREFIX_PATH is not specified, provide a convenient default, so that the `find_package()` below can find
# the Hyper API. This default assumes this `CMakeLists.txt` to be in its original location under `examples/`.
if (NOT DEFINED CMAKE_PREFIX_PATH)
get_filename_component(HAPI_PACKAGE_LOCATION "${CMAKE_SOURCE_DIR}/.." REALPATH)
set(CMAKE_PREFIX_PATH "${HAPI_PACKAGE_LOCATION}/share/cmake" CACHE PATH "CMake prefix path" FORCE)
endif ()

project("Tableau Hyper API for C++ Examples" LANGUAGES CXX)

find_package(tableauhyperapi-cxx REQUIRED CONFIG)

# Determine some directories in the `tableauhyperapi-c` package for use below.
get_filename_component(tableauhyperapi-c_BINARY_DIR "${tableauhyperapi-c_DIR}/../../bin" ABSOLUTE)
get_filename_component(tableauhyperapi-c_LIBRARY_DIR "${tableauhyperapi-c_DIR}/../../lib" ABSOLUTE)
if (WIN32)
set(tableauhyperapi-c_DYLIB_DIR "${tableauhyperapi-c_BINARY_DIR}")
else ()
set(tableauhyperapi-c_DYLIB_DIR "${tableauhyperapi-c_LIBRARY_DIR}")
endif ()

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

enable_testing()

# Copy over the superstore CSV dataset.
file(COPY "${CMAKE_SOURCE_DIR}/data/sample_extracts/" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/data")
file(COPY "${CMAKE_SOURCE_DIR}/data/superstore_normalized/" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/data")
if (WIN32)
# On Windows, the Hyper API is copied into the binary directory so the examples can pick it up during execution.
# On Posix systems, the Hyper API path is already compiled into the RPATH of the examples.
file(COPY "${tableauhyperapi-c_DYLIB_DIR}/" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
endif ()

# -----------------------------------------------------------------------------
# `create_hyper_file_from_csv.cpp`

add_executable(create_hyper_file_from_csv create_hyper_file_from_csv.cpp)
target_link_libraries(create_hyper_file_from_csv PRIVATE Tableau::tableauhyperapi-cxx)
add_test(
NAME create_hyper_file_from_csv
COMMAND ${CMAKE_COMMAND} -E env HYPER_PATH=${HYPER_PATH} $<TARGET_FILE:create_hyper_file_from_csv>
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

# -----------------------------------------------------------------------------
# `delete_data_in_existing_hyper_file.cpp`

add_executable(delete_data_in_existing_hyper_file delete_data_in_existing_hyper_file.cpp)
target_link_libraries(delete_data_in_existing_hyper_file PRIVATE Tableau::tableauhyperapi-cxx)
add_test(
NAME delete_data_in_existing_hyper_file
COMMAND ${CMAKE_COMMAND} -E env HYPER_PATH=${HYPER_PATH} $<TARGET_FILE:delete_data_in_existing_hyper_file>
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

# -----------------------------------------------------------------------------
# `insert_data_into_multiple_tables.cpp`

add_executable(insert_data_into_multiple_tables insert_data_into_multiple_tables.cpp)
target_link_libraries(insert_data_into_multiple_tables PRIVATE Tableau::tableauhyperapi-cxx)
add_test(
NAME insert_data_into_multiple_tables
COMMAND ${CMAKE_COMMAND} -E env HYPER_PATH=${HYPER_PATH} $<TARGET_FILE:insert_data_into_multiple_tables>
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

# -----------------------------------------------------------------------------
# `insert_data_into_single_table.cpp`

add_executable(insert_data_into_single_table insert_data_into_single_table.cpp)
target_link_libraries(insert_data_into_single_table PRIVATE Tableau::tableauhyperapi-cxx)
add_test(
NAME insert_data_into_single_table
COMMAND ${CMAKE_COMMAND} -E env HYPER_PATH=${HYPER_PATH} $<TARGET_FILE:insert_data_into_single_table>
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

# -----------------------------------------------------------------------------
# `insert_data_into_single_table.cpp`

add_executable(insert_data_with_expressions insert_data_with_expressions.cpp)
target_link_libraries(insert_data_with_expressions PRIVATE Tableau::tableauhyperapi-cxx)
add_test(
NAME insert_data_with_expressions
COMMAND ${CMAKE_COMMAND} -E env HYPER_PATH=${HYPER_PATH} $<TARGET_FILE:insert_data_with_expressions>
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

# -----------------------------------------------------------------------------
# `insert_spatial_data_to_a_hyper_file.cpp`

add_executable(insert_spatial_data_to_a_hyper_file insert_spatial_data_to_a_hyper_file.cpp)
target_link_libraries(insert_spatial_data_to_a_hyper_file PRIVATE Tableau::tableauhyperapi-cxx)
add_test(
NAME insert_spatial_data_to_a_hyper_file
COMMAND ${CMAKE_COMMAND} -E env HYPER_PATH=${HYPER_PATH} $<TARGET_FILE:insert_spatial_data_to_a_hyper_file>
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

# -----------------------------------------------------------------------------
# `read_and_print_data_from_existing_hyper_file.cpp`

add_executable(read_and_print_data_from_existing_hyper_file read_and_print_data_from_existing_hyper_file.cpp)
target_link_libraries(read_and_print_data_from_existing_hyper_file PRIVATE Tableau::tableauhyperapi-cxx)
add_test(
NAME read_and_print_data_from_existing_hyper_file
COMMAND ${CMAKE_COMMAND} -E env HYPER_PATH=${HYPER_PATH} $<TARGET_FILE:read_and_print_data_from_existing_hyper_file>
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

# -----------------------------------------------------------------------------
# `update_data_in_existing_hyper_file.cpp`

add_executable(update_data_in_existing_hyper_file update_data_in_existing_hyper_file.cpp)
target_link_libraries(update_data_in_existing_hyper_file PRIVATE Tableau::tableauhyperapi-cxx)
add_test(
NAME update_data_in_existing_hyper_file
COMMAND ${CMAKE_COMMAND} -E env HYPER_PATH=${HYPER_PATH} $<TARGET_FILE:update_data_in_existing_hyper_file>
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

81 changes: 81 additions & 0 deletions CPP/create_hyper_file_from_csv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// -----------------------------------------------------------------------------
//
// This file is the copyrighted property of Tableau Software and is protected
// by registered patents and other applicable U.S. and international laws and
// regulations.
//
// You may adapt this file and modify it to fit into your context and use it
// as a template to start your own projects.
//
// -----------------------------------------------------------------------------

/**
* \example create_hyper_file_from_csv.cpp
*
* An example of how to load data from a CSV file into a new Hyper file.
*/

#include <hyperapi/hyperapi.hpp>
#include <iostream>
#include <string>

static const hyperapi::TableDefinition customerTable{
"Customer", // Since the table name is not prefixed with an explicit schema name, the table will reside in the default "public" namespace.
{hyperapi::TableDefinition::Column{"Customer ID", hyperapi::SqlType::text(), hyperapi::Nullability::NotNullable},
hyperapi::TableDefinition::Column{"Customer Name", hyperapi::SqlType::text(), hyperapi::Nullability::NotNullable},
hyperapi::TableDefinition::Column{"Loyalty Reward Points", hyperapi::SqlType::bigInt(), hyperapi::Nullability::NotNullable},
hyperapi::TableDefinition::Column{"Segment", hyperapi::SqlType::text(), hyperapi::Nullability::NotNullable}}};

static void runCreateHyperFileFromCSV() {
std::cout << "EXAMPLE - Load data from CSV into table in new Hyper file" << std::endl;
const std::string pathToDatabase = "data/customer.hyper";

// Starts the Hyper Process with telemetry enabled to send data to Tableau.
// To opt out, simply set telemetry=hyperapi::Telemetry::DoNotSendUsageDataToTableau.
{
// Optional process parameters. They are documented in the Tableau Hyper documentation, chapter "Process Settings"
// (https://help.tableau.com/current/api/hyper_api/en-us/reference/sql/processsettings.html).
std::unordered_map<std::string, std::string> processParameters = {
// Limits the number of Hyper event log files to two.
{"log_file_max_count", "2"},
// Limits the size of Hyper event log files to 100 megabytes.
{"log_file_size_limit", "100M"}};

hyperapi::HyperProcess hyper(hyperapi::Telemetry::SendUsageDataToTableau, "example", processParameters);
// Creates new Hyper file "customer.hyper".
// Replaces existing file with hyperapi::CreateMode::CreateAndReplace if it already exists.
{
// Optional connection parameters. They are documented in the Tableau Hyper documentation, chapter "Connection Settings"
// (https://help.tableau.com/current/api/hyper_api/en-us/reference/sql/connectionsettings.html).
std::unordered_map<std::string, std::string> connectionParameters = {{"lc_time", "en_US"}};

hyperapi::Connection connection(hyper.getEndpoint(), pathToDatabase, hyperapi::CreateMode::CreateAndReplace, connectionParameters);
const hyperapi::Catalog& catalog = connection.getCatalog();

catalog.createTable(customerTable);

// Using path to current file, create a path that locates CSV file packaged with these examples.
std::string pathToCSV = "data/customers.csv";

// Load all rows into "Customers" table from the CSV file.
// `executeCommand` executes a SQL statement and returns the impacted row count.
int64_t rowCount = connection.executeCommand(
"COPY " + customerTable.getTableName().toString() + " from " + hyperapi::escapeStringLiteral(pathToCSV) +
" with (format csv, NULL 'NULL', delimiter ',', header)");

std::cout << "The number of rows in table " << customerTable.getTableName() << " is " << rowCount << "." << std::endl;
}
std::cout << "The connection to the Hyper file has been closed." << std::endl;
}
std::cout << "The Hyper Process has been shut down." << std::endl;
}

int main() {
try {
runCreateHyperFileFromCSV();
} catch (const hyperapi::HyperException& e) {
std::cout << e.toString() << std::endl;
return 1;
}
return 0;
}
Loading

0 comments on commit dbd2c52

Please sign in to comment.