Code Monkey home page Code Monkey logo

trino-python-client's Introduction

Build Status Trino Slack Presto: The Definitive Guide book download

Introduction

This package provides a client interface to query Trino a distributed SQL engine. It supports Python 2.7, 3.5, 3.6, and pypy.

Installation

$ pip install trino

Quick Start

Use the DBAPI interface to query Trino:

import trino
conn = trino.dbapi.connect(
    host='localhost',
    port=8080,
    user='the-user',
    catalog='the-catalog',
    schema='the-schema',
)
cur = conn.cursor()
cur.execute('SELECT * FROM system.runtime.nodes')
rows = cur.fetchall()

This will query the system.runtime.nodes system tables that shows the nodes in the Trino cluster.

The DBAPI implementation in trino.dbapi provides methods to retrieve fewer rows for example Cursorfetchone() or Cursor.fetchmany(). By default Cursor.fetchmany() fetches one row. Please set trino.dbapi.Cursor.arraysize accordingly.

Basic Authentication

The BasicAuthentication class can be used to connect to a LDAP-configured Trino cluster:

import trino
conn = trino.dbapi.connect(
    host='coordinator url',
    port=8443,
    user='the-user',
    catalog='the-catalog',
    schema='the-schema',
    http_scheme='https',
    auth=trino.auth.BasicAuthentication("principal id", "password"),
)
cur = conn.cursor()
cur.execute('SELECT * FROM system.runtime.nodes')
rows = cur.fetchall()

Transactions

The client runs by default in autocommit mode. To enable transactions, set isolation_level to a value different than IsolationLevel.AUTOCOMMIT:

import trino
from trino import transaction
with trino.dbapi.connect(
    host='localhost',
    port=8080,
    user='the-user',
    catalog='the-catalog',
    schema='the-schema',
    isolation_level=transaction.IsolationLevel.REPEATABLE_READ,
) as conn:
  cur = conn.cursor()
  cur.execute('INSERT INTO sometable VALUES (1, 2, 3)')
  cur.fetchone()
  cur.execute('INSERT INTO sometable VALUES (4, 5, 6)')
  cur.fetchone()

The transaction is created when the first SQL statement is executed. trino.dbapi.Connection.commit() will be automatically called when the code exits the with context and the queries succeed, otherwise `trino.dbapi.Connection.rollback()' will be called.

Development

Getting Started With Development

Start by forking the repository and then modify the code in your fork.

Clone the repository and go inside the code directory. Then you can get the version with ./setup.py --version.

We recommend that you use virtualenv for development:

$ virtualenv .venv
$ . .venv/bin/activate
# TODO add requirements.txt: pip install -r requirements.txt
$ pip install .

For development purpose, pip can reference the code you are modifying in a virtualenv:

$ pip install -e .[tests]

That way, you do not need to run pip install again to make your changes applied to the virtualenv.

When the code is ready, submit a Pull Request.

Code Style

  • For Python code, adhere to PEP 8.
  • Prefer code that is readable over one that is "clever".
  • When writing a Git commit message, follow these guidelines.

Running Tests

There is a helper scripts, run, that provides commands to run tests. Type ./run tests to run both unit and integration tests.

trino-python-client uses pytest for its tests. To run only unit tests, type:

$ pytest tests

Then you can pass options like --pdb or anything supported by pytest --help.

To run the tests with different versions of Python in managed virtualenvs, use tox (see the configuration in tox.ini):

$ tox

To run integration tests:

$ pytest integration_tests

They pull a Docker image and then run a container with a Trino server:

  • the image is named trinodb/trino:${TRINO_VERSION}
  • the container is named trino-python-client-tests-{uuid4()[:7]}

Releasing

  • Set up your development environment.
  • Check the local workspace is up to date and has no uncommitted changes
    git fetch -a && git status
  • Change version in trino/__init__.py to a new version, e.g. 0.123.0.
  • Commit
    git commit -a -m "Bump version to 0.123.0"
  • Create an annotated tag
    git tag -m "" 0.123.0
  • Create release package and upload it to PyPI
    . .venv/bin/activate &&
    pip install twine &&
    rm -rf dist/ &&
    ./setup.py sdist bdist_wheel &&
    twine upload dist/* &&
    open https://pypi.org/project/trino/ &&
    echo "Released!"
  • Push the branch and the tag
    git push upstream master 0.123.0
  • Send release announcement.

Need Help?

Feel free to create an issue as it make your request visible to other users and contributors.

If an interactive discussion would be better or if you just want to hangout and chat about the Trino Python client, you can join us on the #python-client channel on Trino Slack.

trino-python-client's People

Contributors

aravindanilango avatar asottile avatar caiopavanelli avatar ebyhr avatar electrum avatar fernando-data avatar findepi avatar ggreg avatar haozhun avatar harph avatar hashhar avatar highker avatar iadi7ya avatar kevin-yao avatar losipiuk avatar lxynov avatar matthewwardrop avatar oneraynyday avatar realknorke avatar rgbkrk avatar rushton avatar rzeyde-varada avatar takuti avatar yuokada avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.