Cmdlets for Redis

Build 24.0.9064

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 Redis 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 RedisCmdlets

The following line is then added to your profile, loading the cmdlets on the next session:

Import-Module RedisCmdlets;

You can then use the Connect-Redis cmdlet to create a connection object that can be passed to other cmdlets:

$conn = Connect-Redis -Server '127.0.0.1' -Password 'test'

Connecting to Redis

Set the Server connection property to the name or address of the server your Redis instance is running on.

If your Redis server is running on a port other than the default (6379), you can specify your port in the Port property.

Authenticating to Redis

The cmdlet supports Password and ACL authentication. Connections to Redis instances that aren't password protected are supported as well.

No Authentication

Set the AuthScheme property to None. This indicates the Redis instance is not password protected (using the requirepass directive in the configuration file).

Password

Set the AuthScheme property to Password and set the Password property to the password used to authenticate with a password protected Redis instance using the Redis AUTH command.

ACL (Access Control List)

Set the following to connect:

  • AuthScheme: Set this to ACL.
  • User: Set this to the username you use to authenticate with Redis ACL.
  • Password: Set this to the password you use to authenticate with Redis ACL.

Securing Redis Connections

You can set UseSSL to negotiate SSL/TLS encryption when you connect.

Retrieving Data

The Select-Redis cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-Redis -Connection $conn -Table "Customers" -Columns @("City, CompanyName") -Where "Country='US'"
The Invoke-Redis 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-Redis -Connection $conn -Table Customers -Where "Country = 'US'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myCustomersData.csv -NoTypeInformation

You will notice that we piped the results from Select-Redis 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-Redis -Server '127.0.0.1' -Password 'test'
PS C:\> $row = Select-Redis -Connection $conn -Table "Customers" -Columns (City, CompanyName) -Where "Country = 'US'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "Customers",
  "Columns":  [

  ],
  "City":  "MyCity",
  "CompanyName":  "MyCompanyName"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-Redis -Connection $conn -Table Customers -Where "Country = 'US'" | Remove-Redis

Modifying Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Redis, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyCustomersUpdates.csv | %{
  $record = Select-Redis -Connection $conn -Table Customers -Where ("_id = `'"+$_._id+"`'")
  if($record){
    Update-Redis -Connection $conn -Table Customers -Columns @("City","CompanyName") -Values @($_.City, $_.CompanyName) -Where "_id  = `'$_._id`'"
  }else{
    Add-Redis -Connection $conn -Table Customers -Columns @("City","CompanyName") -Values @($_.City, $_.CompanyName)
  }
}

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 24.0.9064