Skip to content

Commit 37c21c9

Browse files
committed
Script to convert tde to hyper
A few changes to script
1 parent 6bfc6b3 commit 37c21c9

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# tde-to-hyper
2+
## __tde_to_hyper__
3+
4+
![Community Supported](https://img.shields.io/badge/Support%20Level-Community%20Supported-53bd92.svg)
5+
6+
__Current Version__: 1.0
7+
8+
This script can be used to create a `.hyper` file from a `.tde` file.
9+
10+
# Get started
11+
12+
## __Prerequisites__
13+
14+
To run the script, you will need:
15+
16+
- a computer running Windows, macOS, or Linux
17+
18+
- Python (3.7 or newer)
19+
20+
- Install the Hyper API `pip install -r requirements.txt`
21+
22+
## Run the sample
23+
24+
Ensure that you have installed the requirements and then just run the sample Python file.
25+
The following instructions assume that you have set up a virtual environment for Python. For more information on
26+
creating virtual environments, see [venv - Creation of virtual environments](https://docs.python.org/3/library/venv.html)
27+
in the Python Standard Library.
28+
29+
1. Open a terminal and activate the Python virtual environment (`venv`).
30+
31+
1. Navigate to the folder where you installed the sample.
32+
33+
1. Run the Python script:
34+
35+
**python tde_to_hyper.py input_tde_path**
36+
37+
The script requires a path to a tde file and will convert the tde to a `.hyper` file. The `.hyper` file will be created in the directory of the tde file.
38+
39+
Example:
40+
41+
```cli
42+
(venv)c:\mydir> python tde_to_hyper.py extract.tde
43+
Successfully converted extract.tde to extract.hyper
44+
45+
(venv)c:\mydir> python tde_to_hyper.py c:\path\to\file\extract.tde
46+
Successfully converted c:\path\to\file\extract.tde to c:\path\to\file\extract.hyper
47+
```
48+
49+
## __Resources__
50+
Check out these resources to learn more:
51+
52+
- [Hyper API docs](https://help.tableau.com/current/api/hyper_api/en-us/index.html)
53+
54+
- [Tableau Hyper API Reference (Python)](https://help.tableau.com/current/api/hyper_api/en-us/reference/py/index.html)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tableauhyperapi==0.0.14109
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import argparse
2+
from pathlib import Path
3+
from tableauhyperapi import HyperProcess, Telemetry, \
4+
Connection, CreateMode, TableDefinition, escape_name, \
5+
escape_string_literal
6+
7+
def convert_tde_to_hyper(tde_path: Path):
8+
# Rename path with a .hyper extension in the directory of the tde file
9+
hyper_database = tde_path.with_name(tde_path.stem + '.hyper')
10+
11+
with HyperProcess(telemetry=Telemetry.SEND_USAGE_DATA_TO_TABLEAU) as hyper:
12+
with Connection(endpoint=hyper.endpoint, database=hyper_database, create_mode=CreateMode.CREATE_AND_REPLACE) as connection:
13+
# Schema and table for TDE file is constant
14+
schema = 'Extract'
15+
table = 'Extract'
16+
17+
# Create the temp external table for the TDE file
18+
create_external_table_query = _get_external_table_query(str(tde_path), schema, table)
19+
connection.execute_command(create_external_table_query)
20+
21+
# Get the name of the table created from the catalog
22+
td = connection.catalog.get_table_definition(table)
23+
24+
# Create the schema
25+
connection.catalog.create_schema(schema)
26+
27+
# Create the destination table in the Hyper database
28+
schema_table = f"\"{schema}\".\"{table}\""
29+
create_table_command = f"CREATE TABLE {escape_name(schema_table)} AS SELECT * FROM {td.table_name}"
30+
31+
# Execute
32+
connection.execute_command(create_table_command)
33+
34+
print(f"Successfully converted {tde_path} to {hyper_database}")
35+
36+
def _get_external_table_query(tde_file_path: str,
37+
schema: str,
38+
table: str):
39+
return f"""CREATE TEMP EXTERNAL TABLE {escape_name(table)} FOR {escape_string_literal(tde_file_path)}
40+
(WITH (FORMAT TDE, TABLE {escape_string_literal(f"{schema}.{table}")}, SANITIZE))"""
41+
42+
if __name__ == '__main__':
43+
argparser = argparse.ArgumentParser(description="Script to convert a TDE file to a Hyper file.")
44+
argparser.add_argument("input_tde_path", type=Path, help="The input TDE file path that will be converted to a Hyper file.")
45+
args = argparser.parse_args()
46+
47+
input_tde_path = Path(args.input_tde_path)
48+
if not input_tde_path.exists():
49+
raise Exception(f"{input_tde_path} not found")
50+
51+
convert_tde_to_hyper(input_tde_path)

0 commit comments

Comments
 (0)