Dataset

Dataset

Interact with your BigQuery dataset. Create a Dataset instance with BigQuery#createDataset or BigQuery#dataset.

Constructor

new Dataset(bigQuery, id, optionsopt)

Parameters:
Name Type Attributes Description
bigQuery BigQuery

BigQuery instance.

id string

The ID of the Dataset.

options object <optional>

Dataset options.

Properties
Name Type Attributes Description
location string <optional>

The geographic location of the dataset. Defaults to US.

Source:
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

Members

getModelsStream

List all or some of the {module:bigquery/model} objects in your project as a readable object stream.

Source:
Examples
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

dataset.getModelsStream()
  .on('error', console.error)
  .on('data', (model) => {})
  .on('end', () => {
    // All models have been retrieved
  });

If you anticipate many results, you can end a stream early to prevent unnecessary processing and API requests.

dataset.getModelsStream()
  .on('data', function(model) {
    this.end();
  });

getTablesStream

List all or some of the {module:bigquery/table} objects in your project as a readable object stream.

Source:
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

dataset.getTablesStream()
  .on('error', console.error)
  .on('data', (table) => {})
  .on('end', () => {
    // All tables have been retrieved
  });

//-
// If you anticipate many results, you can end a stream early to prevent
// unnecessary processing and API requests.
//-
dataset.getTablesStream()
  .on('data', function(table) {
    this.end();
  });

Methods

create(callbackopt) → {Promise}

Create a dataset.

Parameters:
Name Type Attributes Description
callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

dataset Dataset

The created dataset.

apiResponse object

The full API response.

Source:
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');
dataset.create((err, dataset, apiResponse) => {
  if (!err) {
    // The dataset was created successfully.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.create().then((data) => {
  const dataset = data[0];
  const apiResponse = data[1];
});

createQueryJob(options, callbackopt) → {Promise}

Run a query as a job. No results are immediately returned. Instead, your callback will be executed with a Job object that you must ping for the results. See the Job documentation for explanations of how to check on the status of the job.

See BigQuery#createQueryJob for full documentation of this method.

Parameters:
Name Type Attributes Description
options object

See BigQuery#createQueryJob for full documentation of this method.

callback function <optional>

See BigQuery#createQueryJob for full documentation of this method.

Source:

createQueryStream(options) → {stream}

Run a query scoped to your dataset as a readable object stream.

See BigQuery#createQueryStream for full documentation of this method.

Parameters:
Name Type Description
options object

See BigQuery#createQueryStream for full documentation of this method.

Source:

createTable(id, optionsopt, callbackopt) → {Promise}

Create a table given a tableId or configuration object.

Parameters:
Name Type Attributes Description
id string

Table id.

options object <optional>

See a Table resource.

Properties
Name Type Attributes Description
schema string | object <optional>

A comma-separated list of name:type pairs. Valid types are "string", "integer", "float", "boolean", and "timestamp". If the type is omitted, it is assumed to be "string". Example: "name:string, age:integer". Schemas can also be specified as a JSON array of fields, which allows for nested and repeated fields. See a Table resource for more detailed information.

callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request

table Table

The newly created table.

apiResponse object

The full API response.

Source:
See:
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

const tableId = 'institution_data';

const options = {
  // From the data.gov CSV dataset (http://goo.gl/kSE7z6):
  schema: 'UNITID,INSTNM,ADDR,CITY,STABBR,ZIP,FIPS,OBEREG,CHFNM,...'
};

dataset.createTable(tableId, options, (err, table, apiResponse) => {});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.createTable(tableId, options).then((data) => {
  const table = data[0];
  const apiResponse = data[1];
});

delete(optionsopt, callbackopt) → {Promise}

Delete the dataset.

Parameters:
Name Type Attributes Description
options object <optional>

The configuration object.

Properties
Name Type Attributes Default Description
force boolean <optional>
false

Force delete dataset and all tables.

callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request

apiResponse object

The full API response.

Source:
See:
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

//-
// Delete the dataset, only if it does not have any tables.
//-
dataset.delete((err, apiResponse) => {});

//-
// Delete the dataset and any tables it contains.
//-
dataset.delete({ force: true }, (err, apiResponse) => {});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.delete().then((data) => {
  const apiResponse = data[0];
});

exists(callbackopt) → {Promise}

Check if the dataset exists.

Parameters:
Name Type Attributes Description
callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

exists boolean

Whether the dataset exists or not.

Source:
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');
dataset.exists((err, exists) => {});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.exists().then((data) => {
  const exists = data[0];
});

get(optionsopt, callbackopt) → {Promise}

Get a dataset if it exists.

You may optionally use this to "get or create" an object by providing an object with autoCreate set to true. Any extra configuration that is normally required for the create method must be contained within this object as well.

Parameters:
Name Type Attributes Description
options options <optional>

Configuration object.

Properties
Name Type Attributes Default Description
autoCreate boolean <optional>
false

Automatically create the object if it does not exist.

callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

dataset Dataset

The dataset.

Source:
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');
dataset.get((err, dataset, apiResponse) => {
  if (!err) {
    // `dataset.metadata` has been populated.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.get().then((data) => {
  const dataset = data[0];
  const apiResponse = data[1];
});

getMetadata(callbackopt) → {Promise}

Get the metadata for the Dataset.

Parameters:
Name Type Attributes Description
callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

metadata object

The dataset's metadata.

apiResponse object

The full API response.

Source:
See:
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');
dataset.getMetadata((err, metadata, apiResponse) => {});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.getMetadata().then((data) => {
  const metadata = data[0];
  const apiResponse = data[1];
});

getModels(optionsopt, callbackopt) → {Promise}

Get a list of models.

Parameters:
Name Type Attributes Description
options object <optional>

Configuration object.

Properties
Name Type Attributes Default Description
autoPaginate boolean <optional>
true

Have pagination handled automatically.

maxApiCalls number <optional>

Maximum number of API calls to make.

maxResults number <optional>

Maximum number of results to return.

pageToken string <optional>

Token returned from a previous call, to request the next page of results.

callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request

models Array.<Model>

The list of models from your Dataset.

Source:
See:
Examples
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

dataset.getModels((err, models) => {
  // models is an array of `Model` objects.
});

To control how many API requests are made and page through the results manually, set `autoPaginate` to `false`.

function manualPaginationCallback(err, models, nextQuery, apiResponse) {
  if (nextQuery) {
    // More results exist.
    dataset.getModels(nextQuery, manualPaginationCallback);
  }
}

dataset.getModels({
  autoPaginate: false
}, manualPaginationCallback);

If the callback is omitted, we'll return a Promise.

dataset.getModels().then((data) => {
  const models = data[0];
});

getTables(optionsopt, callbackopt) → {Promise}

Get a list of tables.

Parameters:
Name Type Attributes Description
options object <optional>

Configuration object.

Properties
Name Type Attributes Default Description
autoPaginate boolean <optional>
true

Have pagination handled automatically.

maxApiCalls number <optional>

Maximum number of API calls to make.

maxResults number <optional>

Maximum number of results to return.

pageToken string <optional>

Token returned from a previous call, to request the next page of results.

callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request

tables Array.<Table>

The list of tables from your Dataset.

Source:
See:
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

dataset.getTables((err, tables) => {
  // tables is an array of `Table` objects.
});

//-
// To control how many API requests are made and page through the results
// manually, set `autoPaginate` to `false`.
//-
function manualPaginationCallback(err, tables, nextQuery, apiResponse) {
  if (nextQuery) {
    // More results exist.
    dataset.getTables(nextQuery, manualPaginationCallback);
  }
}

dataset.getTables({
  autoPaginate: false
}, manualPaginationCallback);

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.getTables().then((data) => {
  const tables = data[0];
});

model(id) → {Model}

Create a Model object.

Parameters:
Name Type Description
id string

The ID of the model.

Source:
Throws:

if model ID is missing.

Type
TypeError
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

const model = dataset.model('my-model');

setMetadata(metadata, callbackopt) → {Promise}

Sets the metadata of the Dataset object.

Parameters:
Name Type Attributes Description
metadata object

Metadata to save on the Dataset.

callback function <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

apiResponse object

The full API response.

Source:
See:
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

const metadata = {
  description: 'Info for every institution in the 2013 IPEDS universe'
};

dataset.setMetadata(metadata, (err, apiResponse) => {});

//-
// If the callback is omitted, we'll return a Promise.
//-
dataset.setMetadata(metadata).then((data) => {
  const apiResponse = data[0];
});

table(id, optionsopt) → {Table}

Create a Table object.

Parameters:
Name Type Attributes Description
id string

The ID of the table.

options object <optional>

Table options.

Properties
Name Type Attributes Description
location string <optional>

The geographic location of the table, by default this value is inherited from the dataset. This can be used to configure the location of all jobs created through a table instance. It cannot be used to set the actual location of the table. This value will be superseded by any API responses containing location data for the table.

Source:
Throws:

if table ID is missing.

Type
TypeError
Example
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('institutions');

const institutions = dataset.table('institution_data');