Model Context Protocol (MCP) finally gives AI models a way to access the business data needed to make them really useful at work. CData MCP Servers have the depth and performance to make sure AI has access to all of the answers.
Try them now for free →How to pipe Elasticsearch Data to CSV in PowerShell
Use standard PowerShell cmdlets to access Elasticsearch tables.
The CData Cmdlets Module for Elasticsearch is a standard PowerShell module offering straightforward integration with Elasticsearch. Below, you will find examples of using our Elasticsearch Cmdlets with native PowerShell cmdlets.
About Elasticsearch Data Integration
Accessing and integrating live data from Elasticsearch has never been easier with CData. Customers rely on CData connectivity to:
- Access both the SQL endpoints and REST endpoints, optimizing connectivity and offering more options when it comes to reading and writing Elasticsearch data.
- Connect to virtually every Elasticsearch instance starting with v2.2 and Open Source Elasticsearch subscriptions.
- Always receive a relevance score for the query results without explicitly requiring the SCORE() function, simplifying access from 3rd party tools and easily seeing how the query results rank in text relevance.
- Search through multiple indices, relying on Elasticsearch to manage and process the query and results instead of the client machine.
Users frequently integrate Elasticsearch data with analytics tools such as Crystal Reports, Power BI, and Excel, and leverage our tools to enable a single, federated access layer to all of their data sources, including Elasticsearch.
For more information on CData's Elasticsearch solutions, check out our Knowledge Base article: CData Elasticsearch Driver Features & Differentiators.
Getting Started
Creating a Connection to Your Elasticsearch Data
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.
$conn = Connect-Elasticsearch -Server "$Server" -Port "$Port" -User "$User" -Password "$Password"
Selecting Data
Follow the steps below to retrieve data from the Orders table and pipe the result into to a CSV file:
Select-Elasticsearch -Connection $conn -Table Orders | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myOrdersData.csv -NoTypeInformation
You will notice that we piped the results from Select-Elasticsearch into a Select-Object cmdlet and excluded some properties before piping them into an Export-Csv cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each "row" in the result set, and we do not necessarily want that information in our CSV file.
The Connection, Table, and Columns are appended to the results in order to facilitate piping results from one of the CData Cmdlets directly into another one.Deleting Data
The following line deletes any records that match the criteria:
Select-Elasticsearch -Connection $conn -Table Orders -Where "ShipCity = New York" | Remove-Elasticsearch
Inserting and Updating Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Elasticsearch, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyOrdersUpdates.csv | %{ $record = Select-Elasticsearch -Connection $Elasticsearch -Table Orders -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-Elasticsearch -Connection $elasticsearch -Table Orders -Columns ("OrderName","Freight") -Values ($_.OrderName, $_.Freight) -Where ("Id = `'"+$_.Id+"`'") }else{ Add-Elasticsearch -Connection $elasticsearch -Table Orders -Columns ("OrderName","Freight") -Values ($_.OrderName, $_.Freight) } }
As always, our goal is to simplify the way you connect to data. With cmdlets users can install a data module, set the connection properties, and start building. Download Cmdlets and start working with your data in PowerShell today!