DateModifiedViewsCategory

Trino access is represented by many Python libraries among those are Dropbox/PyHive, trinodb/trino-python-client, PySpark, and PyJDBC. Mostly of libraries use Python DB-API interface to access Trino which uniforms commands.

trinodb/trino-python-client library is actively supported by Trino community and native to Trino. An alternative option to access Trino is described in Python Access to Trino Cluster with PyHive in article.

The sample is run with Python 3.8 in Windows with Starburst distribution.

1. Install Trino client library

Linux

With sudo access.

sudo pip3 install trino

Windows

Run as administrator.

pip install trino

Also, it's possible to install the library without elevated privileges in Linux and Windows.

pip3 install --user trino

2. Include requested libraries

import trino

3. Establish connection

  • Access to Trino cluster without password.

    conn = trino.dbapi.connect(host='localhost',
                               port=8080,
                               catalog='system',
                               schema='runtime')
    
  • Trino cluster is secured by password but skip SSL verification. This case might be used during development stage.

    conn = trino.dbapi.connect(host='localhost',
                               port=443,
                               http_scheme='https',
                               catalog='system',
                               schema='runtime',
                               auth=trino.auth.BasicAuthentication('<user name>', '<password>'),
                               verify=False)
    
  • Trino cluster is secured by password.

    Option #1. Follow instructions in Convert Java Keystore to PEM File Format article to create trino.pem file. The file contains Trino SSL public certificate converted from Java keystore file.

    Option #2. Extract trino.pem certificate from Internet Browser. Follow Export TLS/SSL Server Certificate from Internet Browser article.

    conn = trino.dbapi.connect(host='localhost',
                               port=443,
                               http_scheme='https',
                               catalog='system',
                               schema='runtime',
                               auth=trino.auth.BasicAuthentication('<user name>', '<password>'),
                               verify='trino.pem')
    

4. Create cursor

cur = conn.cursor()

5. Retrieve data

cur.execute('SELECT * FROM nodes')
for row in cur.fetchall():
    print(row)

6. Improvements

To disable insecure warnings during https requests if verify=False, add the code in import section.

import urllib3
urllib3.disable_warnings()

7. Troubleshooting

In case of getting ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777) error, check your certificate expiration date. The date has to be valid.

Resources


Comments

comments powered by Disqus