Skip to content

Commit 51bfe8c

Browse files
committed
Update BigQuery samples
1 parent ba151d5 commit 51bfe8c

20 files changed

+940
-1893
lines changed

bigquery/README.md

+43-42
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,21 @@ __Usage:__ `node datasets --help`
3535

3636
```
3737
Commands:
38-
create <datasetId> Create a new dataset with the specified ID.
39-
delete <datasetId> Delete the dataset with the specified ID.
40-
list List datasets in the specified project.
41-
size <datasetId> Calculate the size of the specified dataset.
38+
create <datasetId> Creates a new dataset.
39+
delete <datasetId> Deletes a dataset.
40+
list [projectId] Lists all datasets in the specified project or the current project.
41+
size <datasetId> [projectId] Calculates the size of a dataset.
4242
4343
Options:
44-
--projectId, -p Optionally specify the project ID to use. [string] [default: "nodejs-docs-samples"]
45-
--help Show help [boolean]
44+
--help Show help [boolean]
4645
4746
Examples:
48-
node datasets create my_dataset Create a new dataset with the ID "my_dataset".
49-
node datasets delete my_dataset Delete a dataset identified as "my_dataset".
50-
node datasets list List datasets.
51-
node datasets list -p bigquery-public-data List datasets in the "bigquery-public-data" project.
52-
node datasets size my_dataset Calculate the size of "my_dataset".
53-
node datasets size hacker_news -p bigquery-public-data Calculate the size of "bigquery-public-data:hacker_news".
47+
node datasets create my_dataset Creates a new dataset named "my_dataset".
48+
node datasets delete my_dataset Deletes a dataset named "my_dataset".
49+
node datasets list Lists all datasets in the current project.
50+
node datasets list bigquery-public-data Lists all datasets in the "bigquery-public-data" project.
51+
node datasets size my_dataset Calculates the size of "my_dataset" in the current project.
52+
node datasets size hacker_news bigquery-public-data Calculates the size of "bigquery-public-data:hacker_news".
5453
5554
For more information, see https://cloud.google.com/bigquery/docs
5655
```
@@ -68,17 +67,17 @@ __Usage:__ `node queries --help`
6867
Commands:
6968
sync <sqlQuery> Run the specified synchronous query.
7069
async <sqlQuery> Start the specified asynchronous query.
71-
wait <jobId> Wait for the specified job to complete and retrieve its results.
70+
shakespeare Queries a public Shakespeare dataset.
7271
7372
Options:
7473
--help Show help [boolean]
7574
7675
Examples:
77-
node queries sync "SELECT * FROM
78-
`publicdata.samples.natality` LIMIT 5;"
79-
node queries async "SELECT * FROM
80-
`publicdata.samples.natality` LIMIT 5;"
81-
node queries wait job_VwckYXnR8yz54GBDMykIGnrc2
76+
node queries sync "SELECT * FROM publicdata.samples.natality Synchronously queries the natality dataset.
77+
LIMIT 5;"
78+
node queries async "SELECT * FROM Queries the natality dataset as a job.
79+
publicdata.samples.natality LIMIT 5;"
80+
node queries shakespeare Queries a public Shakespeare dataset.
8281
8382
For more information, see https://cloud.google.com/bigquery/docs
8483
```
@@ -94,39 +93,41 @@ __Usage:__ `node tables --help`
9493

9594
```
9695
Commands:
97-
create <datasetId> <tableId> Create a new table with the specified ID in the
98-
specified dataset.
99-
list <datasetId> List tables in the specified dataset.
100-
delete <datasetId> <tableId> Delete the specified table from the specified dataset.
101-
copy <srcDatasetId> <srcTableId> <destDatasetId> Make a copy of an existing table.
102-
<destTableId>
103-
browse <datasetId> <tableId> List the rows from the specified table.
104-
import <datasetId> <tableId> <fileName> Import data from a local file or a Google Cloud Storage
105-
file into the specified table.
96+
create <datasetId> <tableId> <schema> [projectId] Creates a new table.
97+
list <datasetId> [projectId] Lists all tables in a dataset.
98+
delete <datasetId> <tableId> [projectId] Deletes a table.
99+
copy <srcDatasetId> <srcTableId> <destDatasetId> Makes a copy of a table.
100+
<destTableId> [projectId]
101+
browse <datasetId> <tableId> [projectId] Lists rows in a table.
102+
import <datasetId> <tableId> <fileName> [projectId] Imports data from a local file into a table.
103+
import-gcs <datasetId> <tableId> <bucketName> <fileName> Imports data from a Google Cloud Storage file into a
104+
[projectId] table.
106105
export <datasetId> <tableId> <bucketName> <fileName> Export a table from BigQuery to Google Cloud Storage.
107-
insert <datasetId> <tableId> <json_or_file> Insert a JSON array (as a string or newline-delimited
106+
[projectId]
107+
insert <datasetId> <tableId> <json_or_file> [projectId] Insert a JSON array (as a string or newline-delimited
108108
file) into a BigQuery table.
109109
110110
Options:
111111
--help Show help [boolean]
112112
113113
Examples:
114-
node tables create my_dataset my_table Create table "my_table" in "my_dataset".
115-
node tables list my_dataset List tables in "my_dataset".
116-
node tables browse my_dataset my_table Display rows from "my_table" in "my_dataset".
117-
node tables delete my_dataset my_table Delete "my_table" from "my_dataset".
118-
node tables import my_dataset my_table ./data.csv Import a local file into a table.
119-
node tables import my_dataset my_table data.csv --bucket Import a GCS file into a table.
120-
my-bucket
121-
node tables export my_dataset my_table my-bucket my-file Export my_dataset:my_table to gcs://my-bucket/my-file as
122-
raw CSV.
123-
node tables export my_dataset my_table my-bucket my-file -f Export my_dataset:my_table to gcs://my-bucket/my-file as
124-
JSON --gzip gzipped JSON.
125-
node tables insert my_dataset my_table json_string Insert the JSON array represented by json_string into
114+
node tables create my_dataset my_table "Name:string, Createss a new table named "my_table" in "my_dataset".
115+
Age:integer, Weight:float, IsMagic:boolean"
116+
node tables list my_dataset Lists tables in "my_dataset".
117+
node tables browse my_dataset my_table Displays rows from "my_table" in "my_dataset".
118+
node tables delete my_dataset my_table Deletes "my_table" from "my_dataset".
119+
node tables import my_dataset my_table ./data.csv Imports a local file into a table.
120+
node tables import-gcs my_dataset my_table my-bucket Imports a GCS file into a table.
121+
data.csv
122+
node tables export my_dataset my_table my-bucket my-file Exports my_dataset:my_table to gcs://my-bucket/my-file
123+
as raw CSV.
124+
node tables export my_dataset my_table my-bucket my-file -f Exports my_dataset:my_table to gcs://my-bucket/my-file
125+
JSON --gzip as gzipped JSON.
126+
node tables insert my_dataset my_table json_string Inserts the JSON array represented by json_string into
126127
my_dataset:my_table.
127-
node tables insert my_dataset my_table json_file Insert the JSON objects contained in json_file (one per
128+
node tables insert my_dataset my_table json_file Inserts the JSON objects contained in json_file (one per
128129
line) into my_dataset:my_table.
129-
node tables copy src_dataset src_table dest_dataset Copy src_dataset:src_table to dest_dataset:dest_table.
130+
node tables copy src_dataset src_table dest_dataset Copies src_dataset:src_table to dest_dataset:dest_table.
130131
dest_table
131132
132133
For more information, see https://cloud.google.com/bigquery/docs

bigquery/datasets.js

+95-120
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,136 @@
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.
1+
/**
2+
* Copyright 2016, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
1315

1416
'use strict';
1517

16-
// [START setup]
17-
// By default, the client will authenticate using the service account file
18-
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
19-
// the project specified by the GCLOUD_PROJECT environment variable. See
20-
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/latest/guides/authentication
21-
var BigQuery = require('@google-cloud/bigquery');
22-
// [END setup]
23-
24-
function createDataset (datasetId, callback) {
25-
var bigquery = BigQuery();
26-
var dataset = bigquery.dataset(datasetId);
27-
28-
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/bigquery/latest/bigquery/dataset?method=create
29-
dataset.create(function (err, dataset, apiResponse) {
30-
if (err) {
31-
return callback(err);
32-
}
33-
34-
console.log('Created dataset: %s', datasetId);
35-
return callback(null, dataset, apiResponse);
36-
});
18+
const BigQuery = require('@google-cloud/bigquery');
19+
20+
// [START bigquery_create_dataset]
21+
function createDataset (datasetId) {
22+
// Instantiates a client
23+
const bigquery = BigQuery();
24+
25+
// Creates a new dataset, e.g. "my_new_dataset"
26+
return bigquery.createDataset(datasetId)
27+
.then((results) => {
28+
const dataset = results[0];
29+
console.log(`Dataset ${dataset.id} created.`);
30+
return dataset;
31+
});
3732
}
33+
// [END bigquery_create_dataset]
3834

39-
function deleteDataset (datasetId, callback) {
40-
var bigquery = BigQuery();
41-
var dataset = bigquery.dataset(datasetId);
35+
// [START bigquery_delete_dataset]
36+
function deleteDataset (datasetId) {
37+
// Instantiates a client
38+
const bigquery = BigQuery();
4239

43-
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/bigquery/latest/bigquery/dataset?method=delete
44-
dataset.delete(function (err) {
45-
if (err) {
46-
return callback(err);
47-
}
40+
// References an existing dataset, e.g. "my_dataset"
41+
const dataset = bigquery.dataset(datasetId);
4842

49-
console.log('Deleted dataset: %s', datasetId);
50-
return callback(null);
51-
});
43+
// Deletes the dataset
44+
return dataset.delete()
45+
.then(() => {
46+
console.log(`Dataset ${dataset.id} deleted.`);
47+
});
5248
}
49+
// [END bigquery_delete_dataset]
5350

54-
function listDatasets (projectId, callback) {
55-
var bigquery = BigQuery({
51+
// [START bigquery_list_datasets]
52+
function listDatasets (projectId) {
53+
// Instantiates a client
54+
const bigquery = BigQuery({
5655
projectId: projectId
5756
});
5857

59-
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/bigquery/latest/bigquery?method=getDatasets
60-
bigquery.getDatasets(function (err, datasets) {
61-
if (err) {
62-
return callback(err);
63-
}
64-
65-
console.log('Found %d dataset(s)!', datasets.length);
66-
return callback(null, datasets);
67-
});
58+
// Lists all datasets in the specified project
59+
return bigquery.getDatasets()
60+
.then((results) => {
61+
const datasets = results[0];
62+
console.log('Datasets:');
63+
datasets.forEach((dataset) => console.log(dataset.id));
64+
return datasets;
65+
});
6866
}
67+
// [END bigquery_list_datasets]
6968

70-
// [START get_dataset_size]
71-
// Control-flow helper library
72-
var async = require('async');
73-
74-
function getDatasetSize (datasetId, projectId, callback) {
75-
// Instantiate a bigquery client
76-
var bigquery = BigQuery({
69+
// [START bigquery_get_dataset_size]
70+
function getDatasetSize (datasetId, projectId) {
71+
// Instantiate a client
72+
const bigquery = BigQuery({
7773
projectId: projectId
7874
});
79-
var dataset = bigquery.dataset(datasetId);
80-
81-
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/bigquery/latest/bigquery/dataset?method=getTables
82-
dataset.getTables(function (err, tables) {
83-
if (err) {
84-
return callback(err);
85-
}
86-
87-
return async.map(tables, function (table, cb) {
88-
// Fetch more detailed info for each table
89-
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/bigquery/latest/bigquery/table?method=get
90-
table.get(function (err, tableInfo) {
91-
if (err) {
92-
return cb(err);
93-
}
94-
// Return numBytes converted to Megabytes
95-
var numBytes = tableInfo.metadata.numBytes;
96-
return cb(null, (parseInt(numBytes, 10) / 1000) / 1000);
97-
});
98-
}, function (err, sizes) {
99-
if (err) {
100-
return callback(err);
101-
}
102-
var sum = sizes.reduce(function (cur, prev) {
103-
return cur + prev;
104-
}, 0);
105-
106-
console.log('Size of %s: %d MB', datasetId, sum);
107-
return callback(null, sum);
75+
76+
// References an existing dataset, e.g. "my_dataset"
77+
const dataset = bigquery.dataset(datasetId);
78+
79+
// Lists all tables in the dataset
80+
return dataset.getTables()
81+
.then((results) => results[0])
82+
// Retrieve the metadata for each table
83+
.then((tables) => Promise.all(tables.map((table) => table.get())))
84+
.then((results) => results.map((result) => result[0]))
85+
// Select the size of each table
86+
.then((tables) => tables.map((table) => (parseInt(table.metadata.numBytes, 10) / 1000) / 1000))
87+
// Sum up the sizes
88+
.then((sizes) => sizes.reduce((cur, prev) => cur + prev, 0))
89+
// Print and return the size
90+
.then((sum) => {
91+
console.log(`Size of ${dataset.id}: ${sum} MB`);
92+
return sum;
10893
});
109-
});
11094
}
111-
// [END get_dataset_size]
95+
// [END bigquery_get_dataset_size]
11296

11397
// The command-line program
114-
var cli = require('yargs');
115-
var makeHandler = require('../utils').makeHandler;
98+
const cli = require(`yargs`);
11699

117-
var program = module.exports = {
100+
const program = module.exports = {
118101
createDataset: createDataset,
119102
deleteDataset: deleteDataset,
120103
listDatasets: listDatasets,
121104
getDatasetSize: getDatasetSize,
122-
main: function (args) {
105+
main: (args) => {
123106
// Run the command-line program
124107
cli.help().strict().parse(args).argv;
125108
}
126109
};
127110

128111
cli
129112
.demand(1)
130-
.command('create <datasetId>', 'Create a new dataset with the specified ID.', {}, function (options) {
131-
program.createDataset(options.datasetId, makeHandler());
132-
})
133-
.command('delete <datasetId>', 'Delete the dataset with the specified ID.', {}, function (options) {
134-
program.deleteDataset(options.datasetId, makeHandler());
113+
.command(`create <datasetId>`, `Creates a new dataset.`, {}, (opts) => {
114+
program.createDataset(opts.datasetId);
135115
})
136-
.command('list', 'List datasets in the specified project.', {}, function (options) {
137-
program.listDatasets(options.projectId, makeHandler(true, 'id'));
116+
.command(`delete <datasetId>`, `Deletes a dataset.`, {}, (opts) => {
117+
program.deleteDataset(opts.datasetId);
138118
})
139-
.command('size <datasetId>', 'Calculate the size of the specified dataset.', {}, function (options) {
140-
program.getDatasetSize(options.datasetId, options.projectId, makeHandler());
119+
.command(`list [projectId]`, `Lists all datasets in the specified project or the current project.`, {}, (opts) => {
120+
program.listDatasets(opts.projectId || process.env.GCLOUD_PROJECT);
141121
})
142-
.option('projectId', {
143-
alias: 'p',
144-
requiresArg: true,
145-
type: 'string',
146-
default: process.env.GCLOUD_PROJECT,
147-
description: 'Optionally specify the project ID to use.',
148-
global: true
122+
.command(`size <datasetId> [projectId]`, `Calculates the size of a dataset.`, {}, (opts) => {
123+
program.getDatasetSize(opts.datasetId, opts.projectId || process.env.GCLOUD_PROJECT);
149124
})
150-
.example('node $0 create my_dataset', 'Create a new dataset with the ID "my_dataset".')
151-
.example('node $0 delete my_dataset', 'Delete a dataset identified as "my_dataset".')
152-
.example('node $0 list', 'List datasets.')
153-
.example('node $0 list -p bigquery-public-data', 'List datasets in the "bigquery-public-data" project.')
154-
.example('node $0 size my_dataset', 'Calculate the size of "my_dataset".')
155-
.example('node $0 size hacker_news -p bigquery-public-data', 'Calculate the size of "bigquery-public-data:hacker_news".')
125+
.example(`node $0 create my_dataset`, `Creates a new dataset named "my_dataset".`)
126+
.example(`node $0 delete my_dataset`, `Deletes a dataset named "my_dataset".`)
127+
.example(`node $0 list`, `Lists all datasets in the current project.`)
128+
.example(`node $0 list bigquery-public-data`, `Lists all datasets in the "bigquery-public-data" project.`)
129+
.example(`node $0 size my_dataset`, `Calculates the size of "my_dataset" in the current project.`)
130+
.example(`node $0 size hacker_news bigquery-public-data`, `Calculates the size of "bigquery-public-data:hacker_news".`)
156131
.wrap(120)
157132
.recommendCommands()
158-
.epilogue('For more information, see https://cloud.google.com/bigquery/docs');
133+
.epilogue(`For more information, see https://cloud.google.com/bigquery/docs`);
159134

160135
if (module === require.main) {
161136
program.main(process.argv.slice(2));

0 commit comments

Comments
 (0)