Custom Functions#
IMPORT / EXPORT with custom callbacks and custom parameters (gzip, encoding, columns, CSV formatting)#
"""
HTTP transport
EXPORT and IMPORT from Exasol with custom callbacks and custom IMPORT / EXPORT parameters
"""
import os
import shutil
import tempfile
import examples._config as config
import pyexasol
def connect() -> pyexasol.ExaConnection:
# Connect with compression enabled
return pyexasol.connect(
dsn=config.dsn,
user=config.user,
password=config.password,
schema=config.schema,
compression=True,
websocket_sslopt=config.websocket_sslopt,
)
# Prepare empty tables
with connect() as C:
C.execute("TRUNCATE TABLE users_copy")
C.execute("TRUNCATE TABLE users_copy_reordered")
# Export to custom callback
def my_export_callback(pipe, dst):
# Save 2 first lines
lines = [pipe.readline(), pipe.readline()]
# Dump everything else to /dev/null
dev_null = open(os.devnull, "wb")
shutil.copyfileobj(pipe, dev_null)
dev_null.close()
return lines
# Run EXPORT using the custom callback function
with connect() as C:
result = C.export_to_callback(my_export_callback, None, "users")
statement = C.last_statement()
print(result)
print(f"EXPORTED {statement.rowcount()} rows in {statement.execution_time}s")
# Import from custom callback
def my_import_callback(pipe, src):
for line in src:
pipe.write(line)
# Run IMPORT using the custom callback function
with connect() as C:
C.import_from_callback(my_import_callback, result, "users_copy")
statement = C.last_statement()
print(f"IMPORTED {statement.rowcount()} rows in {statement.execution_time}s")
# Export as gzipped file
with tempfile.TemporaryFile() as file:
with connect() as C:
C.export_to_file(
file, "users", export_params={"with_column_names": True, "format": "gz"}
)
statement = C.last_statement()
file.seek(0)
print(file.read(100))
file.seek(0)
print(f"EXPORTED {statement.rowcount()} rows in {statement.execution_time}s")
# Import gzipped file
with connect() as C:
C.import_from_file(
file, "users_copy", import_params={"skip": 1, "format": "gz"}
)
statement = C.last_statement()
print(f"IMPORTED {statement.rowcount()} rows in {statement.execution_time}s")
# Custom encoding for IMPORT and EXPORT
with tempfile.TemporaryFile() as file:
with connect() as C:
C.export_to_file(file, "users", export_params={"encoding": "WINDOWS-1251"})
file.seek(0)
print(file.readline())
file.seek(0)
# Import file with custom encoding
with connect() as C:
C.import_from_file(
file, "users_copy", import_params={"encoding": "WINDOWS-1251"}
)
statement = C.last_statement()
print(f"IMPORTED {statement.rowcount()} rows in {statement.execution_time}s")
# Custom columns list for EXPORT
with tempfile.TemporaryFile() as file:
with connect() as C:
C.export_to_file(
file,
"users",
export_params={
"columns": ["register_dt", "user_id", "status", "user_name"]
},
)
file.seek(0)
print(file.readline())
file.seek(0)
# Custom columns list for IMPORT
with connect() as C:
C.import_from_file(
file,
"users_copy_reordered",
import_params={
"columns": ["register_dt", "user_id", "status", "user_name"]
},
)
statement = C.last_statement()
print(f"IMPORTED {statement.rowcount()} rows in {statement.execution_time}s")
# Custom CSV cols formatting
with tempfile.TemporaryFile() as file:
with connect() as C:
C.export_to_file(
file,
"users",
export_params={
"csv_cols": [
"1",
"2",
"3 FORMAT='DD-MM-YYYY'",
"4..6",
"7 FORMAT='999.99999'",
"8",
]
},
)
file.seek(0)
print(file.readline())
file.seek(0)
with connect() as C:
C.import_from_file(
file,
"users_copy",
import_params={
"csv_cols": [
"1",
"2",
"3 FORMAT='DD-MM-YYYY'",
"4..6",
"7 FORMAT='999.99999'",
"8",
]
},
)
statement = C.last_statement()
print(f"IMPORTED {statement.rowcount()} rows in {statement.execution_time}s")