How to pipe Presto Data to CSV in PowerShell



Use standard PowerShell cmdlets to access Presto tables.

The CData Cmdlets Module for Presto is a standard PowerShell module offering straightforward integration with Presto. Below, you will find examples of using our Presto Cmdlets with native PowerShell cmdlets.

About Presto Data Integration

Accessing and integrating live data from Trino and Presto SQL engines has never been easier with CData. Customers rely on CData connectivity to:

  • Access data from Trino v345 and above (formerly PrestoSQL) and Presto v0.242 and above (formerly PrestoDB)
  • Read and write access all of the data underlying your Trino or Presto instances
  • Optimized query generation for maximum throughput.

Presto and Trino allow users to access a variety of underlying data sources through a single endpoint. When paired with CData connectivity, users get pure, SQL-92 access to their instances, allowing them to integrate business data with a data warehouse or easily access live data directly from their preferred tools, like Power BI and Tableau.

In many cases, CData's live connectivity surpasses the native import functionality available in tools. One customer was unable to effectively use Power BI due to the size of the datasets needed for reporting. When the company implemented the CData Power BI Connector for Presto they were able to generate reports in real-time using the DirectQuery connection mode.


Getting Started


Creating a Connection to Your Presto Data

Set the Server and Port connection properties to connect, in addition to any authentication properties that may be required.

To enable TLS/SSL, set UseSSL to true.

Authenticating with LDAP

In order to authenticate with LDAP, set the following connection properties:

  • AuthScheme: Set this to LDAP.
  • User: The username being authenticated with in LDAP.
  • Password: The password associated with the User you are authenticating against LDAP with.

Authenticating with Kerberos

In order to authenticate with KERBEROS, set the following connection properties:

  • AuthScheme: Set this to KERBEROS.
  • KerberosKDC: The Kerberos Key Distribution Center (KDC) service used to authenticate the user.
  • KerberosRealm: The Kerberos Realm used to authenticate the user with.
  • KerberosSPN: The Service Principal Name for the Kerberos Domain Controller.
  • KerberosKeytabFile: The Keytab file containing your pairs of Kerberos principals and encrypted keys.
  • User: The user who is authenticating to Kerberos.
  • Password: The password used to authenticate to Kerberos.

$conn = Connect-Presto  -Server "$Server" -Port "$Port"

Selecting Data

Follow the steps below to retrieve data from the Customer table and pipe the result into to a CSV file:

Select-Presto -Connection $conn -Table Customer | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myCustomerData.csv -NoTypeInformation

You will notice that we piped the results from Select-Presto 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-Presto -Connection $conn -Table Customer -Where "Id = 123456789" | Remove-Presto

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 Presto, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyCustomerUpdates.csv | %{
  $record = Select-Presto -Connection $Presto -Table Customer -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-Presto -Connection $presto -Table Customer -Columns ("FirstName","LastName") -Values ($_.FirstName, $_.LastName) -Where ("Id = `'"+$_.Id+"`'")
  }else{
    Add-Presto -Connection $presto -Table Customer -Columns ("FirstName","LastName") -Values ($_.FirstName, $_.LastName)
  }
}

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!

Ready to get started?

Download a free trial of the Presto Cmdlets to get started:

 Download Now

Learn more:

Presto Icon Presto Data Cmdlets

An easy-to-use set of PowerShell Cmdlets offering real-time access to Presto. The Cmdlets allow users to easily read, write, update, and delete live data - just like working with SQL server.