Establishing a Connection
With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our Presto Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.
Installing and Connecting
If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.
Install-Module PrestoCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module PrestoCmdlets;
You can then use the Connect-Presto cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-Presto -Server '127.0.0.1' -Port 9200
Connecting to Presto
Set the Server and Port connection properties to connect, in addition to any authentication properties that may be required.
Securing Presto Connections
To enable TLS/SSL in the cmdlet, set UseSSL to true.
Authenticating to Presto
Presto supports authentication via either LDAP or Kerberos. If the Presto server does not have authentication set up, leave AuthScheme set to NONE (default).The following subsections describe the requirements for authenticating with either LDAP or Kerberos.
LDAP
To authenticate with LDAP, set these connection properties:
- AuthScheme: LDAP.
- User: The authenticating user.
- Password: The authenticating user's password.
Kerberos
For details on authenticating with Kerberos, see Using Kerberos.
Retrieving Data
The Select-Presto cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-Presto -Connection $conn -Table "[Hive].[Default].Customers" -Columns @("Id, Name") -Where "Industry='Floppy Disks'"The Invoke-Presto cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.
Piping Cmdlet Output
The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:
Select-Presto -Connection $conn -Table [Hive].[Default].Customers -Where "Industry = 'Floppy Disks'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my[Hive].[Default].CustomersData.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 object in the result set, and we do not necessarily want that information in our CSV file.
However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:
PS C:\> $conn = Connect-Presto -Server '127.0.0.1' -Port 9200 PS C:\> $row = Select-Presto -Connection $conn -Table "[Hive].[Default].Customers" -Columns (Id, Name) -Where "Industry = 'Floppy Disks'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "[Hive].[Default].Customers", "Columns": [ ], "Id": "MyId", "Name": "MyName" }
Deleting Data
The following line deletes any records that match the criteria:
Select-Presto -Connection $conn -Table [Hive].[Default].Customers -Where "Industry = 'Floppy Disks'" | Remove-Presto
Modifying 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:\My[Hive].[Default].CustomersUpdates.csv | %{ $record = Select-Presto -Connection $conn -Table [Hive].[Default].Customers -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-Presto -Connection $conn -Table [Hive].[Default].Customers -Columns @("Id","Name") -Values @($_.Id, $_.Name) -Where "Id = `'$_.Id`'" }else{ Add-Presto -Connection $conn -Table [Hive].[Default].Customers -Columns @("Id","Name") -Values @($_.Id, $_.Name) } }