Reputation: 51
I am trying to insert, delete, search and update in google bigquaey using node js. I have followed up the all documents in google could. But no where its mentioned, how to connect - So I have created this, --
How to connect GCP BigQuery with NodeJS. I am new here. Any help will work.
the code which I have written -
const {BigQuery} = require('@google-cloud/bigquery');
const mainFunction = () => {
insertIntoBigQuery("John", "[email protected]", "+1-23232344231");
}
async function insertIntoBigQuery(name, email, phone) {
// const insertIntoBigQuery = async(name, email, phone) => {
const bigqueryClient = new BigQuery();
// The SQL query to run
const sqlQuery = "INSERT INTO `p4tyu78.Backend_testing.Test1` VALUES ("+name+ "," +email+","+ phone+")";
const options = {
query: sqlQuery,
// Location must match that of the dataset(s) referenced in the query.
location: 'US',
};
// Run the query
const [rows] = await bigqueryClient.query(options);
console.log('Rows:');
rows.forEach(row => console.log(`${row.subject}: ${row.num_duplicates}`));
}
const updateIntoBigQuery = () => {
}
const deleteFromBigQuery = () => {
}
const searchFromBigQuery = async() => {
const bigqueryClient = new BigQuery();
// The SQL query to run
const sqlQuery = "SELECT * from `p4tyu78.Backend_testing.Test1`";
const options = {
query: sqlQuery,
// Location must match that of the dataset(s) referenced in the query.
location: 'US',
};
// Run the query
const [rows] = await bigqueryClient.query(options);
console.log('Rows:');
rows.forEach(row => console.log(`${row.subject}: ${row.num_duplicates}`));
}
mainFunction();
Upvotes: 5
Views: 4834
Reputation: 76000
Forget the API key, you can't use it for BigQuery (and it's still working with legacy API, but very few). You need to use OAuth2 mechanism to be authenticated.
When you use a Google Cloud API, you need to add an access token in the header of your API request. But in your case, you don't perform directly these API call, you use the client library.
You have a description of the authentication in the documentation. To go quicker, on Google Cloud environment, you can rely on the service service account. In some service you can customize them. You can alse use you own credential, on your computer, and a service account key file on external platform (on prem, on other cloud provider).
So, in your case, you initiate the bigquery library like this const bigqueryClient = new BigQuery();
. That means you rely on the environment credential to be connected to BigQuery.
On your environment, to use your own credential, you can use gcloud CLI and run this
gcloud auth application-default login
Login and enjoy!!
But the security is a long way on GCP, it's only the beginning, ask again later if you have difficulties in your choices
Upvotes: 3