### Create Datasets and Tables Source: https://context7.com/googleapis/nodejs-bigquery/llms.txt Provides examples for creating new datasets and defining table schemas within those datasets. ```javascript async function createDataset() { const datasetId = 'my_new_dataset'; const options = { location: 'US' }; const [dataset] = await bigquery.createDataset(datasetId, options); console.log(`Dataset ${dataset.id} created.`); } async function createTable() { const datasetId = 'my_dataset'; const tableId = 'my_table'; const schema = [ {name: 'Name', type: 'STRING', mode: 'REQUIRED'}, {name: 'Age', type: 'INTEGER'}, {name: 'Weight', type: 'FLOAT'}, {name: 'IsMagic', type: 'BOOLEAN'}, ]; const options = { schema: schema, location: 'US' }; const [table] = await bigquery.dataset(datasetId).createTable(tableId, options); console.log(`Table ${table.id} created.`); } ``` -------------------------------- ### Install Google BigQuery Client Library Source: https://github.com/googleapis/nodejs-bigquery/blob/main/README.md Installs the official Google BigQuery Node.js client library via npm. This is a prerequisite for interacting with BigQuery services. ```bash npm install @google-cloud/bigquery ``` -------------------------------- ### Create a BigQuery Dataset Source: https://github.com/googleapis/nodejs-bigquery/blob/main/README.md Demonstrates how to initialize the BigQuery client and create a new dataset. It uses the asynchronous createDataset method and logs the resulting dataset ID. ```javascript const {BigQuery} = require('@google-cloud/bigquery'); async function createDataset() { const bigqueryClient = new BigQuery(); const [dataset] = await bigqueryClient.createDataset(datasetName); console.log(`Dataset ${dataset.id} created.`); } createDataset(); ``` -------------------------------- ### Initialize BigQuery Client Source: https://context7.com/googleapis/nodejs-bigquery/llms.txt Demonstrates how to instantiate the BigQuery client, which automatically handles authentication using Google Cloud Application Default Credentials. ```javascript const {BigQuery} = require('@google-cloud/bigquery'); const bigquery = new BigQuery(); ``` -------------------------------- ### Execute SQL Queries Source: https://context7.com/googleapis/nodejs-bigquery/llms.txt Shows how to run a SQL query as a job and retrieve the results. This method is suitable for standard analytical queries. ```javascript async function query() { const query = `SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013` WHERE state = 'TX' LIMIT 100`; const options = { query: query, location: 'US', }; const [job] = await bigquery.createQueryJob(options); console.log(`Job ${job.id} started.`); const [rows] = await job.getQueryResults(); rows.forEach(row => console.log(row)); } ``` -------------------------------- ### Load Data from Cloud Storage Source: https://context7.com/googleapis/nodejs-bigquery/llms.txt Demonstrates how to trigger a load job to import data from a CSV file stored in Google Cloud Storage into a BigQuery table. ```javascript async function loadData() { const datasetId = 'my_dataset'; const tableId = 'my_table'; const bucketName = 'my_bucket'; const fileName = 'data.csv'; const metadata = { sourceFormat: 'CSV', skipLeadingRows: 1, autodetect: true }; const [job] = await bigquery.dataset(datasetId).table(tableId).load(storage.bucket(bucketName).file(fileName), metadata); console.log(`Job ${job.id} completed.`); } ``` -------------------------------- ### Copy BigQuery Tables Source: https://context7.com/googleapis/nodejs-bigquery/llms.txt Illustrates how to copy a source table to a destination table within BigQuery using the copy method. ```javascript async function copyTable() { const srcDatasetId = 'source_dataset'; const srcTableId = 'source_table'; const destDatasetId = 'dest_dataset'; const destTableId = 'dest_table'; const [job] = await bigquery .dataset(srcDatasetId) .table(srcTableId) .copy(bigquery.dataset(destDatasetId).table(destTableId)); console.log(`Job ${job.id} completed.`); } ``` -------------------------------- ### Manage and Monitor BigQuery Jobs Source: https://context7.com/googleapis/nodejs-bigquery/llms.txt Shows how to list, retrieve, monitor, and cancel BigQuery jobs. It includes event listeners for tracking job completion and error states. ```javascript async function manageJob() { const [jobs] = await bigquery.getJobs(); const job = bigquery.job('job_id'); const [metadata] = await job.getMetadata(); console.log('Job status:', metadata.status.state); await job.cancel(); job.on('complete', metadata => { console.log('Job complete'); }); job.on('error', err => { console.error('Job error:', err); }); } ``` -------------------------------- ### Manage Data Ingestion and Retrieval Source: https://context7.com/googleapis/nodejs-bigquery/llms.txt Covers streaming inserts for real-time data ingestion and standard row retrieval from existing tables. ```javascript async function insertRowsAsStream() { const datasetId = 'my_dataset'; const tableId = 'my_table'; const rows = [{name: 'Tom', age: 30}, {name: 'Jane', age: 32}]; await bigquery.dataset(datasetId).table(tableId).insert(rows); console.log(`Inserted ${rows.length} rows`); } async function getRows() { const datasetId = 'my_dataset'; const tableId = 'my_table'; const [rows] = await bigquery.dataset(datasetId).table(tableId).getRows(); console.log('Rows:', rows); } ``` -------------------------------- ### Extract BigQuery Table to GCS Source: https://github.com/googleapis/nodejs-bigquery/blob/main/README.md Demonstrates how to export a BigQuery table to Google Cloud Storage. Supports various formats and compression options. ```javascript async function extractTableToGCS(datasetId, tableId, bucketName, fileName) { const {BigQuery} = require('@google-cloud/bigquery'); const bigquery = new BigQuery(); const destinationUri = `gs://${bucketName}/${fileName}`; const file = bigquery.dataset(datasetId).table(tableId).file(destinationUri); const [job] = await file.extract(); console.log(`Job ${job.id} started.`); } ``` -------------------------------- ### Export BigQuery Table to Cloud Storage Source: https://context7.com/googleapis/nodejs-bigquery/llms.txt Demonstrates how to export data from a BigQuery table to a CSV file in Google Cloud Storage. It uses the extract method with options for format and compression. ```javascript async function extractTable() { const datasetId = 'my_dataset'; const tableId = 'my_table'; const bucketName = 'my_bucket'; const fileName = 'export.csv'; const options = { format: 'CSV', gzip: true, }; const [job] = await bigquery .dataset(datasetId) .table(tableId) .extract(storage.bucket(bucketName).file(fileName), options); console.log(`Job ${job.id} completed.`); } ``` -------------------------------- ### Retrieve BigQuery Dataset Metadata Source: https://github.com/googleapis/nodejs-bigquery/blob/main/README.md Fetches metadata for a specific BigQuery dataset, including its properties and labels. ```javascript async function getDataset(datasetId) { const {BigQuery} = require('@google-cloud/bigquery'); const bigquery = new BigQuery(); const [dataset] = await bigquery.dataset(datasetId).get(); console.log('Dataset metadata:', dataset.metadata); } ``` -------------------------------- ### Manage Table-Level IAM Policies Source: https://context7.com/googleapis/nodejs-bigquery/llms.txt Provides methods to retrieve and update IAM policies for a specific BigQuery table, allowing for granular access control. ```javascript async function manageIamPolicy() { const datasetId = 'my_dataset'; const tableId = 'my_table'; const table = bigquery.dataset(datasetId).table(tableId); const [policy] = await table.getIamPolicy(); console.log('Current policy:', policy); const newPolicy = { bindings: [ { role: 'roles/bigquery.dataViewer', members: ['user:viewer@example.com'], }, ], }; await table.setIamPolicy(newPolicy); } ``` -------------------------------- ### Stream Data into BigQuery Table Source: https://github.com/googleapis/nodejs-bigquery/blob/main/README.md Inserts rows into a BigQuery table using the streaming API, allowing for real-time data ingestion. ```javascript async function insertRowsAsStream(datasetId, tableId, rows) { const {BigQuery} = require('@google-cloud/bigquery'); const bigquery = new BigQuery(); await bigquery.dataset(datasetId).table(tableId).insert(rows); console.log(`Inserted ${rows.length} rows.`); } ``` -------------------------------- ### Stream Query Results Source: https://context7.com/googleapis/nodejs-bigquery/llms.txt Utilizes a stream-based approach to process large query results efficiently without loading the entire result set into memory. ```javascript function streamQueryResults() { const query = 'SELECT * FROM `my_dataset.my_table`'; bigquery.createQueryStream(query) .on('data', row => console.log(row)) .on('end', () => console.log('Stream complete')) .on('error', err => console.error('Error:', err)); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.