Cloud Spanner Client Library for Node.js

repository·main·Indexed 41 days ago

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

A programmatic interface for Google Cloud Spanner, a fully managed relational database service. Version 8.6.0 of the @google-cloud/spanner library enables developers to perform transactional queries, manage instances, handle global-scale databases, and perform DML operations with returning clauses. It supports multiplexed and regular session modes, client-side metrics, JSON data types, and index management.

Tokens
26.5K
Snippets
105
Records
131
Agent score
100%

What's inside Cloud Spanner

  1. Overview of Cloud Spanner Node.js Samples

    main

    The nodejs-spanner samples repository provides a comprehensive collection of code examples for interacting with Cloud Spanner using the Node.js client library. These samples cover a wide range of operations including:

    • Backup Management: Creating, deleting, restoring, and canceling backups, including support for KMS encryption keys.
    • Database Administration: Managing database roles, creating/updating databases, and configuring default leaders.
    • Schema Management: Creating and altering tables, indexes, sequences, and foreign keys (including cascade actions) for both GoogleSQL and PostgreSQL dialects.
    • Data Manipulation: Performing DML (Insert, Update, Delete) with RETURNING clauses, batch writes, and working with specialized types like JSON, JSONB, and Protobuf.
    • Querying: Executing SQL queries, using parameters, directed reads, and utilizing indexes.
    • Transactions: Implementing read-write transactions with various isolation levels, lock modes, and timeouts.
    • Instance Management: Configuring instances, autoscaling, and managing instance configurations.
    • Observability: Implementing tracing with OpenTelemetry and OTLP.
    • PostgreSQL Dialect: Specific samples for Spanner's PostgreSQL interface, including sequence management and dialect-specific syntax.
  2. Understand Session Modes: Multiplexed vs Regular Sessions

    main

    Cloud Spanner supports two session modes. The Node.js client enables Multiplexed Sessions by default to reduce session management overhead and minimize errors.

    Multiplexed Sessions

    This is the default mode. It is designed to be more efficient for session management.

    Regular Sessions

    To use regular sessions instead of the default multiplexed mode, you must disable multiplexed sessions using specific environment variables. Depending on your transaction type, set the following to false:

    Transaction TypeEnvironment Variables to set to false
    Read-Only TransactionsGOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS
    Partitioned OperationsGOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS<br>GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_PARTITIONED_OPS
    Read-Write TransactionsGOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS<br>GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_FOR_RW
  3. Understand library versioning and stability

    main

    This library follows Semantic Versioning and is classified as stable.

    As a stable library, the code surface is not subject to backwards-incompatible changes unless absolutely necessary (such as critical security issues), and such changes are preceded by an extensive deprecation period. Issues and requests for stable libraries are prioritized.

  4. Understand metric filtering criteria for the exporter

    main

    The custom metric exporter does not export all metrics. It applies specific filters to ensure only relevant Spanner data is sent to Google Cloud Monitoring. To be exported, a metric must meet both of the following conditions:

    1. The metric scope must be set to spanner-nodejs.
    2. The metric name must match one of the following predefined values:
      • attempt_latencies
      • attempt_count
      • operation_latencies
      • operation_count
      • gfe_latencies
      • gfe_connectivity_error_count
      • afe_latencies
      • afe_connectivity_error_count
  5. Quickstart for Cloud Spanner Node.js Client

    main

    To get started with the Cloud Spanner Node.js client, follow these steps:

    1. Before you begin

    • Select or create a Google Cloud Platform project.
    • Enable billing for your project.
    • Enable the Cloud Spanner API.
    • Set up authentication to access the API from your local workstation.

    2. Install the client library

    Install the @google-cloud/spanner package using npm:

    npm install @google-cloud/spanner

    3. Use the client library

    Initialize the Spanner client, reference an instance and database, and execute a query using database.run().

    // Imports the Google Cloud client library
    const {Spanner} = require('@google-cloud/spanner');
    
    // Creates a client
    const spanner = new Spanner({projectId});
    
    // Gets a reference to a Cloud Spanner instance and database
    const instance = spanner.instance(instanceId);
    const database = instance.database(databaseId);
    
    // The query to execute
    const query = {
      sql: 'SELECT 1',
    };
    
    // Execute a simple SQL statement
    const [rows] = await database.run(query);
    console.log(`Query: ${rows.length} found.`);
    rows.forEach(row => console.log(row));
  6. Explore Cloud Spanner Node.js Client Samples

    main

    The nodejs-docs-samples-spanner repository contains a wide variety of code samples demonstrating how to use the @google-cloud/spanner client library. These samples cover advanced features including:

    • Data Types: Working with proto columns, enum types, timestamp, struct, and sequence operations (create, alter, drop).
    • Transactions: Performing read-write transactions with specific read-lock-mode, isolation level (e.g., repeatable reads), statement timeout, transaction timeout, and the use of transaction tags.
    • Querying & Performance: Using queryoptions, setting request tags for single queries, and managing RPC priority for reads, queries, batch DML, and partitioned DML.
    • Schema Management: Creating and altering tables with foreign key delete cascade actions, and managing database schemas.
    • Administrative Tasks: Updating instance configurations (like default backup schedules) and backup schedules.
    • Security: Reading data using specific database roles.

    You can find the source code for each sample in the samples/ directory of the repository.

  7. Check Node.js version compatibility

    main

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

    If you must use a legacy/end-of-life version of Node.js, client libraries targeting those versions are available via npm dist-tags using the legacy-(version) convention. Note that legacy versions are not tested in continuous integration and may lack security patches or up-to-date dependencies.

    # Example: Install client libraries compatible with Node.js 8
    npm install @google-cloud/spanner@legacy-8
  8. Load data into Spanner using YCSB

    main

    To populate your Spanner database for benchmarking, follow these steps:

    1. Configure Environment Variables: Set GOOGLE_APPLICATION_CREDENTIALS to your service account JSON key path and GCLOUD_PROJECT to your Google Cloud project ID.
    2. Download YCSB Binding: Download the custom YCSB Cloud Spanner binding release.
    3. Execute Load: Use the ycsb load cloudspanner command. You must provide a workload file (-P), the target table (-p table=...), the Spanner instance and database names, and the desired recordcount and operationcount.
    $ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials.json
    $ export GCLOUD_PROJECT=your-project-id
    
    $ curl https://storage.googleapis.com/cloud-spanner-ycsb-custom-release/ycsb-cloudspanner-binding-0.13.0.tar.gz | tar -xzv
    $ ycsb-cloudspanner-binding-0.13.0/bin/ycsb load cloudspanner \
      -P ycsb-cloudspanner-binding-0.13.0/workloads/workloada \
      -p table=usertable -p cloudspanner.instance=ycsb-instance \
      -p recordcount=5000 -p operationcount=100 -p cloudspanner.database=ycsb \
      -threads 32
  9. Work with Spanner PostgreSQL dialect: DML operations

    main

    The client library supports various Data Manipulation Language (DML) operations for Spanner databases using the PostgreSQL dialect. Key capabilities include:

    • Basic Updates: Updating data in existing tables.
    • Parameterized DML: Executing DML statements with parameters for security and performance.
    • DML with RETURNING: Using the RETURNING clause during INSERT, UPDATE, or DELETE operations to retrieve modified rows.
    • Batch DML: Executing a batch of DML statements.
    • Partitioned DML: Executing partitioned DML for large-scale data modifications.

    Refer to the specific samples for implementation details.

    // Examples available in:
    // samples/pg-dml-getting-started-update.js
    // samples/pg-dml-with-parameter.js
    // samples/pg-dml-returning-insert.js
    // samples/pg-dml-returning-update.js
    // samples/pg-dml-returning-delete.js
    // samples/pg-dml-batch.js
    // samples/pg-dml-partitioned.js
  10. Configure end-to-end tracing for Spanner

    main

    End-to-end tracing helps debug latency issues specific to Spanner by linking client-side traces with server-side activity.

    To enable it:

    1. Opt-in: Set the environment variable SPANNER_ENABLE_END_TO_END_TRACING=true OR set enableEndToEndTracing: true in the observabilityOptions of the Spanner client.
    2. Set Propagation: Configure the OpenTelemetry global propagator to use W3CTraceContextPropagator.
    // 1. Opt-in via code
    const spanner = new Spanner({
      projectId: projectId,
      observabilityOptions: {
        tracerProvider: provider,
        enableEndToEndTracing: true,
      },
    });
    
    // 2. Set trace context propagation
    const { propagation } = require('@opentelemetry/api');
    const { W3CTraceContextPropagator } = require('@opentelemetry/core');
    propagation.setGlobalPropagator(new W3CTraceContextPropagator());
  11. Install OpenTelemetry dependencies for Spanner

    main

    To enable observability, add the following packages to your package.json:

    Required SDKs:

    • @opentelemetry/sdk-trace-base (^1.26.0)
    • @opentelemetry/sdk-trace-node (^1.26.0)

    Google Cloud Trace Exporter:

    • @google-cloud/opentelemetry-cloud-trace-exporter (^2.4.1)

    gRPC Instrumentation (Optional):

    • @opentelemetry/instrumentation (^0.53.0)
    • @opentelemetry/instrumentation-grpc (^0.53.0)
    // Required packages for OpenTelemetry SDKs
    "@opentelemetry/sdk-trace-base": "^1.26.0",
    "@opentelemetry/sdk-trace-node": "^1.26.0",
    
    // Package to use Google Cloud Trace exporter
    "@google-cloud/opentelemetry-cloud-trace-exporter": "^2.4.1",
    
    // Packages to enable gRPC instrumentation
    "@opentelemetry/instrumentation": "^0.53.0",
    "@opentelemetry/instrumentation-grpc": "^0.53.0"
  12. Run YCSB client benchmarks

    main

    You can run the Node.js client benchmarks using the pre-configured npm script or by calling the benchmark script directly with custom parameters for fine-grained control.

    # Option 1: Using npm script
    $ npm run ycsb
    
    # Option 2: Direct execution with custom parameters
    $ node benchmarks/ycsb.js run -P benchmarks/workloada -p table=usertable \
      -p cloudspanner.instance=ycsb-542756a4 -p recordcount=5000 \
      -p operationcount=100 -p cloudspanner.database=ycsb -p num_worker=1