This repository has been archived by the owner on Aug 10, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dataset size tutorial. Make modifications for testing directories. (
#34)
- Loading branch information
Showing
14 changed files
with
2,762 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Copyright 2017, Google, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const utils = require('../test/utils'); | ||
|
||
const { TUTORIALS_PATH, EMBEDMD_REGEXP } = utils; | ||
const files = fs.readdirSync(TUTORIALS_PATH); | ||
|
||
files.forEach((entry, i) => { | ||
let content, filename; | ||
const dir = path.join(TUTORIALS_PATH, '/', entry); | ||
fs.stat(dir, (err, stats) => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
if (stats.isDirectory()) { | ||
filename = path.join(dir, 'index.md'); | ||
fs.readFile(filename, { encoding: 'utf8' }, (err, content) => { | ||
if (err) { | ||
throw err; | ||
return; | ||
} | ||
|
||
if (EMBEDMD_REGEXP.test(content)) { | ||
const cmd = `embedmd -w ${filename}`; | ||
console.log(cmd); | ||
utils.runAsync(cmd, dir) | ||
.then(console.log).catch(console.error); | ||
} | ||
}); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tutorials/using-nodejs-to-calculate-the-size-of-a-bigquery-dataset/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
// Read and validate the input arguments | ||
const [projectId, datasetId] = process.argv.slice(2); | ||
|
||
if (!projectId || !datasetId) { | ||
console.log('Usage: node index.js PROJECT_ID DATASET_ID'); | ||
console.log('Example: node index.js bigquery-public-data hacker_news'); | ||
process.exit(); | ||
} | ||
|
||
// Instantiate a BigQuery client | ||
const bigquery = require('@google-cloud/bigquery')({ | ||
projectId: projectId | ||
}); | ||
|
||
// References an existing dataset, e.g. "my_dataset" | ||
const dataset = bigquery.dataset(datasetId); | ||
|
||
// Lists all tables in the dataset | ||
dataset.getTables() | ||
.then((results) => results[0]) | ||
// Retrieve the metadata for each table | ||
.then((tables) => Promise.all(tables.map((table) => table.get()))) | ||
.then((results) => results.map((result) => result[0])) | ||
// Select the size of each table | ||
.then((tables) => tables.map((table) => (parseInt(table.metadata.numBytes, 10) / 1000) / 1000)) | ||
// Sum up the sizes | ||
.then((sizes) => sizes.reduce((cur, prev) => cur + prev, 0)) | ||
// Print and return the size | ||
.then((sum) => { | ||
console.log(`Size of ${dataset.id}: ${sum} MB`); | ||
}); |
Oops, something went wrong.