Extract, Transform, and Load Elasticsearch Data in Python

Ready to get started?

Download a free trial:

Download Now

Learn more:

Elasticsearch Python Connector

Python Connector Libraries for Elasticsearch Data Connectivity. Integrate Elasticsearch with popular Python tools like Pandas, SQLAlchemy, Dash & petl.

The CData Python Connector for Elasticsearch enables you to create ETL applications and pipelines for Elasticsearch data in Python with petl.

The rich ecosystem of Python modules lets you get to work quickly and integrate your systems more effectively. With the CData Python Connector for Elasticsearch and the petl framework, you can build Elasticsearch-connected applications and pipelines for extracting, transforming, and loading Elasticsearch data. This article shows how to connect to Elasticsearch with the CData Python Connector and use petl and pandas to extract, transform, and load Elasticsearch data.

With built-in, optimized data processing, the CData Python Connector offers unmatched performance for interacting with live Elasticsearch data in Python. When you issue complex SQL queries from Elasticsearch, the driver pushes supported SQL operations, like filters and aggregations, directly to Elasticsearch and utilizes the embedded SQL engine to process unsupported operations client-side (often SQL functions and JOIN operations).

Connecting to Elasticsearch Data

Connecting to Elasticsearch data looks just like connecting to any relational data source. Create a connection string using the required connection properties. For this article, you will pass the connection string as a parameter to the create_engine function.

Set the Server and Port connection properties to connect. To authenticate, set the User and Password properties, PKI (public key infrastructure) properties, or both. To use PKI, set the SSLClientCert, SSLClientCertType, SSLClientCertSubject, and SSLClientCertPassword properties.

The data provider uses X-Pack Security for TLS/SSL and authentication. To connect over TLS/SSL, prefix the Server value with 'https://'. Note: TLS/SSL and client authentication must be enabled on X-Pack to use PKI.

Once the data provider is connected, X-Pack will then perform user authentication and grant role permissions based on the realms you have configured.

After installing the CData Elasticsearch Connector, follow the procedure below to install the other required modules and start accessing Elasticsearch through Python objects.

Install Required Modules

Use the pip utility to install the required modules and frameworks:

pip install petl
pip install pandas

Build an ETL App for Elasticsearch Data in Python

Once the required modules and frameworks are installed, we are ready to build our ETL app. Code snippets follow, but the full source code is available at the end of the article.

First, be sure to import the modules (including the CData Connector) with the following:

import petl as etl
import pandas as pd
import cdata.elasticsearch as mod

You can now connect with a connection string. Use the connect function for the CData Elasticsearch Connector to create a connection for working with Elasticsearch data.

cnxn = mod.connect("Server=;Port=9200;User=admin;Password=123456;")

Create a SQL Statement to Query Elasticsearch

Use SQL to create a statement for querying Elasticsearch. In this article, we read data from the Orders entity.

sql = "SELECT OrderName, Freight FROM Orders WHERE ShipCity = 'New York'"

Extract, Transform, and Load the Elasticsearch Data

With the query results stored in a DataFrame, we can use petl to extract, transform, and load the Elasticsearch data. In this example, we extract Elasticsearch data, sort the data by the Freight column, and load the data into a CSV file.

Loading Elasticsearch Data into a CSV File

table1 = etl.fromdb(cnxn,sql)

table2 = etl.sort(table1,'Freight')


In the following example, we add new rows to the Orders table.

Adding New Rows to Elasticsearch

table1 = [ ['OrderName','Freight'], ['NewOrderName1','NewFreight1'], ['NewOrderName2','NewFreight2'], ['NewOrderName3','NewFreight3'] ]

etl.appenddb(table1, cnxn, 'Orders')

With the CData Python Connector for Elasticsearch, you can work with Elasticsearch data just like you would with any database, including direct access to data in ETL packages like petl.

Free Trial & More Information

Download a free, 30-day trial of the Elasticsearch Python Connector to start building Python apps and scripts with connectivity to Elasticsearch data. Reach out to our Support Team if you have any questions.

Full Source Code

import petl as etl
import pandas as pd
import cdata.elasticsearch as mod

cnxn = mod.connect("Server=;Port=9200;User=admin;Password=123456;")

sql = "SELECT OrderName, Freight FROM Orders WHERE ShipCity = 'New York'"

table1 = etl.fromdb(cnxn,sql)

table2 = etl.sort(table1,'Freight')


table3 = [ ['OrderName','Freight'], ['NewOrderName1','NewFreight1'], ['NewOrderName2','NewFreight2'], ['NewOrderName3','NewFreight3'] ]

etl.appenddb(table3, cnxn, 'Orders')