Google BigQuery Client Library for Node.js

repository·main·Indexed 19 days ago

https://github.com/googleapis/nodejs-bigquery

A comprehensive Node.js interface for interacting with Google BigQuery, supporting dataset and table management, querying, and data ingestion. The library allows for loading data from local files or Google Cloud Storage (CSV, JSON, Avro, ORC, Parquet), managing partitioned and clustered tables, and executing standard SQL queries with various parameter types. Note: This repository is deprecated and has moved to google-cloud-node.

Tokens
22.6K
Snippets
100
Records
119
Agent score
66%

What's inside @google-cloud/bigquery

  1. Browse BigQuery Samples

    main

    The samples/ directory contains various Node.js implementation examples for BigQuery operations. Common tasks covered include:

    • Table Management: createTable, deleteTable, copyTable, addEmptyColumn, addColumnLoadAppend, addColumnQueryAppend.
    • Dataset Management: createDataset, deleteDataset.
    • Job Management: createJob, cancelJob.
    • Advanced Table Types: createTablePartitioned, createTableRangePartitioned, createTableClustered.
    • Views and Routines: createView, createRoutine, createRoutineDDL.
    • Data Extraction: extractTableToGCS, extractTableJSON, extractTableCompressed.
    • Authentication: clientJSONCredentials, authViewTutorial.
  2. Quickstart for Google BigQuery Node.js Client

    main

    To get started with the Google BigQuery Node.js client library, follow these prerequisite steps:

    1. Select or create a Cloud Platform project.
    2. Enable the Google BigQuery API in your selected project.
    3. Set up authentication to allow your local workstation to access the API (e.g., using Application Default Credentials).

    Note: This repository is deprecated. All content and history have moved to google-cloud-node.

  3. Check Node.js version compatibility

    main

    The BigQuery Node.js client library is compatible with all current active and maintenance versions of Node.js.

    If you are using an end-of-life version of Node.js, it is recommended to update to an actively supported LTS version. While legacy versions are supported on a best-effort basis, they are not tested in continuous integration, may lack security patches, and may have outdated dependencies.

  4. Explore BigQuery Node.js Client samples

    main

    The repository contains a comprehensive collection of code samples located in the samples/ directory. Each sample includes its own README.md with specific instructions for running that particular example. These samples cover a wide range of BigQuery operations, including:

    • Data Loading: Loading CSV, JSON, Avro, or Parquet from Google Cloud Storage (GCS) or local files.
    • Querying: Running standard queries, dry runs, paginated queries, and using various parameter types (named, positional, structs, arrays).
    • Table Management: Creating, deleting, copying, and updating tables (including partitioned and clustered tables).
    • Dataset Management: Creating, deleting, and updating datasets.
    • Job Management: Creating, getting, and cancelling jobs.
    • Authentication: Using client JSON credentials.
    • Schema Operations: Adding columns, relaxing columns, and handling nested/repeated schemas.
    • Other: Managing models, routines, and views.
  5. Run the BigQuery client benchmark

    main

    To run the performance benchmarks for the BigQuery client, execute the bench.js script using Node.js and provide a JSON file containing the queries to be benchmarked as an argument.

    Note on Accuracy: Because the BigQuery service caches requests, you should run the benchmark at least twice and disregard the results from the first run to ensure you are measuring actual processing performance rather than cache hits.

    node bench.js queries.json
  6. How query parameters and types are handled

    main

    The BigQuery client automatically translates JavaScript values into BigQuery types.

    Automatic Type Mapping:

    • BigQueryDate $\rightarrow$ DATE
    • BigQueryDatetime $\rightarrow$ DATETIME
    • BigQueryTime $\rightarrow$ TIME
    • BigQueryTimestamp $\rightarrow$ TIMESTAMP
    • Buffer $\rightarrow$ BYTES
    • Big $\rightarrow$ BIGNUMERIC (if length $\ge$ 10) or NUMERIC
    • BigQueryInt $\rightarrow$ INT64
    • Geography $\rightarrow$ GEOGRAPHY
    • Boolean $\rightarrow$ BOOL
    • Number $\rightarrow$ INT64 (if integer) or FLOAT64
    • String $\rightarrow$ STRING
    • Array $\rightarrow$ ARRAY
    • Object $\rightarrow$ STRUCT
    • JSON $\rightarrow$ JSON (via JSON.stringify)

    Important Note on Empty Arrays: If you provide an empty array as a query parameter, the client cannot infer the type. You must provide the type explicitly via the types field in your query options.

  7. Handle INT64 precision with BigQuery.int()

    main

    Standard JavaScript numbers cannot safely represent all INT64 values. To maintain precision, use bigquery.int() to wrap values in a BigQueryInt object. You can provide an integerTypeCastFunction via IntegerTypeCastOptions to define how the value is converted.

    If you attempt to decode an INT64 that exceeds Number.MAX_SAFE_INTEGER without custom casting, the library will throw an error.

    const {BigQuery} = require('@google-cloud/bigquery');
    const bigquery = new BigQuery();
    
    const largeIntegerValue = Number.MAX_SAFE_INTEGER + 1;
    
    const options = {
      integerTypeCastFunction: value => value.split(), // Example custom function
    };
    
    const bqInteger = bigquery.int(largeIntegerValue, options);
    const customValue = bqInteger.valueOf();
  8. Manage BigQuery Datasets with the Dataset class

    main

    The Dataset class allows you to interact with a specific BigQuery dataset. You can obtain a Dataset instance using BigQuery#createDataset or BigQuery#dataset.

    Common tasks include creating, getting, checking existence, and deleting datasets, as well as managing child resources like tables, routines, and models.

    const {BigQuery} = require('@google-cloud/bigquery');
    const bigquery = new BigQuery();
    const dataset = bigquery.dataset('institutions');
  9. Monitor BigQuery jobs using the Job class

    main

    The Job class represents a BigQuery job and can be used to check the status of a running job or fetch results from a completed one.

    Jobs are event emitters. The status of a job is polled continuously, but polling only begins after you register a complete listener. You should also register an error listener to catch issues that impede the job.

    To stop the job from polling for updates, remove all listeners using removeAllListeners().

    const {BigQuery} = require('@google-cloud/bigquery');
    const bigquery = new BigQuery();
    
    const job = bigquery.job('job-id');
    
    // Polling starts once this listener is registered
    job.on('complete', (metadata) => {
      // The job is complete.
    });
    
    job.on('error', (err) => {
      // An error occurred during the job.
    });
    
    // To stop polling:
    job.removeAllListeners();