Skip to content

Commit b8b6cc3

Browse files
committed
Refactor old BigQuery samples and add new ones.
1 parent 1cb30c5 commit b8b6cc3

26 files changed

+1397
-975
lines changed

bigquery/dataset_size.js

Lines changed: 0 additions & 147 deletions
This file was deleted.

bigquery/datasets.js

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START all]
17+
// [START setup]
18+
// By default, the client will authenticate using the service account file
19+
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
20+
// the project specified by the GCLOUD_PROJECT environment variable. See
21+
// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
22+
var BigQuery = require('@google-cloud/bigquery');
23+
// [END setup]
24+
25+
// Control-flow helper library
26+
var async = require('async');
27+
28+
// [START create_dataset]
29+
/**
30+
* List datasets in the authenticated project.
31+
*
32+
* @param {string} name The name for the new dataset.
33+
* @param {string} projectId The project ID to use.
34+
* @param {function} callback The callback function.
35+
*/
36+
function createDataset (name, projectId, callback) {
37+
// Instantiate a bigquery client
38+
var bigquery = BigQuery({
39+
projectId: projectId
40+
});
41+
var dataset = bigquery.dataset(name);
42+
43+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/bigquery/latest/bigquery
44+
dataset.create(function (err, dataset) {
45+
if (err) {
46+
return callback(err);
47+
}
48+
49+
console.log('Created dataset: %s', name);
50+
return callback(null, dataset);
51+
});
52+
}
53+
// [END create_dataset]
54+
55+
// [START delete_dataset]
56+
/**
57+
* List datasets in the authenticated project.
58+
*
59+
* @param {string} name The name for the new dataset.
60+
* @param {string} projectId The project ID to use.
61+
* @param {function} callback The callback function.
62+
*/
63+
function deleteDataset (name, projectId, callback) {
64+
// Instantiate a bigquery client
65+
var bigquery = BigQuery({
66+
projectId: projectId
67+
});
68+
var dataset = bigquery.dataset(name);
69+
70+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/bigquery/latest/bigquery
71+
dataset.delete(function (err) {
72+
if (err) {
73+
return callback(err);
74+
}
75+
76+
console.log('Deleted dataset: %s', name);
77+
return callback(null);
78+
});
79+
}
80+
// [END delete_dataset]
81+
82+
// [START list_datasets]
83+
/**
84+
* List datasets in the authenticated project.
85+
*
86+
* @param {string} projectId The project ID to use.
87+
* @param {function} callback The callback function.
88+
*/
89+
function listDatasets (projectId, callback) {
90+
// Instantiate a bigquery client
91+
var bigquery = BigQuery({
92+
projectId: projectId
93+
});
94+
95+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/bigquery/latest/bigquery
96+
bigquery.getDatasets(function (err, datasets) {
97+
if (err) {
98+
return callback(err);
99+
}
100+
101+
console.log('Found %d dataset(s)!', datasets.length);
102+
return callback(null, datasets);
103+
});
104+
}
105+
// [END list_datasets]
106+
107+
// [START get_dataset_size]
108+
/**
109+
* Calculate the size of the specified dataset.
110+
*
111+
* @param {string} datasetId The ID of the dataset.
112+
* @param {string} projectId The project ID of the dataset.
113+
* @param {function} callback The callback function.
114+
*/
115+
function getDatasetSize (datasetId, projectId, callback) {
116+
// Instantiate a bigquery client
117+
var bigquery = BigQuery({
118+
projectId: projectId
119+
});
120+
var dataset = bigquery.dataset(datasetId);
121+
122+
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/bigquery/latest/bigquery/dataset
123+
dataset.getTables(function (err, tables) {
124+
if (err) {
125+
return callback(err);
126+
}
127+
128+
return async.map(tables, function (table, cb) {
129+
// Fetch more detailed info for each table
130+
table.get(function (err, tableInfo) {
131+
if (err) {
132+
return cb(err);
133+
}
134+
// Return numBytes converted to Megabytes
135+
var numBytes = tableInfo.metadata.numBytes;
136+
return cb(null, (parseInt(numBytes, 10) / 1000) / 1000);
137+
});
138+
}, function (err, sizes) {
139+
if (err) {
140+
return callback(err);
141+
}
142+
var sum = sizes.reduce(function (cur, prev) {
143+
return cur + prev;
144+
}, 0);
145+
146+
console.log('Size of %s: %d MB', datasetId, sum);
147+
return callback(null, sum);
148+
});
149+
});
150+
}
151+
// [END get_dataset_size]
152+
// [END all]
153+
154+
// The command-line program
155+
var cli = require('yargs');
156+
var makeHandler = require('../util').makeHandler;
157+
158+
var program = module.exports = {
159+
createDataset: createDataset,
160+
deleteDataset: deleteDataset,
161+
listDatasets: listDatasets,
162+
getDatasetSize: getDatasetSize,
163+
main: function (args) {
164+
// Run the command-line program
165+
cli.help().strict().parse(args).argv;
166+
}
167+
};
168+
169+
cli
170+
.demand(1)
171+
.command('create <name>', 'Create a new dataset.', {}, function (options) {
172+
program.createDataset(options.name, options.projectId, makeHandler());
173+
})
174+
.command('delete <datasetId>', 'Delete the specified dataset.', {}, function (options) {
175+
program.deleteDataset(options.datasetId, options.projectId, makeHandler());
176+
})
177+
.command('list', 'List datasets in the authenticated project.', {}, function (options) {
178+
program.listDatasets(options.projectId, makeHandler(true, 'id'));
179+
})
180+
.command('size <datasetId>', 'Calculate the size of the specified dataset.', {}, function (options) {
181+
program.getDatasetSize(options.datasetId, options.projectId, makeHandler());
182+
})
183+
.option('projectId', {
184+
alias: 'p',
185+
requiresArg: true,
186+
type: 'string',
187+
default: process.env.GCLOUD_PROJECT,
188+
description: 'Optionally specify the project ID to use.',
189+
global: true
190+
})
191+
.example('node $0 create my-dataset', 'Create a new dataset named "my-dataset".')
192+
.example('node $0 delete my-dataset', 'Delete "my-dataset".')
193+
.example('node $0 list', 'List datasets.')
194+
.example('node $0 size my-dataset', 'Calculate the size of "my-dataset".')
195+
.wrap(80)
196+
.recommendCommands()
197+
.epilogue('For more information, see https://cloud.google.com/bigquery/docs');
198+
199+
if (module === require.main) {
200+
program.main(process.argv.slice(2));
201+
}

0 commit comments

Comments
 (0)