Automate Lakebase Integration Tasks from PowerShell

Jerod Johnson
Jerod Johnson
Senior Technology Evangelist
Are you in search of a quick and easy way to access Lakebase data from PowerShell? This article demonstrates how to utilize the Lakebase Cmdlets for tasks like connecting to Lakebase data, automating operations, downloading data, and more.

The CData Cmdlets for Lakebase are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Lakebase.

PowerShell Cmdlets or ADO.NET Provider?

The Cmdlets are not only a PowerShell interface to Lakebase, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Lakebase data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Lakebase. To access Lakebase data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Lakebase.

Once you have acquired the necessary connection properties, accessing Lakebase data in PowerShell can be enabled in three steps.

To connect to Databricks Lakebase, start by setting the following properties:

  • DatabricksInstance: The Databricks instance or server hostname, provided in the format instance-abcdef12-3456-7890-abcd-abcdef123456.database.cloud.databricks.com.
  • Server: The host name or IP address of the server hosting the Lakebase database.
  • Port (optional): The port of the server hosting the Lakebase database, set to 5432 by default.
  • Database (optional): The database to connect to after authenticating to the Lakebase Server, set to the authenticating user's default database by default.

OAuth Client Authentication

To authenicate using OAuth client credentials, you need to configure an OAuth client in your service principal. In short, you need to do the following:

  1. Create and configure a new service principal
  2. Assign permissions to the service principal
  3. Create an OAuth secret for the service principal

For more information, refer to the Setting Up OAuthClient Authentication section in the Help documentation.

OAuth PKCE Authentication

To authenticate using the OAuth code type with PKCE (Proof Key for Code Exchange), set the following properties:

  • AuthScheme: OAuthPKCE.
  • User: The authenticating user's user ID.

For more information, refer to the Help documentation.

PowerShell

  1. Install the module:

    Install-Module LakebaseCmdlets
  2. Connect:

    $lakebase = Connect-Lakebase  -DatabricksInstance "$DatabricksInstance" -Server "$Server" -Port "$Port" -Database "$Database" -InitiateOAuth "$InitiateOAuth"
    
  3. Search for and retrieve data:

    $shipcountry = "USA"
    $orders = Select-Lakebase -Connection $lakebase -Table "Orders" -Where "ShipCountry = `'$ShipCountry`'"
    $orders
    

    You can also use the Invoke-Lakebase cmdlet to execute SQL commands:

    $orders = Invoke-Lakebase -Connection $lakebase -Query 'SELECT * FROM Orders WHERE ShipCountry = @ShipCountry' -Params @{'@ShipCountry'='USA'}
    

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Lakebase\lib\System.Data.CData.Lakebase.dll")
        
  2. Connect to Lakebase:

     
    $conn= New-Object System.Data.CData.Lakebase.LakebaseConnection("DatabricksInstance=lakebase;Server=127.0.0.1;Port=5432;Database=my_database;InitiateOAuth=GETANDREFRESH;")
    $conn.Open()
    
  3. Instantiate the LakebaseDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT ShipName, ShipCity from Orders"
    
    $da= New-Object System.Data.CData.Lakebase.LakebaseDataAdapter($sql, $conn)
    $dt= New-Object System.Data.DataTable
    $da.Fill($dt)
    
    $dt.Rows | foreach {
    	Write-Host $_.shipname $_.shipcity
    }
      

Update Lakebase Data

PowerShell

Update-Lakebase -Connection $Lakebase -Columns @('ShipName','ShipCity') -Values @('MyShipName', 'MyShipCity') -Table Orders -Id "MyId"

ADO.NET

$cmd =  New-Object System.Data.CData.Lakebase.LakebaseCommand("UPDATE Orders SET ShipCountry='USA' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Lakebase.LakebaseParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()

Insert Lakebase Data

PowerShell

Add-Lakebase -Connection $Lakebase -Table Orders -Columns @("ShipName", "ShipCity") -Values @("MyShipName", "MyShipCity") 

ADO.NET

$cmd =  New-Object System.Data.CData.Lakebase.LakebaseCommand("INSERT INTO Orders (ShipCountry) VALUES (@myShipCountry)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Lakebase.LakebaseParameter("@myShipCountry","USA")))
$cmd.ExecuteNonQuery()

Delete Lakebase Data

PowerShell

Remove-Lakebase -Connection $Lakebase -Table "Orders" -Id "MyId"

ADO.NET

$cmd =  New-Object System.Data.CData.Lakebase.LakebaseCommand("DELETE FROM Orders WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Lakebase.LakebaseParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()

Ready to get started?

Download a free trial of the Lakebase Data Provider to get started:

 Download Now

Learn more:

Lakebase Icon Lakebase ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Lakebase.