Automate Lakebase Integration Tasks from PowerShell
The CData ADO.NET Provider for Lakebase is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Lakebase.
ADO.NET Provider
The ADO.NET Provider provides a SQL interface for Lakebase; this tutorial shows how to use the Provider to create, retrieve, update, and delete Lakebase data.
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:
- Create and configure a new service principal
- Assign permissions to the service principal
- 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.
-
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 Data
$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
$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
$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