Ali Raza
Ali Raza

Reputation: 146

Add Data from node.js to Google BigQuery Table

I am working on Google BigQuery . i want to upload data to BigQuery Table from node.js . but i am facing a trouble here . when i send my json data form node.js app to insert that data in bigQuery table by using this code

line = {"id":"123", "dttme":"201807012130", "brwsr":"Chrome", "pg_id":"hpv1"};
const datajson=line;

const {BigQuery} = require('@google-cloud/bigquery');
const TableObjectHeader = {
      "tableReference": {
        "projectId": "local-bebop-277417",
        "datasetId": "local-bebop-277417:123",
        "tableId": "local-bebop-277417:123.first",
      }
    }
    const bigqueryClient = new BigQuery();
const dataset = bigqueryClient.dataset(TableObjectHeader['tableReference']['datasetId']);
const table = dataset.table(TableObjectHeader['tableReference']['tableId']);
table.insert(datajson, function(err, response) {
      console.log("error:"+JSON.stringify(err));
      console.log("response:"+JSON.stringify(response));
});

then in console i am getting

error:{}
response:undefined

and i am not getting my datajson in BigQuery Tble ? Can any one help me how to fix this problem ?

Upvotes: 2

Views: 6336

Answers (1)

Nick_Kh
Nick_Kh

Reputation: 5243

I've noticed the same error, when my GCP projectID was not defined during Bigquery Node.js Client initialization.

According to the documentation, NodeJS developing environment has to be properly prepared, once you've decided to use @google-cloud/bigquery library, by setting up:

  • Authentication parameters;
  • Google Cloud BigQuery API to be enabled;
  • Target Bigquery GCP ProjectID explicitly via gcloud config set for user shell session on the relevant NodeJS executor's machine or individually initializing for each client const bigquery = new BigQuery({projectId: 'my-project'}); connection attempt to Bigquery REST API.

I've made some correctives in you initial code snippet to fix this issue:

line = {"id":"123", "dttme":"201807012130", "brwsr":"Chrome", "pg_id":"hpv1"};
const datajson=line;

const {BigQuery} = require('@google-cloud/bigquery');
const TableObjectHeader = {
      "tableReference": {
        "datasetId": "datasetId",
        "tableId": "tableId",
      }
    }
    const bigqueryClient = new BigQuery({projectId: 'my-project'});
const dataset = bigqueryClient.dataset(TableObjectHeader['tableReference']['datasetId']);
const table = dataset.table(TableObjectHeader['tableReference']['tableId']);
table.insert(datajson, function(err, response) {
      console.log("error:"+JSON.stringify(err));
      console.log("response:"+JSON.stringify(response));
});

Be also aware and don't share any user sensitive and confidential data across your code examples or/and explanation details to keep them only for private usage.

Upvotes: 3

Related Questions