Automate Google Spanner Integration Tasks from PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Google Spanner ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Google Cloud Spanner databases.



Are you looking for a quick and easy way to access Google Spanner data from PowerShell? We show how to use the Cmdlets for Google Spanner and the CData ADO.NET Provider for Google Spanner to connect to Google Spanner data and synchronize, automate, download, and more.

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.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the Google Spanner API, 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.

After obtaining the needed connection properties, accessing Google Spanner data in PowerShell consists of three basic 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.

PowerShell

  1. Install the module:

    Install-Module GoogleSpannerCmdlets
  2. Connect:

    $googlespanner = Connect-GoogleSpanner -ProjectId "$ProjectId" -InstanceId "$InstanceId" -Database "$Database"
  3. 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'}

ADO.NET

  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")
  2. Connect to Google Spanner:

    $conn= New-Object System.Data.CData.GoogleSpanner.GoogleSpannerConnection("ProjectId='project1';InstanceId='instance1';Database='db1';InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. 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 Google Spanner Data

PowerShell

Update-GoogleSpanner -Connection $GoogleSpanner -Columns @('Name','TotalDue') -Values @('MyName', 'MyTotalDue') -Table Customer -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.GoogleSpanner.GoogleSpannerCommand("UPDATE Customer SET Id='1' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.GoogleSpanner.GoogleSpannerParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Google Spanner Data

PowerShell

Add-GoogleSpanner -Connection $GoogleSpanner -Table Customer -Columns @("Name", "TotalDue") -Values @("MyName", "MyTotalDue")

ADO.NET

$cmd = New-Object System.Data.CData.GoogleSpanner.GoogleSpannerCommand("INSERT INTO Customer (Id) VALUES (@myId)", $conn) $cmd.Parameters.Add(new System.Data.CData.GoogleSpanner.GoogleSpannerParameter("@myId","1")) $cmd.ExecuteNonQuery()

Delete Google Spanner Data

PowerShell

Remove-GoogleSpanner -Connection $GoogleSpanner -Table "Customer" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.GoogleSpanner.GoogleSpannerCommand("DELETE FROM Customer WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.GoogleSpanner.GoogleSpannerParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()