Code Samples

Python code snippets to carry out common operations on e6data

Best Practices

Environment Variables

Setting environment variables is encouraged over hardcoding authentication variables and cluster IPs. The following environment variables can be set:

  • HOST_NAME: IP address or hostname of the cluster to be used.

  • E6_USER: Email address of the user account running the queries.

  • E6_TOKEN: Access token generated by the user in the e6data Console.

HOST_NAME=os.getenv("HOST_NAME")
E6_USER=os.getenv("E6_USER")
E6_TOKEN = os.getenv("E6_TOKEN")

Usage:

from e6data_python_connector import Connection

Create Connection

Sample code for establishing a connection with an e6data cluster.

from e6data_python_connector import Connection

username = '<username>'  # Your e6data Email ID.
password = '<password>'  # Access Token generated in the e6data console.

host = '<host>'  # IP address or hostname of the cluster to be used.
database = '<database>'  # # Database to perform the query on.
port = 443  # Port of the e6data engine.
catalog_name = '<catalog_name>'
cluster_uuid = '<cluster_uuid>'  # Specify cluster UUID on when secure=True
secure = True # Flag to use a secure channel for data transfer, default to False

conn = Connection(
    host=host,
    port=port,
    username=username,
    database=database,
    password=password,
    cluster_uuid=cluster_uuid,
    secure=secure
)

Switch Database in an Existing Connection

Perform Query & Get Results

This code executes a query and fetches all the results.

The cursor supports fetchmany and fetchone methods.

To get all the results:

To get only one result (first row):

To limit the number of results:

Code Hygiene

It is recommended to clear the cursor, close the cursor and close the connection after running a function as a best practice. This enhances performance by clearing old data from memory.

Get Row Count

This code executes a query and returns the number of rows in the result set.

Fetching rowcount from queryplanner is preferred over using fetchall method.

Get Query Execution Plan

The following code runs a query and returns the execution plan generated for the query by the e6data engine.

The execution plan data will be returned as a JSON string and should be parsed as JSON data.

Abort a Query

The following code aborts a query, referenced by the input Query ID. Refer Get Query ID for information on obtaining the Query ID.

Get Query Time Metrics

The following code runs a query and returns:

  • amount of time taken to execute the query in seconds

  • amount of time the query spent in the queue awaiting execution in seconds

  • amount of time taken to parse the query in seconds

Get Query ID

This code executes a query and returns the query ID.

Combine Multiple Functions

The following code is an example using multiple functions defined above to return:

  • Number of rows

  • The time taken to execute the query

  • Query results

Last updated