Class BigQuery (8.0.0)

In the following examples from this page and the other modules (Dataset,Table, etc.), we are going to be using a dataset fromdata.gov of higher education institutions.

We will create a table with the correct schema, import the public CSV file into that table, and query it for data.

This client supports enabling query-related preview features via environmental variables. By setting the environment variable QUERY_PREVIEW_ENABLED to the string "TRUE", the client will enable preview features, though behavior may still be controlled via the bigquery service as well. Currently, the feature(s) in scope include: stateless queries (query execution without corresponding job metadata).

SeeWhat is BigQuery?

Inheritance

Service >BigQuery

Package

@google-cloud/bigquery

Examples

Install the client library withnpm:

npminstall@google-cloud/bigquery

Import the client library

const{BigQuery}=require('@google-cloud/bigquery');

Create a client that usesApplication Default Credentials (ADC):

constbigquery=newBigQuery();

Create a client withexplicit credentials:

constbigquery=newBigQuery({projectId:'your-project-id',keyFilename:'/path/to/keyfile.json'});

Full quickstart example:

// Imports the Google Cloud client libraryconst{BigQuery}=require('@google-cloud/bigquery');asyncfunctioncreateDataset(){// Creates a clientconstbigqueryClient=newBigQuery();// Create the datasetconst[dataset]=awaitbigqueryClient.createDataset(datasetName);console.log(`Dataset${dataset.id} created.`);}createDataset();

Constructors

(constructor)(options)

constructor(options?:BigQueryOptions);

Constructs a new instance of theBigQuery class

Parameter
NameDescription
optionsBigQueryOptions

Properties

location

location?:string;

setLogFunction

staticsetLogFunction:typeofsetLogFunction;

universeDomain

getuniverseDomain():string;

Methods

createDataset(id, options)

createDataset(id:string,options?:DatasetResource):Promise<DatasetResponse>;
Parameters
NameDescription
idstring

ID of the dataset to create.

optionsDatasetResource

See aDataset resource.

Returns
TypeDescription
Promise<DatasetResponse>
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();bigquery.createDataset('my-dataset',function(err,dataset,apiResponse){});//-// If the callback is omitted, we'll return a Promise.//-bigquery.createDataset('my-dataset').then(function(data){constdataset=data[0];constapiResponse=data[1];});

createDataset(id, options, callback)

createDataset(id:string,options:DatasetResource,callback:DatasetCallback):void;
Parameters
NameDescription
idstring
optionsDatasetResource
callbackDatasetCallback
Returns
TypeDescription
void

createDataset(id, callback)

createDataset(id:string,callback:DatasetCallback):void;
Parameters
NameDescription
idstring
callbackDatasetCallback
Returns
TypeDescription
void

createJob(options)

createJob(options:JobOptions):Promise<JobResponse>;

Creates a job. Typically when creating a job you'll have a very specific task in mind. For this we recommend one of the following methods:


However in the event you need a finer level of control over the job creation, you can use this method to pass in a rawJob resource object.

SeeJobs Overview SeeJobs: insert API Documentation

Parameter
NameDescription
optionsJobOptions

Object in the form of aJob resource;

Returns
TypeDescription
Promise<JobResponse_2>
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();constoptions={configuration:{query:{query:'SELECT url FROM `publicdata.samples.github_nested` LIMIT 100'}}};bigquery.createJob(options,function(err,job){if(err){// Error handling omitted.}job.getQueryResults(function(err,rows){});});//-// If the callback is omitted, we'll return a Promise.//-bigquery.createJob(options).then(function(data){constjob=data[0];returnjob.getQueryResults();});

createJob(options, callback)

createJob(options:JobOptions,callback:JobCallback):void;
Parameters
NameDescription
optionsJobOptions
callbackJobCallback
Returns
TypeDescription
void

createQueryJob(options)

createQueryJob(options:Query|string):Promise<JobResponse>;

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

SeeJobs: insert API Documentation

Parameter
NameDescription
optionsQuery | string

The configuration object. This must be in the format of the`configuration.query` property of a Jobs resource. If a string is provided, this is used as the query string, and all other options are defaulted.

Returns
TypeDescription
Promise<JobResponse_2>
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();constquery='SELECT url FROM `publicdata.samples.github_nested` LIMIT100';//-// You may pass only a query string, having a new table created to storethe// results of the query.//-bigquery.createQueryJob(query,function(err,job){});//-// You can also control the destination table by providing a// {@link Table} object.//-bigquery.createQueryJob({destination:bigquery.dataset('higher_education').table('institutions'),query:query},function(err,job){});//-// After you have run `createQueryJob`, your query will execute in a job.Your// callback is executed with a {@link Job} object so that you may// check for the results.//-bigquery.createQueryJob(query,function(err,job){if(!err){job.getQueryResults(function(err,rows,apiResponse){});}});//-// If the callback is omitted, we'll return a Promise.//-bigquery.createQueryJob(query).then(function(data){constjob=data[0];constapiResponse=data[1];returnjob.getQueryResults();});

createQueryJob(options, callback)

createQueryJob(options:Query|string,callback:JobCallback):void;
Parameters
NameDescription
optionsQuery | string
callbackJobCallback
Returns
TypeDescription
void

createQueryStream(options)

createQueryStream(options?:Query|string):ResourceStream<RowMetadata>;
Parameter
NameDescription
optionsQuery | string
Returns
TypeDescription
ResourceStream<RowMetadata>

dataset(id, options)

dataset(id:string,options?:DatasetOptions):Dataset;

Create a reference to a dataset.

Parameters
NameDescription
idstring

ID of the dataset.

optionsDatasetOptions

Dataset options.

Returns
TypeDescription
Dataset
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();constdataset=bigquery.dataset('higher_education');

date(value)

date(value:BigQueryDateOptions|string):BigQueryDate;
Parameter
NameDescription
valueBigQueryDateOptions | string

The date. If a string, this should be in the format the API describes:YYYY-[M]M-[D]D. Otherwise, provide an object.

Returns
TypeDescription
BigQueryDate
Example
const{BigQuery}=require('@google-cloud/bigquery');constdate=BigQuery.date('2017-01-01');//-// Alternatively, provide an object.//-constdate2=BigQuery.date({year:2017,month:1,day:1});

date(value)

staticdate(value:BigQueryDateOptions|string):BigQueryDate;

TheDATE type represents a logical calendar date, independent of time zone. It does not represent a specific 24-hour time period. Rather, a given DATE value represents a different 24-hour period when interpreted in different time zones, and may represent a shorter or longer day during Daylight Savings Time transitions.

Parameter
NameDescription
valueBigQueryDateOptions | string

The date. If a string, this should be in the format the API describes:YYYY-[M]M-[D]D. Otherwise, provide an object.

Returns
TypeDescription
BigQueryDate
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();constdate=bigquery.date('2017-01-01');//-// Alternatively, provide an object.//-constdate2=bigquery.date({year:2017,month:1,day:1});

datetime(value)

datetime(value:BigQueryDatetimeOptions|string):BigQueryDatetime;
Parameter
NameDescription
valueBigQueryDatetimeOptions | string
Returns
TypeDescription
BigQueryDatetime

datetime(value)

staticdatetime(value:BigQueryDatetimeOptions|string):BigQueryDatetime;

ADATETIME data type represents a point in time. Unlike aTIMESTAMP, this does not refer to an absolute instance in time. Instead, it is the civil time, or the time that a user would see on a watch or calendar.

Parameter
NameDescription
valueBigQueryDatetimeOptions | string

The time. If a string, this should be in the format the API describes:YYYY-[M]M-[D]D[ [H]H:[M]M:[S]S[.DDDDDD]]. Otherwise, provide an object.

Returns
TypeDescription
BigQueryDatetime
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();constdatetime=bigquery.datetime('2017-01-01 13:00:00');//-// Alternatively, provide an object.//-constdatetime=bigquery.datetime({year:2017,month:1,day:1,hours:14,minutes:0,seconds:0});

decodeIntegerValue_(value)

staticdecodeIntegerValue_(value:IntegerTypeCastValue):number;

Convert an INT64 value to Number.

Parameter
NameDescription
valueIntegerTypeCastValue

The INT64 value to convert.

Returns
TypeDescription
number

geography(value)

geography(value:string):Geography;
Parameter
NameDescription
valuestring
Returns
TypeDescription
Geography

geography(value)

staticgeography(value:string):Geography;

A geography value represents a surface area on the Earth in Well-known Text (WKT) format.

Parameter
NameDescription
valuestring

The geospatial data.

Returns
TypeDescription
Geography
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();constgeography=bigquery.geography('POINT(1, 2)');

getDatasets(options)

getDatasets(options?:GetDatasetsOptions):Promise<DatasetsResponse>;

List all or some of the datasets in a project.

SeeDatasets: list API Documentation

Parameter
NameDescription
optionsGetDatasetsOptions

Configuration object.

Returns
TypeDescription
Promise<DatasetsResponse>
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();bigquery.getDatasets(function(err,datasets){if(!err){// datasets is an array of Dataset objects.}});//-// To control how many API requests are made and page through the results// manually, set `autoPaginate` to `false`.//-functionmanualPaginationCallback(err,datasets,nextQuery,apiResponse){if(nextQuery){// More results exist.bigquery.getDatasets(nextQuery,manualPaginationCallback);}}bigquery.getDatasets({autoPaginate:false},manualPaginationCallback);//-// If the callback is omitted, we'll return a Promise.//-bigquery.getDatasets().then(function(datasets){});

getDatasets(options, callback)

getDatasets(options:GetDatasetsOptions,callback:DatasetsCallback):void;
Parameters
NameDescription
optionsGetDatasetsOptions
callbackDatasetsCallback
Returns
TypeDescription
void

getDatasets(callback)

getDatasets(callback:DatasetsCallback):void;
Parameter
NameDescription
callbackDatasetsCallback
Returns
TypeDescription
void

getDatasetsStream(options)

getDatasetsStream(options?:GetDatasetsOptions):ResourceStream<Dataset>;
Parameter
NameDescription
optionsGetDatasetsOptions
Returns
TypeDescription
ResourceStream<Dataset>

getJobs(options)

getJobs(options?:GetJobsOptions):Promise<GetJobsResponse>;

Get all of the jobs from your project.

SeeJobs: list API Documentation

Parameter
NameDescription
optionsGetJobsOptions

Configuration object.

Returns
TypeDescription
Promise<GetJobsResponse>
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();bigquery.getJobs(function(err,jobs){if(!err){// jobs is an array of Job objects.}});//-// To control how many API requests are made and page through the results// manually, set `autoPaginate` to `false`.//-functionmanualPaginationCallback(err,jobs,nextQuery,apiRespose){if(nextQuery){// More results exist.bigquery.getJobs(nextQuery,manualPaginationCallback);}}bigquery.getJobs({autoPaginate:false},manualPaginationCallback);//-// If the callback is omitted, we'll return a Promise.//-bigquery.getJobs().then(function(data){constjobs=data[0];});

getJobs(options, callback)

getJobs(options:GetJobsOptions,callback:GetJobsCallback):void;
Parameters
NameDescription
optionsGetJobsOptions
callbackGetJobsCallback
Returns
TypeDescription
void

getJobs(callback)

getJobs(callback:GetJobsCallback):void;
Parameter
NameDescription
callbackGetJobsCallback
Returns
TypeDescription
void

getJobsStream(options)

getJobsStream(options?:GetJobsOptions):ResourceStream<Job>;
Parameter
NameDescription
optionsGetJobsOptions
Returns
TypeDescription
ResourceStream<Job>

getTypeDescriptorFromProvidedType_(providedType)

staticgetTypeDescriptorFromProvidedType_(providedType:string|ProvidedTypeStruct|ProvidedTypeArray):ValueType;

Return a value's provided type.

Parameter
NameDescription
providedTypestring |ProvidedTypeStruct |ProvidedTypeArray

The type.

Returns
TypeDescription
ValueType

{string} The valid type provided.

getTypeDescriptorFromValue_(value)

staticgetTypeDescriptorFromValue_(value:unknown):ValueType;

Detect a value's type.

Parameter
NameDescription
valueunknown

The value.

Returns
TypeDescription
ValueType

{string} The type detected from the value.

int(value, typeCastOptions)

int(value:string|number|IntegerTypeCastValue,typeCastOptions?:IntegerTypeCastOptions):BigQueryInt;
Parameters
NameDescription
valuestring | number |IntegerTypeCastValue
typeCastOptionsIntegerTypeCastOptions
Returns
TypeDescription
BigQueryInt

int(value, typeCastOptions)

staticint(value:string|number|IntegerTypeCastValue,typeCastOptions?:IntegerTypeCastOptions):BigQueryInt;

A BigQueryInt wraps 'INT64' values. Can be used to maintain precision.

Parameters
NameDescription
valuestring | number |IntegerTypeCastValue

The INT64 value to convert.

typeCastOptionsIntegerTypeCastOptions

Configuration to convert value. Must provide anintegerTypeCastFunction to handle conversion.

Returns
TypeDescription
BigQueryInt

{BigQueryInt}

Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();constlargeIntegerValue=Number.MAX_SAFE_INTEGER+1;constoptions={integerTypeCastFunction:value=>value.split(),};constbqInteger=bigquery.int(largeIntegerValue,options);constcustomValue=bqInteger.valueOf();// customValue is the value returned from your `integerTypeCastFunction`.

job(id, options)

job(id:string,options?:JobOptions):Job;

Create a reference to an existing job.

Parameters
NameDescription
idstring

ID of the job.

optionsJobOptions

Configuration object.

Returns
TypeDescription
Job
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();constmyExistingJob=bigquery.job('job-id');

mergeSchemaWithRows_(schema, rows, options)

staticmergeSchemaWithRows_(schema:TableSchema|TableField|undefined,rows:TableRow[],options:{wrapIntegers:boolean|IntegerTypeCastOptions;selectedFields?:string[];parseJSON?:boolean;}):any[];

Merge a rowset returned from the API with a table schema.

Parameters
NameDescription
schemaTableSchema |TableField | undefined
rowsTableRow[]
options{ wrapIntegers: boolean |IntegerTypeCastOptions; selectedFields?: string[]; parseJSON?: boolean; }
Returns
TypeDescription
any[]

Fields using their matching names from the table's schema.

query(query, options)

query(query:string,options?:QueryOptions):Promise<QueryRowsResponse>;

Run a query scoped to your project. For manual pagination please refer to .

SeeJobs: query API Documentation

Parameters
NameDescription
querystring

A string SQL query or configuration object. For all available options, seeJobs: query request body.

optionsQueryOptions

Configuration object for query results.

Returns
TypeDescription
Promise<QueryRowsResponse>
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();constquery='SELECT url FROM `publicdata.samples.github_nested` LIMIT100';bigquery.query(query,function(err,rows){if(!err){// rows is an array of results.}});//-// Positional SQL parameters are supported.//-bigquery.query({query:['SELECT url','FROM `publicdata.samples.github_nested`','WHERE repository.owner = ?'].join(' '),params:['google']},function(err,rows){});//-// Or if you prefer to name them, that's also supported.//-bigquery.query({query:['SELECT url','FROM `publicdata.samples.github_nested`','WHERE repository.owner = @owner'].join(' '),params:{owner:'google'}},function(err,rows){});//-// Providing types for SQL parameters is supported.//-bigquery.query({query:['SELECT url','FROM `publicdata.samples.github_nested`','WHERE repository.owner = ?'].join(' '),params:[null],types:['string']},function(err,rows){});//-// If you need to use a `DATE`, `DATETIME`, `TIME`, or `TIMESTAMP` type in// your query, see {@link BigQuery.date}, {@link BigQuery.datetime},// {@link BigQuery.time}, and {@link BigQuery.timestamp}.//-//-// If the callback is omitted, we'll return a Promise.//-bigquery.query(query).then(function(data){constrows=data[0];});

query(query, options)

query(query:Query,options?:QueryOptions):Promise<SimpleQueryRowsResponse>;
Parameters
NameDescription
queryQuery
optionsQueryOptions
Returns
TypeDescription
Promise<SimpleQueryRowsResponse>

query(query, options, callback)

query(query:string,options:QueryOptions,callback?:QueryRowsCallback):void;
Parameters
NameDescription
querystring
optionsQueryOptions
callbackQueryRowsCallback
Returns
TypeDescription
void

query(query, options, callback)

query(query:Query,options:QueryOptions,callback?:SimpleQueryRowsCallback):void;
Parameters
NameDescription
queryQuery
optionsQueryOptions
callbackSimpleQueryRowsCallback
Returns
TypeDescription
void

query(query, callback)

query(query:string,callback?:QueryRowsCallback):void;
Parameters
NameDescription
querystring
callbackQueryRowsCallback
Returns
TypeDescription
void

query(query, callback)

query(query:Query,callback?:SimpleQueryRowsCallback):void;
Parameters
NameDescription
queryQuery
callbackSimpleQueryRowsCallback
Returns
TypeDescription
void

queryAsStream_(query, callback)

queryAsStream_(query:Query,callback?:SimpleQueryRowsCallback):void;

This method will be called bycreateQueryStream(). It is required to properly set theautoPaginate option value.

Parameters
NameDescription
queryQuery
callbackSimpleQueryRowsCallback
Returns
TypeDescription
void

range(value, elementType)

range(value:string,elementType?:string):BigQueryRange;

A range represents contiguous range between two dates, datetimes, or timestamps. The lower and upper bound for the range are optional. The lower bound is inclusive and the upper bound is exclusive.

Parameters
NameDescription
valuestring

The range API string or start/end with dates/datetimes/timestamp ranges.

elementTypestring

The range element type - DATE|DATETIME|TIMESTAMP

Returns
TypeDescription
BigQueryRange
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();consttimestampRange=bigquery.range('[2020-10-01 12:00:00+08, 2020-12-31 12:00:00+08)','TIMESTAMP');

range(value, elementType)

staticrange(value:string|BigQueryRangeOptions,elementType?:string):BigQueryRange;

A range represents contiguous range between two dates, datetimes, or timestamps. The lower and upper bound for the range are optional. The lower bound is inclusive and the upper bound is exclusive.

BigQuery.range

Parameters
NameDescription
valuestring |BigQueryRangeOptions

The range API string or start/end with dates/datetimes/timestamp ranges.

elementTypestring

The range element type - DATE|DATETIME|TIMESTAMP

Returns
TypeDescription
BigQueryRange
Example
const{BigQuery}=require('@google-cloud/bigquery');consttimestampRange=BigQuery.range('[2020-10-01 12:00:00+08, 2020-12-31 12:00:00+08)','TIMESTAMP');

time(value)

time(value:BigQueryTimeOptions|string):BigQueryTime;
Parameter
NameDescription
valueBigQueryTimeOptions | string
Returns
TypeDescription
BigQueryTime

time(value)

statictime(value:BigQueryTimeOptions|string):BigQueryTime;

ATIME data type represents a time, independent of a specific date.

Parameter
NameDescription
valueBigQueryTimeOptions | string

The time. If a string, this should be in the format the API describes:[H]H:[M]M:[S]S[.DDDDDD]. Otherwise, provide an object.

Returns
TypeDescription
BigQueryTime
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();consttime=bigquery.time('14:00:00');// 2:00 PM//-// Alternatively, provide an object.//-consttime=bigquery.time({hours:14,minutes:0,seconds:0});

timestamp(value)

timestamp(value:Date|PreciseDate|string|number):BigQueryTimestamp;

A timestamp represents an absolute point in time, independent of any time zone or convention such as Daylight Savings Time.

The recommended input here is aDate orPreciseDate class. If passing as astring, it should be Timestamp literals: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#timestamp_literals. When passing anumber input, it should be epoch seconds in float representation.

Parameter
NameDescription
valueDate |PreciseDate | string | number

The time.

Returns
TypeDescription
BigQueryTimestamp
Example
const{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();consttimestamp=bigquery.timestamp(newDate());

timestamp(value)

statictimestamp(value:Date|PreciseDate|string|number):BigQueryTimestamp;

A timestamp represents an absolute point in time, independent of any time zone or convention such as Daylight Savings Time.

The recommended input here is aDate orPreciseDate class. If passing as astring, it should be Timestamp literals: https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#timestamp_literals. When passing anumber input, it should be epoch seconds in float representation.

BigQuery.timestamp

Parameter
NameDescription
valueDate |PreciseDate | string | number

The time.

Returns
TypeDescription
BigQueryTimestamp
Example
const{BigQuery}=require('@google-cloud/bigquery');consttimestamp=BigQuery.timestamp(newDate());

valueToQueryParameter_(value, providedType)

staticvalueToQueryParameter_(value:any,providedType?:string|ProvidedTypeStruct|ProvidedTypeArray):bigquery.IQueryParameter;

Convert a value into aqueryParameter object.

SeeJobs.query API Reference Docs (see `queryParameters`)

Parameters
NameDescription
valueany

The value.

providedTypestring |ProvidedTypeStruct |ProvidedTypeArray

Provided query parameter type.

Returns
TypeDescription
bigquery.IQueryParameter

{object} A properly-formedqueryParameter object.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-30 UTC.