Connect BigQuery with a Service Account
Connecting BigQuery with a service account involves using the service account credentials to authenticate and authorize access to BigQuery. This allows C3 AI applications to interact with BigQuery without requiring individual user credentials.
Prerequisites
Verify that your project includes the necessary dependencies or libraries that provide the SqlSourceSystem class.
Set up Connection to Google BigQuery using a service account
Establish a secure connection to Google BigQuery using a service account for the SQL source system "TestExternalSystem2".
Create a service account:
a. Go to the Google Cloud Console.
b. Navigate to IAM & Admin > Service Accounts.
c. Click Create Service Account.
d. Fill in the necessary details and click Create.
e. Assign the required roles (e.g., BigQuery Data Viewer).
f. Download the JSON key file.
Prepare the Service Account JSON:
a. Open the downloaded JSON key file.
b. Copy the contents of the file.
Set up variables in your code:
a. Define the name of the SQL source:
Typesystem:sysName = 'TestExternalSystem2'b. Store the service account JSON credentials:
Typevariable:content = `{ "type": "service_account", "project_id": "your_project_id", "private_key_id": "your_private_key_id", "private_key": "your_private_key", "client_email": "your_client_email", "client_id": "your_client_id", "auth_uri": "your_auth_uri", "token_uri": "your_token_uri", "auth_provider_x509_cert_url": "your_auth_provider_x509_cert_url", "client_x509_cert_url": "your_client_x509_cert_url" }`c. Set the JDBC URL:
TypeBigQuery:url = "jdbc:googlebigquery:your_connection_string"
Configure JDBC credentials for Google BigQuery using OAuthJWT
Set up and configure JDBC credentials to securely connect to Google BigQuery using OAuth 2.0 with a JSON Web Token (JWT) for authentication.
Define authentication properties:
Create a properties dictionary to specify the authentication scheme and include the service account JSON credentials.
JSONproperties = { "AuthScheme": "OAuthJWT", "OAuthJWTCertType": "GOOGLEJSONBLOB", "OAuthJWTCert": content }Build JDBC credentials:
Use the JdbcCredentials.builder() method to construct the credentials object with the necessary URL, properties, and datastore type.
JavaScriptcreds = JdbcCredentials.builder().url(url) .username("******") .password("******") .properties(properties) .datastore("googlebigquery").build();Set credentials in JDBC store:
Retrieve the JDBC store for the specified system name and set the credentials.
JavaScriptJdbcStore.forName(sysName).setCredentials(creds, ConfigOverride.APP);Mark JDBC store as external:
Indicate that the JDBC store connects to an external data source.
JavaScriptJdbcStore.forName(sysName).setExternal(ConfigOverride.APP);Test the connection:
Verify the connection to Google BigQuery by running a test query to ensure the setup is correct and functioning as expected.
Connect to Google BigQuery using SqlSourceSystem
Utilize the SqlSourceSystem to establish a connection to Google BigQuery and list the available table names.
Retrieve the SqlSourceSystem:
Use the forName method to get the SqlSourceSystem instance for the specified system name.
JavaScriptsys = SqlSourceSystem.forName(sysName)Establish the connection:
Call the connect method on the SqlSourceSystem instance to establish a connection to Google BigQuery.
JavaScriptsys.connect()List table names:
Use the listTableNames method to retrieve and display the names of the tables available in the connected BigQuery dataset.
JavaScriptsys.connect().listTableNames()Verify the connection:
Ensure that the connection is successful and the table names are correctly listed. This step helps confirm that the setup and connection are functioning as expected.