Skip to main content

A DBAPI and SQLAlchemy dialect for Elasticsearch

Project description

ElasticSearch DBAPI

Build Status PyPI version Coverage Status

elasticsearch-dbapi Implements a DBAPI (PEP-249) and SQLAlchemy dialect, that enables SQL access on elasticsearch clusters for query only access. Uses Elastic X-Pack SQL API

We are currently building support for opendistro/_sql API for AWS Elasticsearch Service / Open Distro SQL

This library supports Elasticsearch 7.X versions.

Installation

$ pip install elasticsearch-dbapi

To install support for AWS Elasticsearch Service / Open Distro:

$ pip install elasticsearch-dbapi[aws]

Usage:

Using DBAPI:

from es.elastic.api import connect

conn = connect(host='localhost')
curs = conn.cursor()
curs.execute(
    "select * from flights LIMIT 10"
)
print([row for row in curs])

Using SQLAlchemy execute:

from sqlalchemy.engine import create_engine

engine = create_engine("elasticsearch+http://localhost:9200/")
rows = engine.connect().execute(
    "select * from flights LIMIT 10"
)
print([row for row in rows])

Using SQLAlchemy:

from sqlalchemy import func, select
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import MetaData, Table


engine = create_engine("elasticsearch+http://localhost:9200/")
logs = Table("flights", MetaData(bind=engine), autoload=True)
count = select([func.count("*")], from_obj=logs).scalar()
print(f"COUNT: {count}")

Using SQLAlchemy reflection:

from sqlalchemy.engine import create_engine
from sqlalchemy.schema import Table, MetaData

engine = create_engine("elasticsearch+http://localhost:9200/")
logs = Table("flights", MetaData(bind=engine), autoload=True)
print(engine.table_names())

metadata = MetaData()
metadata.reflect(bind=engine)
print([table for table in metadata.sorted_tables])
print(logs.columns)

Connection Parameters:

elasticsearch-py is used to establish connections and transport, this is the official elastic python library. Elasticsearch constructor accepts multiple optional parameters that can be used to properly configure your connection on aspects like security, performance and high availability. These optional parameters can be set at the connection string, for example:

   elasticsearch+http://localhost:9200/?http_compress=True&timeout=100

will set transport to use gzip (http_compress) and timeout to 10 seconds.

For more information on configuration options, look at elasticsearch-py’s documentation:

The connection string follows RFC-1738, to support multiple nodes you should use sniff_* parameters

Fetch size

By default the maximum number of rows which get fetched by a single query is limited to 10000. This can be adapted through the fetch_size parameter:

from es.elastic.api import connect

conn = connect(host='localhost')
curs = conn.cursor(fetch_size=1000)

If more than 10000 rows should get fetched then max_result_window has to be adapted as well.

Tests

To run unittest launch elasticsearch and kibana (kibana is really not required but is a nice to have)

$ docker-compose up -d
$ nosetests -v

Special case for sql opendistro endpoint (AWS ES)

AWS ES exposes the opendistro SQL plugin, and it follows a different SQL dialect. Because of dialect and API response differences, we provide limited support for opendistro SQL on this package using the odelasticsearch driver:

from sqlalchemy.engine import create_engine

engine = create_engine(
    "odelasticsearch+https://search-SOME-CLUSTER.us-west-2.es.amazonaws.com:443/"
)
rows = engine.connect().execute(
    "select count(*), Carrier from flights GROUP BY Carrier"
)
print([row for row in rows])

Or using DBAPI:

from es.opendistro.api import connect

conn = connect(host='localhost',port=9200,path="", scheme="http")

curs = conn.cursor().execute(
    "select * from flights LIMIT 10"
)

print([row for row in curs])

Known limitations

This library does not yet support the following features:

  • Array type columns are not supported. Elaticsearch SQL does not support them either. SQLAlchemy get_columns will exclude them.
  • object and nested column types are not well supported and are converted to strings
  • Indexes that whose name start with .
  • GEO points are not currently well-supported and are converted to strings
  • Very limited support for AWS ES, no AWS Auth yet for example

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

elasticsearch-dbapi-0.1.4.tar.gz (43.4 kB view details)

Uploaded Source

Built Distributions

elasticsearch_dbapi-0.1.4-py3.7.egg (77.1 kB view details)

Uploaded Source

elasticsearch_dbapi-0.1.4-py3-none-any.whl (69.8 kB view details)

Uploaded Python 3

File details

Details for the file elasticsearch-dbapi-0.1.4.tar.gz.

File metadata

  • Download URL: elasticsearch-dbapi-0.1.4.tar.gz
  • Upload date:
  • Size: 43.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.6.1 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.7.6

File hashes

Hashes for elasticsearch-dbapi-0.1.4.tar.gz
Algorithm Hash digest
SHA256 fec4cf5e0a940adf6ebc80275d22b7e38483a97934c040059d08d85ae7c9ddce
MD5 2ffbdfdd2e2c5088b27ef790b8d2a039
BLAKE2b-256 1d7b2079fc8ddbdd416471e2ee5a0980aa73c73c7ceaccf0ca246c9b049de1f8

See more details on using hashes here.

Provenance

File details

Details for the file elasticsearch_dbapi-0.1.4-py3.7.egg.

File metadata

  • Download URL: elasticsearch_dbapi-0.1.4-py3.7.egg
  • Upload date:
  • Size: 77.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.6.1 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.7.6

File hashes

Hashes for elasticsearch_dbapi-0.1.4-py3.7.egg
Algorithm Hash digest
SHA256 5938a36f64b0dceaaf9fc3ea0ea48cd8b0edc80a8212b351e74efb727ebae608
MD5 037b7d622412b4d51af0f91b247a084e
BLAKE2b-256 721710f7eaecc4f54a0677c933382dd83a6e01915d6f4ac2af38a9a85c231fee

See more details on using hashes here.

Provenance

File details

Details for the file elasticsearch_dbapi-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: elasticsearch_dbapi-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 69.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.6.1 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.7.6

File hashes

Hashes for elasticsearch_dbapi-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fd21b27688be7c0b80b78baa614271067f7e30aef0ddb93ae826b7b30344f1fe
MD5 7e4ed00408a5791bd1f1ac8a57d191c7
BLAKE2b-256 ec167ba426a70c34aaceedacb6d35a8b5006a85ee5c5599da2b52181a7043e86

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page