13
13
14
14
'use strict' ;
15
15
16
- // [START all]
17
16
// [START setup]
18
17
// By default, the client will authenticate using the service account file
19
18
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
20
19
// the project specified by the GCLOUD_PROJECT environment variable. See
21
- // https://googlecloudplatform.github.io/gcloud -node/#/docs/google-cloud/latest/guides/authentication
20
+ // https://googlecloudplatform.github.io/google-cloud -node/#/docs/google-cloud/latest/guides/authentication
22
21
var BigQuery = require ( '@google-cloud/bigquery' ) ;
23
-
24
- // Instantiate the bigquery client
25
- var bigquery = BigQuery ( ) ;
26
22
// [END setup]
27
23
28
- // Control-flow helper library
29
- var async = require ( 'async' ) ;
24
+ function createDataset ( datasetId , callback ) {
25
+ var bigquery = BigQuery ( ) ;
26
+ var dataset = bigquery . dataset ( datasetId ) ;
30
27
31
- // [START create_dataset]
32
- /**
33
- * List datasets in the authenticated project.
34
- *
35
- * @param {string } name The name for the new dataset.
36
- * @param {function } callback The callback function.
37
- */
38
- function createDataset ( name , callback ) {
39
- var dataset = bigquery . dataset ( name ) ;
40
-
41
- // See https://googlecloudplatform.github.io/gcloud-node/#/docs/bigquery/latest/bigquery
42
- dataset . create ( function ( err , dataset ) {
28
+ // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/bigquery/latest/bigquery/dataset?method=create
29
+ dataset . create ( function ( err , dataset , apiResponse ) {
43
30
if ( err ) {
44
31
return callback ( err ) ;
45
32
}
46
33
47
- console . log ( 'Created dataset: %s' , name ) ;
48
- return callback ( null , dataset ) ;
34
+ console . log ( 'Created dataset: %s' , datasetId ) ;
35
+ return callback ( null , dataset , apiResponse ) ;
49
36
} ) ;
50
37
}
51
- // [END create_dataset]
52
-
53
- // [START delete_dataset]
54
- /**
55
- * List datasets in the authenticated project.
56
- *
57
- * @param {string } name The name for the new dataset.
58
- * @param {function } callback The callback function.
59
- */
60
- function deleteDataset ( name , callback ) {
61
- var dataset = bigquery . dataset ( name ) ;
62
-
63
- // See https://googlecloudplatform.github.io/gcloud-node/#/docs/bigquery/latest/bigquery
38
+
39
+ function deleteDataset ( datasetId , callback ) {
40
+ var bigquery = BigQuery ( ) ;
41
+ var dataset = bigquery . dataset ( datasetId ) ;
42
+
43
+ // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/bigquery/latest/bigquery/dataset?method=delete
64
44
dataset . delete ( function ( err ) {
65
45
if ( err ) {
66
46
return callback ( err ) ;
67
47
}
68
48
69
- console . log ( 'Deleted dataset: %s' , name ) ;
49
+ console . log ( 'Deleted dataset: %s' , datasetId ) ;
70
50
return callback ( null ) ;
71
51
} ) ;
72
52
}
73
- // [END delete_dataset]
74
-
75
- // [START list_datasets]
76
- /**
77
- * List datasets in the authenticated project.
78
- *
79
- * @param {string } projectId The project ID to use.
80
- * @param {function } callback The callback function.
81
- */
53
+
82
54
function listDatasets ( projectId , callback ) {
83
- // Instantiate a bigquery client
84
55
var bigquery = BigQuery ( {
85
56
projectId : projectId
86
57
} ) ;
87
58
88
- // See https://googlecloudplatform.github.io/gcloud- node/#/docs/bigquery/latest/bigquery
59
+ // See https://googlecloudplatform.github.io/google-cloud- node/#/docs/bigquery/latest/bigquery?method=getDatasets
89
60
bigquery . getDatasets ( function ( err , datasets ) {
90
61
if ( err ) {
91
62
return callback ( err ) ;
@@ -95,31 +66,27 @@ function listDatasets (projectId, callback) {
95
66
return callback ( null , datasets ) ;
96
67
} ) ;
97
68
}
98
- // [END list_datasets]
99
69
100
70
// [START get_dataset_size]
101
- /**
102
- * Calculate the size of the specified dataset.
103
- *
104
- * @param {string } datasetId The ID of the dataset.
105
- * @param {string } projectId The project ID.
106
- * @param {function } callback The callback function.
107
- */
71
+ // Control-flow helper library
72
+ var async = require ( 'async' ) ;
73
+
108
74
function getDatasetSize ( datasetId , projectId , callback ) {
109
75
// Instantiate a bigquery client
110
76
var bigquery = BigQuery ( {
111
77
projectId : projectId
112
78
} ) ;
113
79
var dataset = bigquery . dataset ( datasetId ) ;
114
80
115
- // See https://googlecloudplatform.github.io/gcloud- node/#/docs/bigquery/latest/bigquery/dataset
81
+ // See https://googlecloudplatform.github.io/google-cloud- node/#/docs/bigquery/latest/bigquery/dataset?method=getTables
116
82
dataset . getTables ( function ( err , tables ) {
117
83
if ( err ) {
118
84
return callback ( err ) ;
119
85
}
120
86
121
87
return async . map ( tables , function ( table , cb ) {
122
88
// Fetch more detailed info for each table
89
+ // See https://googlecloudplatform.github.io/google-cloud-node/#/docs/bigquery/latest/bigquery/table?method=get
123
90
table . get ( function ( err , tableInfo ) {
124
91
if ( err ) {
125
92
return cb ( err ) ;
@@ -142,7 +109,6 @@ function getDatasetSize (datasetId, projectId, callback) {
142
109
} ) ;
143
110
}
144
111
// [END get_dataset_size]
145
- // [END all]
146
112
147
113
// The command-line program
148
114
var cli = require ( 'yargs' ) ;
@@ -161,13 +127,13 @@ var program = module.exports = {
161
127
162
128
cli
163
129
. demand ( 1 )
164
- . command ( 'create <name >' , 'Create a new dataset.' , { } , function ( options ) {
165
- program . createDataset ( options . name , makeHandler ( ) ) ;
130
+ . command ( 'create <datasetId >' , 'Create a new dataset with the specified ID .' , { } , function ( options ) {
131
+ program . createDataset ( options . datasetId , makeHandler ( ) ) ;
166
132
} )
167
- . command ( 'delete <datasetId>' , 'Delete the specified dataset .' , { } , function ( options ) {
133
+ . command ( 'delete <datasetId>' , 'Delete the dataset with the specified ID .' , { } , function ( options ) {
168
134
program . deleteDataset ( options . datasetId , makeHandler ( ) ) ;
169
135
} )
170
- . command ( 'list' , 'List datasets in the authenticated project.' , { } , function ( options ) {
136
+ . command ( 'list' , 'List datasets in the specified project.' , { } , function ( options ) {
171
137
program . listDatasets ( options . projectId , makeHandler ( true , 'id' ) ) ;
172
138
} )
173
139
. command ( 'size <datasetId>' , 'Calculate the size of the specified dataset.' , { } , function ( options ) {
@@ -181,13 +147,13 @@ cli
181
147
description : 'Optionally specify the project ID to use.' ,
182
148
global : true
183
149
} )
184
- . example ( 'node $0 create my_dataset' , 'Create a new dataset named "my_dataset".' )
185
- . example ( 'node $0 delete my_dataset' , 'Delete "my_dataset".' )
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".' )
186
152
. example ( 'node $0 list' , 'List datasets.' )
187
- . example ( 'node $0 list -p bigquery-public-data' , 'List datasets in a project other than the authenticated project.' )
153
+ . example ( 'node $0 list -p bigquery-public-data' , 'List datasets in the "bigquery-public-data" project.' )
188
154
. example ( 'node $0 size my_dataset' , 'Calculate the size of "my_dataset".' )
189
155
. example ( 'node $0 size hacker_news -p bigquery-public-data' , 'Calculate the size of "bigquery-public-data:hacker_news".' )
190
- . wrap ( 100 )
156
+ . wrap ( 120 )
191
157
. recommendCommands ( )
192
158
. epilogue ( 'For more information, see https://cloud.google.com/bigquery/docs' ) ;
193
159
0 commit comments