It is possible to use the values in your service account as string for authentication. You can use BigQueryOptions and pass a credentials object. Credentials object will need client_email
and private_key
which can be found in your service account json.
Using the sample code you linked in your question, BigQueryOptions
can be implemented in this manner.
const creds = {
client_email: 'your_service_account@project-id.iam.gserviceaccount.com',
private_key: '-----BEGIN PRIVATE KEY-----\nxxxxxxxxxxxxxxxxxxx\n-----END PRIVATE KEY-----\n'
};
const bigquery = new BigQuery(credentials=creds);
The whole code will be:
const {BigQuery} = require('@google-cloud/bigquery');
const creds = {
client_email: 'your_service_account@project-id.iam.gserviceaccount.com',
private_key: '-----BEGIN PRIVATE KEY-----\nxxxxxxxxxxxxxxxxxxx\n-----END PRIVATE KEY-----\n'
};
const bigquery = new BigQuery(credentials=creds);
async function query() {
// Queries the U.S. given names dataset for the state of Texas.
const query = `SELECT name
FROM \`bigquery-public-data.usa_names.usa_1910_2013\`
WHERE state = 'TX'
LIMIT 100`;
// For all options, see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query
const options = {
query: query,
// Location must match that of the dataset(s) referenced in the query.
location: 'US',
};
// Run the query as a job
const [job] = await bigquery.createQueryJob(options);
console.log(`Job ${job.id} started.`);
// Wait for the query to finish
const [rows] = await job.getQueryResults();
// Print the results
console.log('Rows:');
rows.forEach(row => console.log(row));
}
query()
Snippet of the output:
