Google BigQuery Connector
Documentation
Version: 11
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.

    Steps to get Google BigQuery Credentials
    This connection can be configured using two ways. Use Default App (Created by ZappySys) OR Use Custom App created by you.
    To use minimum settings you can start with ZappySys created App. Just change UseCustomApp=false on the properties grid so you dont need ClientID / Secret. When you click Generate Token you might see warning about App is not trusted (Simply Click Advanced Link to expand hidden section and then click Go to App link to Proceed).

    To register custom App, perform the following steps (Detailed steps found in the help link at the end)

    1. Go to Google API Console
    2. From the Project Dropdown (usually found at the top bar) click Select Project
    3. On Project Propup click CREATE PROJECT
    4. Once project is created you can click Select Project to switch the context (You can click on Notification link or Choose from Top Dropdown)
    5. Click ENABLE APIS AND SERVICES
    6. Now we need to Enable two APIs one by one (BigQuery API and Cloud Resource Manager API).
    7. Search BigQuery API. Select and click ENABLE
    8. Search Cloud Resource Manager API. Select and click ENABLE
    9. Go to back to main screen of Google API Console
    10. Click OAuth consent screen Tab. Enter necessary details and Save.

      1. Choose Testing as Publishing status
      2. Set application User type to Internal, if possible
      3. If MAKE INTERNAL option is disabled, then add a user in Test users section, which you will use in authentication process when generating Access and Refresh tokens
    11. Click Credentials Tab
    12. Click CREATE CREDENTIALS (some where in topbar) and select OAuth Client ID option.
    13. When prompted Select Application Type as Desktop App and click Create to receive your ClientID and Secret. Later on you can use this information now to configure Connection with UseCustomApp=true.
    14. Go to OAuth Consent Screen tab. Under Publishing Status click PUBLISH APP to ensure your refresh token doesnt expire often. If you planning to use App for Private use then do not have to worry about Verification Status after Publish.

    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 Fill in the parameter...
    ClientSecret Fill in the parameter...
    Scope Fill in the parameter...
    RetryMode Fill in the parameter...
    RetryStatusCodeList Fill in the parameter...
    RetryCountMax Fill in the parameter...
    RetryMultiplyWaitTime Fill in the parameter...
    Job Location Fill in the parameter...
    Redirect URL (Only for Web App) Fill in the parameter...
    ODBC DSN Oauth Connection Configuration
    Steps to get Google BigQuery Credentials
    Use these steps to authenticate as service account rather than Google / GSuite User. Learn more about service account here

    Basically to call Google API as Service account we need to perform following steps listed in 3 sections (Detailed steps found in the help link at the end)

    Create Project

    First thing is create a Project so we can call Google API. Skip this section if you already have Project (Go to next section)
    1. Go to Google API Console
    2. From the Project Dropdown (usually found at the top bar) click Select Project
    3. On Project Propup click CREATE PROJECT
    4. Once project is created you can click Select Project to switch the context (You can click on Notification link or Choose from Top Dropdown)
    5. Click ENABLE APIS AND SERVICES
    6. Now we need to Enable two APIs one by one (BigQuery API and Cloud Resource Manager API).
    7. Search BigQuery API. Select and click ENABLE
    8. Search Cloud Resource Manager API. Select and click ENABLE

    Create Service Account

    Once Project is created and APIs are enabled we can now create a service account under that project. Service account has its ID which looks like some email ID (not to confuse with Google /Gmail email ID)
    1. Go to Create Service Account
    2. From the Project Dropdown (usually found at the top bar) click Select Project
    3. Enter Service account name and Service account description
    4. Click on Create. Now you should see an option to assign Service Account permissions (See Next Section).

    Give Permission to Service Account

    By default service account cant access BigQuery data or List BigQuery Projects so we need to give that permission using below steps.
    1. After you Create Service Account look for Permission drop down in the Wizard.
    2. Choose BigQuery -> BigQuery Admin role so we can read/write data. (NOTE: If you just need read only access then you can choose BigQuery Data Viewer)
    3. Now choose one more Project -> Viewer and add that role so we can query Project Ids.
    4. Click on Continue. Now you should see an option to Create Key (See Next Section).

    Create Key (P12)

    Once service account is created and Permission is assigned we need to create key file.
    1. In the Cloud Console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, then click Create new key.
    4. Click Create and select P12 format. A P12 key file is downloaded to your computer. We will use this file in our API connection.
    5. Click Close.
    6. Now you may use downloaded *.p12 key file as secret file and Service Account Email as Client ID (e.g. some_name@some_name.iam.gserviceaccount.com).

    Manage Permissions / Give Access to Other Projects

    We saw how to add permissions for Service Account during Account Creation Wizard but if you ever wish to edit after its created or you wish to give permission for other projects then perform forllowing steps.
    1. From the top Select Project for which you like to edit Permission.
    2. Go to IAM Menu option (here)
      Link to IAM: https://console.cloud.google.com/iam-admin/iam
    3. Goto Permissions tab. Over there you will find ADD button.
    4. Enter Service account email for which you like to grant permission. Select role you wish to assign.

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

    GoogleBigQueryDSN
    Google BigQuery
    Service Account (Using Private Key File) [OAuth]
    https://www.googleapis.com/bigquery/v2
    Required Parameters
    Service Account Email Fill in the parameter...
    P12 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 Fill in the parameter...
    RetryMode Fill in the parameter...
    RetryStatusCodeList Fill in the parameter...
    RetryCountMax Fill in the parameter...
    RetryMultiplyWaitTime Fill in the parameter...
    Job Location Fill in the parameter...
    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