Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Pipe Elasticsearch Data to CSV in PowerShell

DZone 's Guide to

Pipe Elasticsearch Data to CSV in PowerShell

Use standard PowerShell cmdlets to access Elasticsearch tables.

· Big Data Zone ·
Free Resource

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.

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!

Topics:
big data ,elasticsearch tutorial ,data transformation ,data ingestion

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}