SQL CLIENTS & USAGE
CLI
- Download latest cli from trino.io
- Save .trino_config to your
$HOMEfolder - Update
.trino_configwith your configurations- set
useras your API Key - optional - replace the
servervalue for a custom HOST
- set
- Run queries as desired
$ trino --execute 'select * from ndaq_rtat10 limit 5'
$ trino --execute 'select * from zacks_hdm limit 5'Python
Here is a sample Python Code to write directly to disk. You'll need to adjust the user to your Data Link API Key.
"""
DataLink Trino Connection - Getting Started Example
This file demonstrates how to get started with DataLink's Trino native JDBC connection.
Use this as a reference to implement a solution based on your specific needs.
DataLink supports Trino native JDBC connections, so customers can use their own implementation
using the Trino JDBC driver: https://trino.io/docs/current/client/jdbc.html
Important:
- Review all TODO comments in this file to customize the query and processing logic for your use case.
Troubleshooting:
- If you are having issues with any query, please enable debug logging (set logging level to DEBUG).
- Debug mode will provide you with the query ID.
- Please provide the query ID to the DataLink Client Success team for assistance.
"""
import logging
from trino.dbapi import connect
def _extract_query_id(cur):
q = getattr(cur, "query_id", None) or getattr(cur, "_query_id", None)
try:
if not q and hasattr(cur, "_operation") and isinstance(cur._operation, dict):
q = cur._operation.get("queryId") or cur._operation.get("query_id")
except Exception:
pass
return q
conn = connect(
host="data.nasdaq.com",
port=443,
user="TODO: your api_key here",
catalog="main",
schema="huron",
http_scheme="https"
)
# Configure logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
#TODO: replace with your query
sql = ("SELECT 1 LIMIT 10")
try:
cur = conn.cursor()
cur.execute(sql)
# Immediately try to read and print the query id after execute succeeds
if(logger.isEnabledFor(logging.DEBUG)):
query_id = _extract_query_id(cur)
logger.debug(f"--------- Trino query id -------: {query_id}")
# use fetchall()/fetchone()/fetchmany() as needed https://github.com/trinodb/trino-python-client#db-api
rows = cur.fetchmany(size=1000)
for row in rows:
logger.info(f"Row: {row}")
# TODO: process rows as needed!
except Exception as exc:
# If execute raised, try to extract/print query id anyway, then re-raise
query_id = _extract_query_id(cur)
logger.error(f"Trino query id (after error): {query_id}")
raise
finally:
# Safety: if for any reason we haven't printed the id yet, attempt once more
cur.close()
logger.info("Closing Trino connection")
NOTE:Tested with Python 3
DotNet C#
- Add DataLinkSQL.csproj in a new project folder, or install those Nuget packages into your existing project.
- Configure environment variables
NASDAQ_DATA_LINK_API_KEYfor your API Key- Optional -
NASDAQ_DATA_LINK_BASE_DOMAINfor a custom HOST
- Add DataLinkSQL.cs to your project folder
- Run included Main to invoke sample queries
$ dotnet run --property DefineConstants=INCLUDE_MAIN- Additionally, create a new module with your desired queries and reuse helper functions
using NReco.PrestoAdo;
using static DataLinkSQL;
class MyApp
{
static void Main()
{
using (PrestoConnection connection = CreateConnection())
{
string sql = "...";
DbDataReader reader = RunSQL(connection, sql);
DisplayReader(reader);
}
}
}$ dotnet run
NOTE:Tested with Python 3
Tableau
- Connect "To a Server" and select "Presto"
- Enter configurations as explained in Endpoints, and also set
- Authentication: Username
- Require SSL: True
- Click the magnifying glass next to the "Enter table name" text box
- You should now see all your tables. Drag and drop them to build your models.
NOTE:Tested with Tableau Desktop 2022.2
DataGrip
Install JDBC driver
- Download the latest driver from trino.io
- Move the driver into a permanent location (where it won't get deleted accidentally); i.e., the DataGrip library folder
- in MacOS, this is
~/Library/Application\ Support/JetBrains/DataGrip${VERSION}/jdbc-drivers/
- in MacOS, this is
- Open DataGrip
- Click: File > New > Driver
- Fill in fields
- Name: Trino 406
- General (tab)
- Driver files: browse and select the downloaded driver
- Class:
io.trino.jdbc.TrinoDriver
- Options (tab)
- Other (section)
- Icon: Trino
- Other (section)
- Click Apply and OK
Set-up Data Source
- Open DataGrip
- Click: File > New > DataSource > Trino 406
- Fill in fields as explained in Endpoints
- Name: DataLink
- General (tab)
- User: your API Key
- URL:
jdbc:trino://${HOST}:${PORT}/${CATALOG}/${SCHEMA}
- Click Apply and OK
Updated about 2 months ago