|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# This file is the copyrighted property of Tableau Software and is protected |
| 4 | +# by registered patents and other applicable U.S. and international laws and |
| 5 | +# regulations. |
| 6 | +# |
| 7 | +# You may adapt this file and modify it to fit into your context and use it |
| 8 | +# as a template to start your own projects. |
| 9 | +# |
| 10 | +# ----------------------------------------------------------------------------- |
| 11 | +from tableauhyperapi import HyperProcess, Telemetry, \ |
| 12 | + Connection, CreateMode, \ |
| 13 | + HyperException |
| 14 | + |
| 15 | +def print_list(l): |
| 16 | + for e in l: |
| 17 | + print(e) |
| 18 | + |
| 19 | + |
| 20 | +def run_hyper_query_external(): |
| 21 | + """ |
| 22 | + An example demonstrating how to use Hyper to read data directly from external sources. |
| 23 | + |
| 24 | + More information can be found here: |
| 25 | + https://help.tableau.com/current/api/hyper_api/en-us/reference/sql/external-data-in-sql.html |
| 26 | + https://help.tableau.com/current/api/hyper_api/en-us/reference/sql/sql-copy.html |
| 27 | + https://help.tableau.com/current/api/hyper_api/en-us/reference/sql/sql-createexternaltable.html |
| 28 | + https://help.tableau.com/current/api/hyper_api/en-us/reference/sql/functions-srf.html#FUNCTIONS-SRF-EXTERNAL |
| 29 | + """ |
| 30 | + |
| 31 | + # Start the Hyper process. |
| 32 | + with HyperProcess(telemetry=Telemetry.SEND_USAGE_DATA_TO_TABLEAU) as hyper: |
| 33 | + # Open a connection to the Hyper process. This will also create the new Hyper file. |
| 34 | + # The `CREATE_AND_REPLACE` mode causes the file to be replaced if it |
| 35 | + # already exists. |
| 36 | + with Connection(endpoint=hyper.endpoint, |
| 37 | + database="output_file.hyper", |
| 38 | + create_mode=CreateMode.CREATE_AND_REPLACE) as connection: |
| 39 | + |
| 40 | + print("Scenario 1: Create a table from filtered parquet data with a calculated extra column") |
| 41 | + # This SQL command queries a parquet file directly and creates the table 'low_prio_orders' in Hyper. |
| 42 | + # The created table contains the data that is returned from the 'SELECT' part of the query. I.e., only |
| 43 | + # a selection of columns, a new calculated column 'employee_nr' and only the rows with low order priority. |
| 44 | + command_1 = """CREATE TABLE low_prio_orders AS |
| 45 | + SELECT order_key, customer_key, price, CAST(SUBSTRING(employee from 0 for 6) AS int) as employee_nr |
| 46 | + FROM external('orders.parquet') |
| 47 | + WHERE priority = 'LOW'""" |
| 48 | + |
| 49 | + connection.execute_command(command_1) |
| 50 | + |
| 51 | + print("table content:") |
| 52 | + print_list(connection.execute_list_query("SELECT * FROM low_prio_orders")) |
| 53 | + print() |
| 54 | + |
| 55 | + print("\nScenario 2: Query multiple external data sources in one query.") |
| 56 | + # This query reads data from a parquet and a CSV file and joins it. Note that, for CSV files, the schema of the file |
| 57 | + # has to be provided and currently cannot be inferred form the file directly (see the `DESCRIPTOR` argument below). |
| 58 | + command_2 = """SELECT country, SUM(quantity * price) |
| 59 | + FROM external('orders.parquet') orders |
| 60 | + join external('customers.csv', |
| 61 | + COLUMNS => DESCRIPTOR(customer_key int, country text, street text, nr int), |
| 62 | + DELIMITER => ',', FORMAT => 'csv', HEADER => false) customers |
| 63 | + on orders.customer_key = customers.customer_key GROUP BY country |
| 64 | + ORDER BY country""" |
| 65 | + print("result:") |
| 66 | + print_list(connection.execute_list_query(command_2)) |
| 67 | + print() |
| 68 | + |
| 69 | + |
| 70 | + print("Scenario 3: Query multiple CSV files that have the same schema in one go.") |
| 71 | + # Note that, for CSV files, the schema of the file has to be provided and currently cannot be inferred form the file directly. |
| 72 | + # (see the `DESCRIPTOR` argument below). |
| 73 | + command_3 = """SELECT * |
| 74 | + FROM external(ARRAY['customers.csv','customers.csv'], |
| 75 | + COLUMNS => DESCRIPTOR(customer_key int, country text, street text, nr int), |
| 76 | + DELIMITER => ',', FORMAT => 'csv', HEADER => false) |
| 77 | + ORDER BY country""" |
| 78 | + |
| 79 | + print("result:") |
| 80 | + print_list(connection.execute_list_query(command_3)) |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + run_hyper_query_external() |
0 commit comments