### Install elasticsearch-dbapi via pip Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Provides commands to install the `elasticsearch-dbapi` library using pip. It includes the base installation and an optional installation for AWS Elasticsearch Service / Open Distro support. ```bash $ pip install elasticsearch-dbapi ``` ```bash $ pip install elasticsearch-dbapi[opendistro] ``` -------------------------------- ### Run Unit Tests for elasticsearch-dbapi Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Provides the necessary commands to set up and execute the unit tests for the `elasticsearch-dbapi` project. It involves starting Elasticsearch and Kibana using Docker Compose and then running tests with `nosetests`. ```bash $ docker-compose up -d ``` ```bash $ nosetests -v ``` -------------------------------- ### Perform Aggregation Queries with SQLAlchemy Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Demonstrates using SQLAlchemy's declarative capabilities to interact with Elasticsearch. It shows how to define a table, use SQLAlchemy's `func.count` for aggregation, and execute the query to get a scalar result. ```python 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}") ``` -------------------------------- ### Connect and Query Elasticsearch using DBAPI Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Demonstrates how to establish a connection to Elasticsearch using the DBAPI interface provided by `elasticsearch-dbapi`. It shows how to create a cursor, execute a SQL query, and iterate over the fetched results. ```python 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]) ``` -------------------------------- ### Configure Basic Authentication for Opendistro with SQLAlchemy Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Illustrates setting up basic username/password authentication for an Opendistro Elasticsearch connection using SQLAlchemy's `create_engine` by embedding credentials directly in the URI. ```Python from sqlalchemy.engine import create_engine engine = create_engine( "odelasticsearch+https://my_user:my_password@search-SOME-CLUSTER.us-west-2.es.amazonaws.com:443/" ) ``` -------------------------------- ### Build and Upload Python Package to PyPI Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/RELEASING.md These commands are used to build the Python package distributions (source distribution and wheel) and then upload the generated files to the Python Package Index (PyPI) using the `twine` utility. This is a standard step in the release process for Python libraries. ```bash python setup.py sdist bdist_wheel twine upload dist/* ``` -------------------------------- ### Enable Opendistro SQL v2 Engine Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Shows how to enable the new Opendistro SQL v2 engine by appending `v2=true` as a query parameter to the connection URI, which brings improvements and fixes. ```URI odelasticsearch+https://search-SOME-CLUSTER.us-west-2.es.amazonaws.com:443/?aws_profile=us-west-2&v2=true ``` -------------------------------- ### Query Elasticsearch using SQLAlchemy Engine Execute Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Illustrates how to connect to Elasticsearch using SQLAlchemy's `create_engine` with the `elasticsearch+http` dialect. It shows how to obtain a connection from the engine and execute a raw SQL query directly. ```python 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]) ``` -------------------------------- ### Connect to Docker Compose Opendistro Instance Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Provides the connection URI for connecting to an Opendistro Elasticsearch instance running via `docker-compose`, including basic authentication and disabling certificate verification. ```URI odelasticsearch+https://admin:admin@localhost:9400/?verify_certs=False ``` -------------------------------- ### Connect to AWS ES Opendistro via DBAPI Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Shows how to connect to an AWS Elasticsearch Opendistro endpoint using the `es.opendistro.api.connect` DBAPI and execute a SQL query to fetch a limited number of rows. ```Python 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]) ``` -------------------------------- ### Configure Elasticsearch Connection Parameters via URL Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Explains how to pass additional connection parameters, such as `http_compress` and `timeout`, to the underlying `elasticsearch-py` library by appending them as query parameters to the SQLAlchemy connection string URL. ```bash elasticsearch+http://localhost:9200/?http_compress=True&timeout=100 ``` -------------------------------- ### Connect to AWS ES Opendistro via SQLAlchemy Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Demonstrates connecting to an AWS Elasticsearch Opendistro endpoint using SQLAlchemy's `odelasticsearch` driver and executing a SQL query to retrieve data. ```Python 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]) ``` -------------------------------- ### Reflect Elasticsearch Schema using SQLAlchemy Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Shows how to use SQLAlchemy's reflection features to inspect the schema of an Elasticsearch instance. It demonstrates listing available table names and reflecting metadata to access column details for a specific table. ```python 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) ``` -------------------------------- ### Configure IAM AWS Profile Authentication for Opendistro with SQLAlchemy Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Demonstrates configuring IAM authentication using an AWS profile for Opendistro Elasticsearch connections in SQLAlchemy, by passing the `aws_profile` parameter in the connection URI. ```Python from sqlalchemy.engine import create_engine engine = create_engine( "odelasticsearch+https://search-SOME-CLUSTER.us-west-2.es.amazonaws.com:443/?aws_profile=us-west-2" ) ``` -------------------------------- ### Configure IAM AWS Key Authentication for Opendistro with SQLAlchemy Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Explains how to use AWS access and secret keys for IAM authentication with Opendistro Elasticsearch via SQLAlchemy, specifying `aws_keys` and `aws_region` as query parameters in the connection URI. ```Python from sqlalchemy.engine import create_engine engine = create_engine( "odelasticsearch+https://:@search-SOME-CLUSTER.us-west-2.es.amazonaws.com:443/?aws_keys=1&&aws_region=" ) ``` -------------------------------- ### Python Project Dependency List Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/requirements-dev.txt This snippet lists all required Python packages and their pinned versions for the project. These dependencies are crucial for ensuring a consistent development, testing, and production environment, covering aspects like code formatting, linting, testing, and cloud integration. ```Python black==22.3.0 coverage==6.3.2 flake8==4.0.1 flake8-commas==2.1.0 flake8-import-order==0.18.1 nose==1.3.7 pip-tools==3.6.0 pre-commit==1.17.0 twine==2.0.0 readme_renderer==24.0 mypy==0.790 requests-aws4auth==1.0.1 boto3==1.16.63 pytest==7.2.1 ``` -------------------------------- ### Set Fetch Size for Elasticsearch Queries Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Demonstrates how to configure the `fetch_size` parameter when establishing a connection using the DBAPI. This parameter controls the maximum number of rows retrieved by a single query, with a default limit of 10000. ```python from es.elastic.api import connect conn = connect(host="localhost", fetch_size=1000) curs = conn.cursor() ``` -------------------------------- ### Set Time Zone for Elasticsearch Queries Source: https://github.com/preset-io/elasticsearch-dbapi/blob/master/README.md Illustrates how to specify the `time_zone` parameter when connecting to Elasticsearch via the DBAPI. This allows overriding the default UTC time zone for queries, ensuring results are aligned with a specific geographic time zone. ```python from es.elastic.api import connect conn = connect(host="localhost", time_zone="Asia/Shanghai") curs = conn.cursor() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.