Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →How to pipe BigQuery Data to CSV in PowerShell
Use standard PowerShell cmdlets to access BigQuery tables.
The CData Cmdlets Module for BigQuery is a standard PowerShell module offering straightforward integration with BigQuery. Below, you will find examples of using our GoogleBigQuery Cmdlets with native PowerShell cmdlets.
About BigQuery Data Integration
CData simplifies access and integration of live Google BigQuery data. Our customers leverage CData connectivity to:
- Simplify access to BigQuery with broad out-of-the-box support for authentication schemes, including OAuth, OAuth JWT, and GCP Instance.
- Enhance data workflows with Bi-directional data access between BigQuery and other applications.
- Perform key BigQuery actions like starting, retrieving, and canceling jobs; deleting tables; or insert job loads through SQL stored procedures.
Most CData customers are using Google BigQuery as their data warehouse and so use CData solutions to migrate business data from separate sources into BigQuery for comprehensive analytics. Other customers use our connectivity to analyze and report on their Google BigQuery data, with many customers using both solutions.
For more details on how CData enhances your Google BigQuery experience, check out our blog post: https://www.cdata.com/blog/what-is-bigquery
Getting Started
Creating a Connection to Your BigQuery Data
Google uses the OAuth authentication standard. To access Google APIs on behalf of individual users, you can use the embedded credentials or you can register your own OAuth app.
OAuth also enables you to use a service account to connect on behalf of users in a Google Apps domain. To authenticate with a service account, you will need to register an application to obtain the OAuth JWT values.
In addition to the OAuth values, you will need to specify the DatasetId and ProjectId. See the "Getting Started" chapter of the help documentation for a guide to using OAuth.
$conn = Connect-GoogleBigQuery -DataSetId "$DataSetId" -ProjectId "$ProjectId"
Selecting Data
Follow the steps below to retrieve data from the Orders table and pipe the result into to a CSV file:
Select-GoogleBigQuery -Connection $conn -Table Orders | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myOrdersData.csv -NoTypeInformation
You will notice that we piped the results from Select-GoogleBigQuery into a Select-Object cmdlet and excluded some properties before piping them into an Export-Csv cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each "row" in the result set, and we do not necessarily want that information in our CSV file.
The Connection, Table, and Columns are appended to the results in order to facilitate piping results from one of the CData Cmdlets directly into another one.Deleting Data
The following line deletes any records that match the criteria:
Select-GoogleBigQuery -Connection $conn -Table Orders -Where "ShipCity = New York" | Remove-GoogleBigQuery
Inserting and Updating Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into BigQuery, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyOrdersUpdates.csv | %{ $record = Select-GoogleBigQuery -Connection $GoogleBigQuery -Table Orders -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-GoogleBigQuery -Connection $googlebigquery -Table Orders -Columns ("OrderName","Freight") -Values ($_.OrderName, $_.Freight) -Where ("Id = `'"+$_.Id+"`'") }else{ Add-GoogleBigQuery -Connection $googlebigquery -Table Orders -Columns ("OrderName","Freight") -Values ($_.OrderName, $_.Freight) } }
As always, our goal is to simplify the way you connect to data. With cmdlets users can install a data module, set the connection properties, and start building. Download Cmdlets and start working with your data in PowerShell today!