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 ApachePhoenix 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 ApachePhoenixCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module ApachePhoenixCmdlets;
You can then use the Connect-ApachePhoenix cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-ApachePhoenix -URL 'http://localhost:8765'
Connecting to Apache Phoenix
The CData Cmdlets PowerShell Module for Apache Phoenix connects to Apache Phoenix via the Phoenix Query Server. Set the URL connection property to connect to Apache Phoenix.
The URL property will typically be the host name or IP address of the server hosting Apache Phoenix followed by the port, for example: http://localhost:8765.
Authenticating to Apache Phoenix
By default, no authentication will be used (plain). If authentication is configured for your server, you can configure one of the following authentication methods.
Basic
If your instance of Apache Phoenix has set up the basic authentication layer, set the following to authenticate:
- AuthScheme: Set this to Basic.
- User: Set this to the user of your Apache Phoenix instance.
- Password: Set this to the password of your Apache Phoenix instance.
Azure HDInsight
To authenticate to Azure HDInsight, set the following:
- AuthScheme: Set this to AzureHDInsight.
- User: Set this to the cluster username that you specified when creating the cluster on Azure HDInsight.
- Password: Set this to the cluster password that you specified when creating the cluster on Azure HDInsight.
- ClusterName: Set this to the name of the cluster containing your Azure HDInsight instance.
All calls are sent to a gateway which picks one of the nodes to handle the request. Accessing the specific nodes is possible if you are running within the internal virtual network of the cluster.
Kerberos
Set the AuthScheme to Negotiate. See Using Kerberos for details on how to authenticate with Kerberos.
Retrieving Data
After you have created a connection, you can use the other cmdlets to perform operations that you would normally expect to be able
to perform against a relational database. The Select-ApachePhoenix cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-ApachePhoenix -Connection $conn -Table "[SampleCatalog_1].[SampleSchema_1].SampleTable_1" -Columns @("Id, Name") -Where "Industry='Floppy Disks'"The Invoke-ApachePhoenix 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-ApachePhoenix -Connection $conn -Table [SampleCatalog_1].[SampleSchema_1].SampleTable_1 -Where "Industry = 'Floppy Disks'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my[SampleCatalog_1].[SampleSchema_1].SampleTable_1Data.csv -NoTypeInformation
You will notice that we piped the results from Select-ApachePhoenix 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-ApachePhoenix -URL 'http://localhost:8765' PS C:\> $row = Select-ApachePhoenix -Connection $conn -Table "[SampleCatalog_1].[SampleSchema_1].SampleTable_1" -Columns (Id, Name) -Where "Industry = 'Floppy Disks'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "[SampleCatalog_1].[SampleSchema_1].SampleTable_1", "Columns": [ ], "Id": "MyId", "Name": "MyName" }
Deleting Data
The following line deletes any records that match the criteria:
Select-ApachePhoenix -Connection $conn -Table [SampleCatalog_1].[SampleSchema_1].SampleTable_1 -Where "Industry = 'Floppy Disks'" | Remove-ApachePhoenix
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Apache Phoenix, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\My[SampleCatalog_1].[SampleSchema_1].SampleTable_1Updates.csv | %{ $record = Select-ApachePhoenix -Connection $conn -Table [SampleCatalog_1].[SampleSchema_1].SampleTable_1 -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-ApachePhoenix -Connection $conn -Table [SampleCatalog_1].[SampleSchema_1].SampleTable_1 -Columns @("Id","Name") -Values @($_.Id, $_.Name) -Where "Id = `'$_.Id`'" }else{ Add-ApachePhoenix -Connection $conn -Table [SampleCatalog_1].[SampleSchema_1].SampleTable_1 -Columns @("Id","Name") -Values @($_.Id, $_.Name) } }