File#

IMPORT / EXPORT to and from a file#
"""
HTTP transport

EXPORT and IMPORT from Exasol to and from files
"""

import itertools
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 table
with connect() as C:
    C.execute("TRUNCATE TABLE users_copy")

# Create temporary file
with tempfile.TemporaryFile() as file:
    # Export to temporary file
    with connect() as C:
        C.export_to_file(
            file,
            "users",
            export_params={
                "with_column_names": True,
                "comment": "Exporting users to temp file",
            },
        )

        statement = C.last_statement()

    file.seek(0)
    for line in itertools.islice(file, 3):
        print(line)
    file.seek(0)

    print(f"EXPORTED {statement.rowcount()} rows in {statement.execution_time}s")

    # Import from temporary file
    with connect() as C:
        C.import_from_file(file, "users_copy", import_params={"skip": 1})
        statement = C.last_statement()

print(f"IMPORTED {statement.rowcount()} rows in {statement.execution_time}s")