Table

Table

Table objects are returned by methods such as Dataset#table, Dataset#createTable, and Dataset#getTables.

Constructor

new Table(dataset, id, optionsopt)

Parameters:
Name Type Attributes Description
dataset Dataset

Dataset instance.

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.

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

const table = dataset.table('my-table');
```

Members

createReadStream

Create a readable stream of the rows of data in your table. This method is simply a wrapper around Table#getRows.

See Tabledata: list API Documentation

Example
```
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');
const table = dataset.table('my-table');

table.createReadStream(options)
  .on('error', console.error)
  .on('data', row => {})
  .on('end', function() {
    // All rows have been retrieved.
  });

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

Methods

create(optionsopt, callbackopt) → {Promise.<CreateTableResponse>}

Create a table.

Parameters:
Name Type Attributes Description
options object <optional>

See Dataset#createTable.

callback CreateTableCallback <optional>
Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

table Table

The new Table.

apiResponse object

The full API response.

Returns:
Type Description
Promise.<CreateTableResponse>
Example
```
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');

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

table.create((err, table, apiResponse) => {
  if (!err) {
    // The table was created successfully.
  }
});

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

createQueryStream(query) → {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
query object

See BigQuery#createQueryStream for full documentation of this method.

Returns:
Type Description
stream

See BigQuery#createQueryStream for full documentation of this method.

createWriteStream(metadataopt) → {WritableStream}

Load data into your table from a readable stream of AVRO, CSV, JSON, ORC, or PARQUET data.

See Jobs: insert API Documentation

Parameters:
Name Type Attributes Description
metadata string | object <optional>

Metadata to set with the load operation. The metadata object should be in the format of the configuration.load property of a Jobs resource. If a string is given, it will be used as the filetype.

Properties
Name Type Attributes Description
jobId string <optional>

Custom job id.

jobPrefix string <optional>

Prefix to apply to the job id.

Returns:
Type Description
WritableStream
Throws:

If source format isn't recognized.

Type
Error
Example
```
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');
const table = dataset.table('my-table');

//-
// Load data from a CSV file.
//-
const request = require('request');

const csvUrl = 'http://goo.gl/kSE7z6';

const metadata = {
  allowJaggedRows: true,
  skipLeadingRows: 1
};

request.get(csvUrl)
  .pipe(table.createWriteStream(metadata))
  .on('job', (job) => {
    // `job` is a Job object that can be used to check the status of the
    // request.
  })
  .on('complete', (job) => {
    // The job has completed successfully.
  });

//-
// Load data from a JSON file.
//-
const fs = require('fs');

fs.createReadStream('./test/testdata/testfile.json')
  .pipe(table.createWriteStream('json'))
  .on('job', (job) => {
    // `job` is a Job object that can be used to check the status of the
    // request.
  })
  .on('complete', (job) => {
    // The job has completed successfully.
  });
```

delete(callbackopt) → {Promise.<DeleteTableResponse>}

Delete a table and all its data.

See Tables: delete API Documentation

Parameters:
Name Type Attributes Description
callback DeleteTableCallback <optional>
Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

apiResponse object

The full API response.

Returns:
Type Description
Promise.<DeleteTableResponse>
Example
```
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');

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

table.delete((err, apiResponse) => {});

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

exists(callbackopt) → {Promise.<TableExistsCallback>}

Check if the table exists.

Parameters:
Name Type Attributes Description
callback TableExistsCallback <optional>
Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

exists boolean

Whether the table exists or not.

Returns:
Type Description
Promise.<TableExistsCallback>
Example
```
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');

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

table.exists((err, exists) => {});

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

get(optionsopt, callbackopt) → {Promise.<GetTableResponse>}

Get a table 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.

If you wish to get a selection of metadata instead of the full table metadata (retrieved by both Table#get by default and by Table#getMetadata), use the options parameter to set the view and/or selectedFields query parameters.

See Tables.get and TableMetadataView

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>
Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

table Table

The Table.

apiResponse object

The full API response.

Returns:
Type Description
Promise.<GetTableResponse>
Example
```
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');

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

const options = {
  view: "BASIC"
}

table.get((err, table, apiResponse) => {
  // `table.metadata` has been populated.
});

table.get(options, (err, table, apiResponse) => {
  // A selection of `table.metadata` has been populated
})

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

getMetadata(callbackopt) → {Promise.<GetTableMetadataResponse>}

Return the metadata associated with the Table.

See Tables: get API Documentation

Parameters:
Name Type Attributes Description
callback GetTableMetadataCallback <optional>

The callback function.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request.

metadata object

The metadata of the Table.

apiResponse object

The full API response.

Returns:
Type Description
Promise.<GetTableMetadataResponse>
Example
```
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');

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

table.getMetadata((err, metadata, apiResponse) => {});

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

getRows(optionsopt, callbackopt) → {Promise.<RowsResponse>}

Retrieves table data from a specified set of rows. The rows are returned to your callback as an array of objects matching your table's schema.

See Tabledata: list API Documentation

Parameters:
Name Type Attributes Description
options object <optional>

The 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.

wrapIntegers boolean | IntegerTypeCastOptions <optional>
false

Wrap values of 'INT64' type in BigQueryInt objects. If a boolean, this will wrap values in BigQueryInt objects. If an object, this will return a value returned by wrapIntegers.integerTypeCastFunction.

callback RowsCallback <optional>

The callback function. If autoPaginate is set to false a ManualQueryResultsCallback should be used.

Properties
Name Type Attributes Description
err error <nullable>

An error returned while making this request

rows array

The table data from specified set of rows.

Returns:
Type Description
Promise.<RowsResponse>
Example
```
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const dataset = bigquery.dataset('my-dataset');
const table = dataset.table('my-table');

table.getRows((err, rows) => {
  if (!err) {
    // rows is an array of results.
  }
});

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

table.getRows({
  autoPaginate: false
}, manualPaginationCallback);

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