![]() |
VOOZH | about |
The CData Cmdlets for Google Spanner 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 Google Spanner.
The Cmdlets are not only a PowerShell interface to Google Spanner, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Google Spanner data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Google Spanner. To access Google Spanner data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Google Spanner.
Once you have acquired the necessary connection properties, accessing Google Spanner data in PowerShell can be enabled in three steps.
Google Spanner uses the OAuth authentication standard. To authenticate using OAuth, you can use the embedded credentials or register an app with Google.
See the Getting Started guide in the CData driver documentation for more information.
Install the module:
Install-Module GoogleSpannerCmdlets
Connect:
$googlespanner = Connect-GoogleSpanner -ProjectId "$ProjectId" -InstanceId "$InstanceId" -Database "$Database" -InitiateOAuth "$InitiateOAuth"
Search for and retrieve data:
$id = "1" $customer = Select-GoogleSpanner -Connection $googlespanner -Table "Customer" -Where "Id = `'$Id`'" $customer
You can also use the Invoke-GoogleSpanner cmdlet to execute SQL commands:
$customer = Invoke-GoogleSpanner -Connection $googlespanner -Query 'SELECT * FROM Customer WHERE Id = @Id' -Params @{'@Id'='1'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Google Spanner\lib\System.Data.CData.GoogleSpanner.dll")
Connect to Google Spanner:
$conn= New-Object System.Data.CData.GoogleSpanner.GoogleSpannerConnection("ProjectId='project1';InstanceId='instance1';Database='db1';InitiateOAuth=GETANDREFRESH;")
$conn.Open()
Instantiate the GoogleSpannerDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Name, TotalDue from Customer"
$da= New-Object System.Data.CData.GoogleSpanner.GoogleSpannerDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.name $_.totaldue
}
Update-GoogleSpanner -Connection $GoogleSpanner -Columns @('Name','TotalDue') -Values @('MyName', 'MyTotalDue') -Table Customer -Id "MyId"
$cmd = New-Object System.Data.CData.GoogleSpanner.GoogleSpannerCommand("UPDATE Customer SET Id='1' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.GoogleSpanner.GoogleSpannerParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-GoogleSpanner -Connection $GoogleSpanner -Table Customer -Columns @("Name", "TotalDue") -Values @("MyName", "MyTotalDue")
$cmd = New-Object System.Data.CData.GoogleSpanner.GoogleSpannerCommand("INSERT INTO Customer (Id) VALUES (@myId)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.GoogleSpanner.GoogleSpannerParameter("@myId","1")))
$cmd.ExecuteNonQuery()
Remove-GoogleSpanner -Connection $GoogleSpanner -Table "Customer" -Id "MyId"
$cmd = New-Object System.Data.CData.GoogleSpanner.GoogleSpannerCommand("DELETE FROM Customer WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.GoogleSpanner.GoogleSpannerParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the Spanner Data Provider to get started:
Download NowLearn more:
👁 Google Cloud Spanner IconRapidly create and deploy powerful .NET applications that integrate with Google Cloud Spanner databases.