|
1
|
|
|
import pandas as pd |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
from . import BasicNeeds as ClassBN |
|
4
|
|
|
from . import TypeDetermination as ClassTD |
|
5
|
|
|
|
|
6
|
|
|
from tableauhyperapi import HyperProcess, Telemetry, \ |
|
7
|
|
|
Connection, CreateMode, \ |
|
8
|
|
|
NOT_NULLABLE, NULLABLE, SqlType, TableDefinition, \ |
|
9
|
|
|
Inserter, \ |
|
10
|
|
|
TableName, \ |
|
11
|
|
|
HyperException |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
View Code Duplication |
class TableauHyperApiExtraLogic: |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
def fn_build_hyper_columns_for_csv(self, detected_csv_structure, verbose): |
|
17
|
|
|
list_hyper_table_columns_to_return = [] |
|
18
|
|
|
for current_field_structure in detected_csv_structure: |
|
19
|
|
|
list_hyper_table_columns_to_return.append(current_field_structure['order']) |
|
20
|
|
|
current_column_type = self.fn_convert_to_hyper_types(current_field_structure['type']) |
|
21
|
|
|
ClassBN.fn_optional_print(ClassBN, verbose, 'Column ' |
|
22
|
|
|
+ str(current_field_structure['order']) + ' having name "' |
|
23
|
|
|
+ current_field_structure['name'] + '" and type "' |
|
24
|
|
|
+ current_field_structure['type'] + '" will become "' |
|
25
|
|
|
+ str(current_column_type) + '"') |
|
26
|
|
|
if current_field_structure['nulls'] == 0: |
|
27
|
|
|
list_hyper_table_columns_to_return[current_field_structure['order']] = TableDefinition.Column( |
|
|
|
|
|
|
28
|
|
|
name = current_field_structure['name'], |
|
|
|
|
|
|
29
|
|
|
type = current_column_type, |
|
|
|
|
|
|
30
|
|
|
nullability = NOT_NULLABLE |
|
|
|
|
|
|
31
|
|
|
) |
|
32
|
|
|
else: |
|
33
|
|
|
list_hyper_table_columns_to_return[current_field_structure['order']] = TableDefinition.Column( |
|
|
|
|
|
|
34
|
|
|
name = current_field_structure['name'], |
|
|
|
|
|
|
35
|
|
|
type = current_column_type, |
|
|
|
|
|
|
36
|
|
|
nullability = NULLABLE |
|
|
|
|
|
|
37
|
|
|
) |
|
38
|
|
|
return list_hyper_table_columns_to_return |
|
39
|
|
|
|
|
40
|
|
|
@staticmethod |
|
41
|
|
|
def fn_convert_to_hyper_types(given_type): |
|
42
|
|
|
switcher = { |
|
43
|
|
|
'empty': SqlType.text(), |
|
44
|
|
|
'int': SqlType.big_int(), |
|
45
|
|
|
'float-USA': SqlType.double(), |
|
46
|
|
|
'date-iso8601': SqlType.date(), |
|
47
|
|
|
'date-USA': SqlType.date(), |
|
48
|
|
|
'time-24': SqlType.time(), |
|
49
|
|
|
'time-24-micro-sec': SqlType.time(), |
|
50
|
|
|
'time-USA': SqlType.time(), |
|
51
|
|
|
'time-USA-micro-sec': SqlType.time(), |
|
52
|
|
|
'datetime-iso8601': SqlType.timestamp(), |
|
53
|
|
|
'datetime-iso8601-micro-sec': SqlType.timestamp(), |
|
54
|
|
|
'str': SqlType.text() |
|
55
|
|
|
} |
|
56
|
|
|
identified_type = switcher.get(given_type) |
|
57
|
|
|
if identified_type is None: |
|
58
|
|
|
identified_type = SqlType.text() |
|
59
|
|
|
return identified_type |
|
60
|
|
|
|
|
61
|
|
|
def fn_create_hyper_file_from_csv(self, input_csv_data_frame, formats_to_evaluate, output_hyper_file, verbose): |
|
|
|
|
|
|
62
|
|
|
detected_csv_structure = ClassTD.fn_detect_csv_structure(ClassTD, input_csv_data_frame, |
|
63
|
|
|
formats_to_evaluate, verbose) |
|
64
|
|
|
hyper_table_columns = self.fn_build_hyper_columns_for_csv(self, detected_csv_structure, verbose) |
|
|
|
|
|
|
65
|
|
|
# Starts the Hyper Process with telemetry enabled/disabled to send data to Tableau or not |
|
66
|
|
|
# To opt in, simply set telemetry=Telemetry.SEND_USAGE_DATA_TO_TABLEAU. |
|
67
|
|
|
# To opt out, simply set telemetry=Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU. |
|
68
|
|
|
with HyperProcess(telemetry = Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU) as hyper: |
|
|
|
|
|
|
69
|
|
|
# Creates new Hyper file <output_hyper_file> |
|
70
|
|
|
# Replaces file with CreateMode.CREATE_AND_REPLACE if it already exists. |
|
71
|
|
|
with Connection(endpoint = hyper.endpoint, |
|
|
|
|
|
|
72
|
|
|
database = output_hyper_file, |
|
|
|
|
|
|
73
|
|
|
create_mode = CreateMode.CREATE_AND_REPLACE) as hyper_connection: |
|
|
|
|
|
|
74
|
|
|
print(f'Connection to the Hyper engine file "{output_hyper_file}" has been created.') |
|
|
|
|
|
|
75
|
|
|
hyper_connection.catalog.create_schema("Extract") |
|
76
|
|
|
print('Hyper schema "Extract" has been created.') |
|
77
|
|
|
hyper_table = TableDefinition( |
|
78
|
|
|
TableName("Extract", "Extract"), |
|
79
|
|
|
columns = hyper_table_columns |
|
|
|
|
|
|
80
|
|
|
) |
|
81
|
|
|
hyper_connection.catalog.create_table(table_definition = hyper_table) |
|
|
|
|
|
|
82
|
|
|
print('Hyper table "Extract" has been created.') |
|
83
|
|
|
# The rows to insert into the <hyper_table> table. |
|
84
|
|
|
data_to_insert = self.fn_rebuild_csv_content_for_hyper(self, input_csv_data_frame, |
|
85
|
|
|
detected_csv_structure, verbose) |
|
|
|
|
|
|
86
|
|
|
# Execute the actual insert |
|
87
|
|
|
with Inserter(hyper_connection, hyper_table) as hyper_inserter: |
|
88
|
|
|
hyper_inserter.add_rows(rows = data_to_insert) |
|
|
|
|
|
|
89
|
|
|
hyper_inserter.execute() |
|
90
|
|
|
# Number of rows in the <hyper_table> table. |
|
91
|
|
|
# `execute_scalar_query` is for executing a query that returns exactly one row with one column. |
|
|
|
|
|
|
92
|
|
|
row_count = hyper_connection.\ |
|
93
|
|
|
execute_scalar_query(query = f'SELECT COUNT(*) FROM {hyper_table.table_name}') |
|
|
|
|
|
|
94
|
|
|
print(f'Number of rows in table {hyper_table.table_name} is {row_count}.') |
|
95
|
|
|
print('Connection to the Hyper engine file has been closed.') |
|
96
|
|
|
print('Hyper engine process has been shut down.') |
|
97
|
|
|
|
|
98
|
|
|
def fn_rebuild_csv_content_for_hyper(self, input_csv_data_frame, detected_fields_type, verbose): |
|
99
|
|
|
input_csv_data_frame.replace(to_replace = [pd.np.nan], value = [None], inplace = True) |
|
|
|
|
|
|
100
|
|
|
# Cycle through all found columns |
|
101
|
|
|
for current_field in detected_fields_type: |
|
102
|
|
|
fld_nm = current_field['name'] |
|
103
|
|
|
ClassBN.fn_optional_print(ClassBN, verbose, 'Column ' + fld_nm + ' ' |
|
104
|
|
|
+ 'has panda_type = ' + str(current_field['panda_type']) + ' ' |
|
105
|
|
|
+ 'and ' + str(current_field['type'])) |
|
106
|
|
|
if current_field['panda_type'] == 'float64' and current_field['type'] == 'int': |
|
107
|
|
|
input_csv_data_frame[fld_nm] = input_csv_data_frame[fld_nm].replace(to_replace = [pd.np.nan, '.0'], |
|
|
|
|
|
|
108
|
|
|
value = [None, ''], |
|
|
|
|
|
|
109
|
|
|
inplace = True) |
|
|
|
|
|
|
110
|
|
|
elif current_field['type'] in ('datetime-iso8601', 'datetime-iso8601-micro-sec'): |
|
111
|
|
|
input_csv_data_frame[fld_nm] = pd.to_datetime(input_csv_data_frame[fld_nm]) |
|
112
|
|
|
return input_csv_data_frame.values |
|
113
|
|
|
|
|
114
|
|
|
def fn_run_hyper_creation(self, input_csv_data_frame, formats_to_evaluate, output_hyper_file, verbose): |
|
|
|
|
|
|
115
|
|
|
try: |
|
116
|
|
|
self.fn_create_hyper_file_from_csv(self, input_csv_data_frame, formats_to_evaluate, output_hyper_file, |
|
|
|
|
|
|
117
|
|
|
verbose) |
|
118
|
|
|
except HyperException as ex: |
|
119
|
|
|
print(ex) |
|
120
|
|
|
exit(1) |
|
121
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.