Google BigQuery Connector
Documentation
Version: 12
Documentation

Google BigQuery Connector - Using T-SQL / Linked Server


T-SQL is a programming language used for managing and querying data in Microsoft SQL Server databases. We provides tools for data integration, automation, and connectivity.

If you want to use T-SQL with ZappySys, you can use our Connector, which allows you to connect to SQL Server databases and execute T-SQL queries from Google BigQuery.

Create Data Source in ZappySys Data Gateway based on API Driver

  1. Download and install ZappySys ODBC PowerPack.

  2. Search for gateway in start menu and Open ZappySys Data Gateway:
    Open ZappySys Data Gateway

  3. Go to Users Tab to add our first Gateway user. Click Add; we will give it a name tdsuser and enter password you like to give. Check Admin option and click OK to save. We will use these details later when we create linked server:
    ZappySys Data Gateway - Add User

  4. Now we are ready to add a data source. Click Add, give data source a name (Copy this name somewhere, we will need it later) and then select Native - ZappySys API Driver. Finally, click OK. And it will create the Data Set for it and open the ZS driver UI.

    GoogleBigqueryDSN

    ZappySys Data Gateway - Add Data Source

  5. When the Configuration window appears give your data source a name if you haven't done that already, then select "Google BigQuery" from the list of Popular Connectors. If "Google BigQuery" is not present in the list, then click "Search Online" and download it. Then set the path to the location where you downloaded it. Finally, click Continue >> to proceed with configuring the DSN:

    GoogleBigqueryDSN
    Google BigQuery
    ODBC DSN Template Selection

  6. Now it's time to configure the Connection Manager. Select Authentication Type, e.g. Token Authentication. Then select API Base URL (in most cases, the default one is the right one). More info is available in the Authentication section.

    User accounts represent a developer, administrator, or any other person who interacts with Google APIs and services. User accounts are managed as Google Accounts, either with Google Workspace or Cloud Identity. They can also be user accounts that are managed by a third-party identity provider and federated with Workforce Identity Federation. [API reference]

    Steps how to get and use Google BigQuery credentials

    Follow these steps on how to create Client Credentials (User Account principle) to authenticate and access BigQuery API in SSIS package or ODBC data source:

    WARNING: If you are planning to automate processes, we recommend that you use a Service Account authentication method. In case, you still need to use User Account, then make sure you use a system/generic account (e.g. automation@my-company.com). When you use a personal account which is tied to a specific employee profile and that employee leaves the company, the token may become invalid and any automated processes using that token will start to fail.

    Step-1: Create project

    This step is optional, if you already have a project in Google Cloud and can use it. However, if you don't, proceed with these simple steps to create one:

    1. First of all, go to Google API Console.

    2. Then click Select a project button and then click NEW PROJECT button:

      Start creating a new project in Google Cloud
    3. Name your project and click CREATE button:

      Create a new project in Google Cloud
    4. Wait until the project is created:

      Wait until project is created in Google Cloud
    5. Done! Let's proceed to the next step.

    Step-2: Enable Google Cloud APIs

    In this step we will enable BigQuery API and Cloud Resource Manager API:

    1. Select your project on the top bar:

      Select project in Google Cloud
    2. Then click the "hamburger" icon on the top left and access APIs & Services:

      Access APIs and services in Google Cloud
    3. Now let's enable several APIs by clicking ENABLE APIS AND SERVICES button:

      Enable API for project in Google Cloud
    4. In the search bar search for bigquery api and then locate and select BigQuery API:

      Search for API in Google Cloud
    5. If BigQuery API is not enabled, enable it:

      Enable Google BigQuery API
    6. Then repeat the step and enable Cloud Resource Manager API as well:

      Enable Cloud Resource Manager API
    7. Done! Let's proceed to the next step.

    Step-3: Create OAuth application

    1. First of all, click the "hamburger" icon on the top left and then hit VIEW ALL PRODUCTS:

      View all products in Google Cloud
    2. Then access Google Auth Platform to start creating an OAuth application:

      Open Google Auth Platform in Google Cloud
    3. Start by pressing GET STARTED button:

      Start creating an app in Google Cloud
    4. Next, continue by filling in App name and User support email fields:

      Fill app info in Google Cloud
    5. Choose Internal option, if it's enabled, otherwise select External:

      Choose app audience in Google Cloud
    6. Optional step if you used Internal option in the previous step. Nevertheless, if you had to use External option, then click ADD USERS to add a user:

      Add test user in Google Cloud app
    7. Then add your contact Email address:

      Enter app contact info in Google Cloud
    8. Finally, check the checkbox and click CREATE button:

      Create app in Google Cloud
    9. Done! Let's create Client Credentials in the next step.

    Step-4: Create Client Credentials

    1. In Google Auth Platform, select Clients menu item and click CREATE CLIENT button:

      Start creating app client in Google Cloud
    2. Choose Desktop app as Application type and name your credentials:

      Create OAuth app client in Google Cloud
    3. Continue by opening the created credentials:

      View app client credentials in Google Cloud
    4. Finally, copy Client ID and Client secret for the later step:

      Use client ID and secret to read Google REST API data
    5. Done! We have all the data needed for authentication, let's proceed to the last step!

    Step-5: Configure connection

    1. Now go to SSIS package or ODBC data source and use previously copied values in User Account authentication configuration:

      • In the ClientId field paste the Client ID value.
      • In the ClientSecret field paste the Client secret value.
    2. Press Generate Token button to generate Access and Refresh Tokens.

    3. Then choose ProjectId from the drop down menu.

    4. Continue by choosing DatasetId from the drop down menu.

    5. Finally, click Test Connection to confirm the connection is working.

    6. Done! Now you are ready to use Google BigQuery Connector!

    Fill in all required parameters and set optional parameters if needed:

    GoogleBigqueryDSN
    Google BigQuery
    User Account [OAuth]
    https://www.googleapis.com/bigquery/v2
    Required Parameters
    UseCustomApp Fill-in the parameter...
    ProjectId (Choose after [Generate Token] clicked) Fill-in the parameter...
    DatasetId (Choose after [Generate Token] clicked and ProjectId selected) Fill-in the parameter...
    Optional Parameters
    ClientId
    ClientSecret
    Scope https://www.googleapis.com/auth/bigquery https://www.googleapis.com/auth/bigquery.insertdata https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/cloud-platform.read-only https://www.googleapis.com/auth/devstorage.full_control https://www.googleapis.com/auth/devstorage.read_only https://www.googleapis.com/auth/devstorage.read_write
    RetryMode RetryWhenStatusCodeMatch
    RetryStatusCodeList 429|503
    RetryCountMax 5
    RetryMultiplyWaitTime True
    Job Location
    Redirect URL (Only for Web App)
    ODBC DSN Oauth Connection Configuration

    Service accounts are accounts that do not represent a human user. They provide a way to manage authentication and authorization when a human is not directly involved, such as when an application needs to access Google Cloud resources. Service accounts are managed by IAM. [API reference]

    Steps how to get and use Google BigQuery credentials

    Follow these steps on how to create Service Account to authenticate and access BigQuery API in SSIS package or ODBC data source:

    Step-1: Create project

    This step is optional, if you already have a project in Google Cloud and can use it. However, if you don't, proceed with these simple steps to create one:

    1. First of all, go to Google API Console.

    2. Then click Select a project button and then click NEW PROJECT button:

      Start creating a new project in Google Cloud
    3. Name your project and click CREATE button:

      Create a new project in Google Cloud
    4. Wait until the project is created:

      Wait until project is created in Google Cloud
    5. Done! Let's proceed to the next step.

    Step-2: Enable Google Cloud APIs

    In this step we will enable BigQuery API and Cloud Resource Manager API:

    1. Select your project on the top bar:

      Select project in Google Cloud
    2. Then click the "hamburger" icon on the top left and access APIs & Services:

      Access APIs and services in Google Cloud
    3. Now let's enable several APIs by clicking ENABLE APIS AND SERVICES button:

      Enable API for project in Google Cloud
    4. In the search bar search for bigquery api and then locate and select BigQuery API:

      Search for API in Google Cloud
    5. If BigQuery API is not enabled, enable it:

      Enable Google BigQuery API
    6. Then repeat the step and enable Cloud Resource Manager API as well:

      Enable Cloud Resource Manager API
    7. Done! Let's proceed to the next step and create a service account.

    Step-3: Create Service Account

    Use the steps below to create a Service Account in Google Cloud:

    1. First of all, go to IAM & Admin in Google Cloud console:

      Access IAM & Admin in Google Cloud
    2. Once you do that, click Service Accounts on the left side and click CREATE SERVICE ACCOUNT button:

      Start creating service account in Google Cloud
    3. Then name your service account and click CREATE AND CONTINUE button:

      Create service account in Google Cloud
    4. Continue by clicking Select a role dropdown and start granting service account BigQuery Admin and Project Viewer roles:

      Start granting service account project roles in Google Cloud
    5. Find BigQuery group on the left and then click on BigQuery Admin role on the right:

      Grant service account BigQuery Admin role
    6. Then click ADD ANOTHER ROLE button, find Project group and select Viewer role:

      Grant service account project viewer role
    7. Finish adding roles by clicking CONTINUE button:

      Finish granting service account project roles in Google Cloud
      You can always add or modify permissions later in IAM & Admin.
    8. Finally, in the last step, just click button DONE:

      Finish configuring service account in Google Cloud
    9. Done! We are ready to add a Key to this service account in the next step.

    Step-4: Add Key to Service Account

    We are ready to add a Key (P12 certificate) to the created Service Account:

    1. In Service Accounts open newly created service account:

      Open service account in Google Cloud
    2. Next, copy email address of your service account for the later step:

      Copy service account email address in Google Cloud
    3. Continue by selecting KEYS tab, then press ADD KEY dropdown, and click Create new key menu item:

      Start creating key for service account in Google Cloud
    4. Finally, select P12 option and hit CREATE button:

      Create P12 key for service account in Google Cloud
    5. P12 certificate downloads into your machine. We have all the data needed for authentication, let's proceed to the last step!

    Step-5: Configure connection

    1. Now go to SSIS package or ODBC data source and configure these fields in Service Account authentication configuration:

      • In the Service Account Email field paste the service account Email address value you copied in the previous step.
      • In the Service Account Private Key Path (i.e. *.p12) field use downloaded certificate's file path.
    2. Done! Now you are ready to use Google BigQuery Connector!

    Fill in all required parameters and set optional parameters if needed:

    GoogleBigqueryDSN
    Google BigQuery
    Service Account [OAuth]
    https://www.googleapis.com/bigquery/v2
    Required Parameters
    Service Account Email Fill-in the parameter...
    Service Account Private Key Path (i.e. *.p12) Fill-in the parameter...
    ProjectId Fill-in the parameter...
    DatasetId (Choose after ProjectId) Fill-in the parameter...
    Optional Parameters
    Scope https://www.googleapis.com/auth/bigquery https://www.googleapis.com/auth/bigquery.insertdata https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/cloud-platform.read-only https://www.googleapis.com/auth/devstorage.full_control https://www.googleapis.com/auth/devstorage.read_only https://www.googleapis.com/auth/devstorage.read_write
    RetryMode RetryWhenStatusCodeMatch
    RetryStatusCodeList 429
    RetryCountMax 5
    RetryMultiplyWaitTime True
    Job Location
    Impersonate As (Enter Email Id)
    ODBC DSN Oauth Connection Configuration

  7. Once the data source has been configured, you can preview data. Select the Preview tab and use settings similar to the following to preview data:
    ODBC ZappySys Data Source Preview

  8. Click OK to finish creating the data source.

Read data in SQL Server from the ZappySys Data Gateway

  1. To read the data in SQL Server the first thing you have to do is create a Linked Server. Go to SQL Server Management Studio and configure it in a similar way:
    SSMS SQL Server Configure Linked Server

  2. Then click on Security option and configure username we created in ZappySys Data Gateway in one of the previous steps:
    SSMS SQL Server Configure Linked Server User Name

  3. Optional: Under the Server Options, Enable RPC and RPC Out and Disable Promotion of Distributed Transactions(MSDTC).

    RPC and MSDTC Settings

    You need to enable RPC Out if you plan to use EXEC(...) AT [MY_LINKED_SERVER_NAME] rather than OPENQUERY.
    If don't enabled it, you will encounter the Server 'MY_LINKED_SERVER_NAME' is not configured for RPC error.

    Query Example:

    EXEC('Select * from Products') AT [MY_LINKED_SERVER_NAME]


    If you plan to use 'INSERT INTO...EXEC(....) AT [MY_LINKED_SERVER_NAME]' in that case you need to Disable Promotion of Distributed Transactions(MSDTC).
    If don't disabled it, you will encounter the The operation could not be performed because OLE DB provider "SQLNCLI11" for linked server "MY_LINKED_SERVER_NAME" was unable to begin a distributed transaction. error.

    Query Example:

    Insert Into dbo.Products
     EXEC('Select * from Products') AT [MY_LINKED_SERVER_NAME]
    


  4. Finally, open a new query and execute a query we saved in one of the previous steps:

    SELECT * FROM OPENQUERY([MY_LINKED_SERVER_NAME], 'SELECT * FROM Products');

    SSMS SQL Server Query Data Results

Create Linked Server using Code

In previous section you saw how to create a Linked Server from UI. You can do similar action by code too (see below). Run below script after changing necessary parameters. Assuming your Data Source name on ZappySys Data Gateway UI is 'GoogleBigqueryDSN'

    USE [master]
    GO
    --///////////////////////////////////////////////////////////////////////////////////////
    --Run below code in SSMS to create Linked Server and use ZappySys Drivers in SQL Server
    --///////////////////////////////////////////////////////////////////////////////////////

    //Replace YOUR_GATEWAY_USER, YOUR_GATEWAY_PASSWORD
    //Replace localhost with IP/Machine name if ZappySys Gateway Running on different machine other than SQL Server
    //Replace Port 5000 if you configured gateway on a different port


    --1. Configure your gateway service as per this article https://zappysys.com/links?id=10036

    --2. Make sure you have SQL Server Installed. You can download FREE SQL Server Express Edition from here if you dont want to buy Paid version https://www.microsoft.com/en-us/sql-server/sql-server-editions-express

    --Uncomment below if you like to drop linked server if it already exists
    --EXEC master.dbo.sp_dropserver @server=N'LS_GoogleBigqueryDSN', @droplogins='droplogins'

    --3. Create new linked server

    EXEC master.dbo.sp_addlinkedserver
      @server = N'LS_GoogleBigqueryDSN'  --Linked server name (this will be used in OPENQUERY sql
    , @srvproduct=N''
    ---- For MSSQL 2012,2014,2016 and 2019 use below (SQL Server Native Client 11.0)---
    , @provider=N'SQLNCLI11'
    ---- For MSSQL 2022 or higher use below (Microsoft OLE DB Driver for SQL Server)---
    --, @provider=N'MSOLEDBSQL'
    , @datasrc=N'localhost,5000' --//Machine / Port where Gateway service is running
    , @provstr=N'Network Library=DBMSSOCN;'
    , @catalog=N'GoogleBigqueryDSN' --Data source name you gave on Gateway service settings

    --4. Attach gateway login with linked server

    EXEC master.dbo.sp_addlinkedsrvlogin
    @rmtsrvname=N'LS_GoogleBigqueryDSN'  --linked server name
    , @useself=N'False'
    , @locallogin=NULL
    , @rmtuser=N'YOUR_GATEWAY_USER' --enter your Gateway user name
    , @rmtpassword='YOUR_GATEWAY_PASSWORD'  --enter your Gateway user's password
    GO

    --5. Enable RPC OUT (This is Optional - Only needed if you plan to use EXEC(...) AT YourLinkedServerName rather than OPENQUERY
    EXEC sp_serveroption 'LS_GoogleBigqueryDSN', 'rpc', true;
    EXEC sp_serveroption 'LS_GoogleBigqueryDSN', 'rpc out', true;

    --Disable MSDTC - Below needed to support INSERT INTO from EXEC AT statement
    EXEC sp_serveroption 'LS_GoogleBigqueryDSN', 'remote proc transaction promotion', false;

    --Increase query timeout if query is going to take longer than 10 mins (Default timeout is 600 seconds)
    --EXEC sp_serveroption 'LS_GoogleBigqueryDSN', 'query timeout', 1200;
    GO