如何使用googleapis bigquery api进行选择

时间:2016-08-07 10:57:42

标签: google-api google-bigquery

我正在使用const google = require('googleapis');流式传输到google bigquery,但现在当我想选择我的数据库时,我很困惑。 查看我需要使用的文档bigquery.jobs.query,但我不明白应该放置实际选择的位置。

var query = `select 1 `;
bqBooking.auth = jwtClient;
bigquery.jobs.query(bqBooking, function (err, rows) {
     if (err) {
         return callback(err);
     }

     printExample(rows);
     callback(null, rows);
 });

2 个答案:

答案 0 :(得分:0)

/**
 * Run an example query.
 *
 * @param {Function} callback Callback function.
 */
function queryExample (callback) {
  var query = 'SELECT TOP(corpus, 10) as title, COUNT(*) as unique_words\n' +
    'FROM [publicdata:samples.shakespeare];';

  bigquery.query(query, function (err, rows) {
    if (err) {
      return callback(err);
    }

    printExample(rows);
    callback(null, rows);
  });
}

https://cloud.google.com/bigquery/create-simple-app-api

答案 1 :(得分:0)

可以在' Request Body'中将查询作为参数传递bigQuery.jobs.query example中显示linkSplit请使用'试用此API'第二个链接中的选项。

var google = require('googleapis');
var bigquery = google.bigquery('v2');

authorize(function(authClient) {
var request = {
// Project ID of the project billed for the query
   projectId: '',  // TODO: Update placeholder value.

   resource: {
       // TODO: Add desired properties to the request body.
       "query": "Select  channel, sum(totalRequests) from 
       conversation_logs.RequestSummary WHERE timeStamp > 
       TIMESTAMP('2017-09-03 00:00:00 UTC') Group by channel;",  
       "maxResults": 1, 
       "useLegacySql": false 
   },

   auth: authClient
 };

 bigquery.jobs.query(request, function(err, response) {
     if (err) {
       console.log(err);
       return;
     }

     // TODO: Change code below to process the `response` object:
     console.log(JSON.stringify(response, null, 2));
 });
});

function authorize(callback) {
  google.auth.getApplicationDefault(function(err, authClient)) {
  if (err) {
      console.log('authentication failed: ', err);
      return;
  }
  if (authClient.createScopedRequired && 
      authClient.createScopedRequired()) {
        var scopes = ['https://www.googleapis.com/auth/cloud-
        platform'];
        authClient = authClient.createScoped(scopes);
      }
      callback(authClient);
  });
 }