Table - Documentation (original) (raw)
Table
Table objects are returned by methods such asDataset#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 | Table options. Properties Name Type Attributes Description location string 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
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 | See Dataset#createTable. | |
callback | CreateTableCallback | Properties Name Type Attributes Description err error 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}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
metadata | string | object | Metadata to set with the load operation. The metadata object should be in the format of theconfiguration.loadproperty of a Jobs resource. If a string is given, it will be used as the filetype. Properties Name Type Attributes Description jobId string Custom job id. jobPrefix string 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>}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback | DeleteTableCallback | Properties Name Type Attributes Description err error 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 | Properties Name Type Attributes Description err error 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 | Configuration object. Properties Name Type Attributes Default Description autoCreate boolean false Automatically create the object if it does not exist. | |
callback | function | Properties Name Type Attributes Description err error 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>}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback | GetTableMetadataCallback | The callback function. Properties Name Type Attributes Description err error 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.}
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 | The configuration object. Properties Name Type Attributes Default Description autoPaginate boolean true Have pagination handled automatically. maxApiCalls number Maximum number of API calls to make. maxResults number Maximum number of results to return. wrapIntegers boolean | IntegerTypeCastOptions 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 bywrapIntegers.integerTypeCastFunction. | |
callback | RowsCallback | The callback function. If autoPaginateis set to false a ManualQueryResultsCallback should be used. Properties Name Type Attributes Description err error An error returned while making this request rows array The table data from specified set of rows. |
Returns:
Type | Description |
---|---|
Promise. |
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]; });