|
| 1 | +# Import necessary standard libraries |
| 2 | +from pathlib import Path # For file path manipulations |
| 3 | + |
| 4 | +# Import pandas for DataFrame creation and manipulation |
| 5 | +import pandas as pd |
| 6 | + |
| 7 | +# Import necessary classes from the Tableau Hyper API |
| 8 | +from tableauhyperapi import ( |
| 9 | + HyperProcess, |
| 10 | + Telemetry, |
| 11 | + Connection, |
| 12 | + CreateMode, |
| 13 | + NOT_NULLABLE, |
| 14 | + NULLABLE, |
| 15 | + SqlType, |
| 16 | + TableDefinition, |
| 17 | + Inserter, |
| 18 | + HyperException, |
| 19 | +) |
| 20 | + |
| 21 | +def run_create_hyper_file_from_dataframe(): |
| 22 | + """ |
| 23 | + An example demonstrating loading data from a pandas DataFrame into a new Hyper file. |
| 24 | + """ |
| 25 | + |
| 26 | + print("EXAMPLE - Load data from pandas DataFrame into table in new Hyper file") |
| 27 | + |
| 28 | + # Step 1: Create a sample pandas DataFrame. |
| 29 | + data = { |
| 30 | + "Customer ID": ["DK-13375", "EB-13705", "JH-13600"], |
| 31 | + "Customer Name": ["John Doe", "Jane Smith", "Alice Johnson"], |
| 32 | + "Loyalty Reward Points": [100, 200, 300], |
| 33 | + "Segment": ["Consumer", "Corporate", "Home Office"], |
| 34 | + } |
| 35 | + df = pd.DataFrame(data) |
| 36 | + |
| 37 | + # Step 2: Define the path where the Hyper file will be saved. |
| 38 | + path_to_database = Path("customer.hyper") |
| 39 | + |
| 40 | + # Step 3: Optional process parameters. |
| 41 | + # These settings limit the number of log files and their size. |
| 42 | + process_parameters = { |
| 43 | + "log_file_max_count": "2", # Limit the number of log files to 2 |
| 44 | + "log_file_size_limit": "100M", # Limit the log file size to 100 megabytes |
| 45 | + } |
| 46 | + |
| 47 | + # Step 4: Start the Hyper Process. |
| 48 | + # Telemetry is set to send usage data to Tableau. |
| 49 | + with HyperProcess( |
| 50 | + telemetry=Telemetry.SEND_USAGE_DATA_TO_TABLEAU, parameters=process_parameters |
| 51 | + ) as hyper: |
| 52 | + |
| 53 | + # Step 5: Optional connection parameters. |
| 54 | + # This sets the locale for time formats to 'en_US'. |
| 55 | + connection_parameters = {"lc_time": "en_US"} |
| 56 | + |
| 57 | + # Step 6: Create a connection to the Hyper file. |
| 58 | + # If the file exists, it will be replaced. |
| 59 | + with Connection( |
| 60 | + endpoint=hyper.endpoint, |
| 61 | + database=path_to_database, |
| 62 | + create_mode=CreateMode.CREATE_AND_REPLACE, |
| 63 | + parameters=connection_parameters, |
| 64 | + ) as connection: |
| 65 | + |
| 66 | + # Step 7: Define the table schema. |
| 67 | + customer_table = TableDefinition( |
| 68 | + table_name="Customer", # Name of the table |
| 69 | + columns=[ |
| 70 | + TableDefinition.Column( |
| 71 | + "Customer ID", SqlType.text(), NOT_NULLABLE |
| 72 | + ), |
| 73 | + TableDefinition.Column( |
| 74 | + "Customer Name", SqlType.text(), NOT_NULLABLE |
| 75 | + ), |
| 76 | + TableDefinition.Column( |
| 77 | + "Loyalty Reward Points", SqlType.big_int(), NOT_NULLABLE |
| 78 | + ), |
| 79 | + TableDefinition.Column("Segment", SqlType.text(), NOT_NULLABLE), |
| 80 | + ], |
| 81 | + ) |
| 82 | + |
| 83 | + # Step 8: Create the table in the Hyper file. |
| 84 | + connection.catalog.create_table(table_definition=customer_table) |
| 85 | + |
| 86 | + # Step 9: Use the Inserter to insert data into the table. |
| 87 | + with Inserter(connection, customer_table) as inserter: |
| 88 | + # Iterate over the DataFrame rows as tuples. |
| 89 | + # 'itertuples' returns an iterator yielding named tuples. |
| 90 | + for row in df.itertuples(index=False, name=None): |
| 91 | + inserter.add_row(row) # Add each row to the inserter |
| 92 | + inserter.execute() # Execute the insertion into the Hyper file |
| 93 | + |
| 94 | + # Step 10: Verify the number of rows inserted. |
| 95 | + row_count = connection.execute_scalar_query( |
| 96 | + f"SELECT COUNT(*) FROM {customer_table.table_name}" |
| 97 | + ) |
| 98 | + print( |
| 99 | + f"The number of rows in table {customer_table.table_name} is {row_count}." |
| 100 | + ) |
| 101 | + print("Data has been successfully inserted into the Hyper file.") |
| 102 | + |
| 103 | + # The connection is automatically closed when exiting the 'with' block. |
| 104 | + print("The connection to the Hyper file has been closed.") |
| 105 | + |
| 106 | + # The Hyper process is automatically shut down when exiting the 'with' block. |
| 107 | + print("The Hyper process has been shut down.") |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + try: |
| 112 | + run_create_hyper_file_from_dataframe() |
| 113 | + except HyperException as ex: |
| 114 | + print(ex) |
| 115 | + exit(1) |
0 commit comments