![]() |
VOOZH | about |
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.
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:
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:
For more information, refer to the Setting Up OAuthClient Authentication section in the Help documentation.
To authenticate using the OAuth code type with PKCE (Proof Key for Code Exchange), set the following properties:
For more information, refer to the Help documentation.
Install the module:
Install-Module LakebaseCmdlets
Connect:
$lakebase = Connect-Lakebase -DatabricksInstance "$DatabricksInstance" -Server "$Server" -Port "$Port" -Database "$Database" -InitiateOAuth "$InitiateOAuth"
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'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Lakebase\lib\System.Data.CData.Lakebase.dll")
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()
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 -Connection $Lakebase -Columns @('ShipName','ShipCity') -Values @('MyShipName', 'MyShipCity') -Table Orders -Id "MyId"
$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()
Add-Lakebase -Connection $Lakebase -Table Orders -Columns @("ShipName", "ShipCity") -Values @("MyShipName", "MyShipCity")
$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()
Remove-Lakebase -Connection $Lakebase -Table "Orders" -Id "MyId"
$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()
CodeProject
Download a free trial of the Lakebase Data Provider to get started:
Download NowLearn more:
👁 Lakebase IconRapidly create and deploy powerful .NET applications that integrate with Lakebase.