Skip to content

googleapis/java-spanner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Google Cloud Spanner Client for Java

Java idiomatic client for Cloud Spanner.

Maven Stability

Quickstart

If you are using Maven with BOM, add this to your pom.xml file:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>26.62.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-spanner</artifactId>
  </dependency>

If you are using Maven without the BOM, add this to your dependencies:

<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-spanner</artifactId>
  <version>6.99.0</version>
</dependency>

If you are using Gradle 5.x or later, add this to your dependencies:

implementation platform('com.google.cloud:libraries-bom:26.67.0')

implementation 'com.google.cloud:google-cloud-spanner'

If you are using Gradle without BOM, add this to your dependencies:

implementation 'com.google.cloud:google-cloud-spanner:6.100.0'

If you are using SBT, add this to your dependencies:

libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.100.0"

Authentication

See the Authentication section in the base directory's README.

Authorization

The client application making API calls must be granted authorization scopes required for the desired Cloud Spanner APIs, and the authenticated principal must have the IAM role(s) required to access GCP resources using the Cloud Spanner API calls.

Getting Started

Prerequisites

You will need a Google Cloud Platform Console project with the Cloud Spanner API enabled. You will need to enable billing to use Google Cloud Spanner. Follow these instructions to get your project set up. You will also need to set up the local development environment by installing the Google Cloud Command Line Interface and running the following commands in command line: gcloud auth login and gcloud config set project [YOUR PROJECT ID].

Installation and setup

You'll need to obtain the google-cloud-spanner library. See the Quickstart section to add google-cloud-spanner as a dependency in your code.

About Cloud Spanner

Cloud Spanner is a fully managed, mission-critical, relational database service that offers transactional consistency at global scale, schemas, SQL (ANSI 2011 with extensions), and automatic, synchronous replication for high availability. Be sure to activate the Cloud Spanner API on the Developer's Console to use Cloud Spanner from your project.

See the Cloud Spanner client library docs to learn how to use this Cloud Spanner Client Library.

Calling Cloud Spanner

Here is a code snippet showing a simple usage example. Add the following imports at the top of your file:

import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.Statement;

Then, to make a query to Spanner, use the following code:

// Instantiates a client
SpannerOptions options = SpannerOptions.newBuilder().build();
Spanner spanner = options.getService();
String instance = "my-instance";
String database = "my-database";
try {
  // Creates a database client
  DatabaseClient dbClient = spanner.getDatabaseClient(
    DatabaseId.of(options.getProjectId(), instance, database));
  // Queries the database
  try (ResultSet resultSet = dbClient.singleUse().executeQuery(Statement.of("SELECT 1"))) {
    // Prints the results
    while (resultSet.next()) {
      System.out.printf("%d\n", resultSet.getLong(0));
    }
  }
} finally {
  // Closes the client which will free up the resources used
  spanner.close();
}

Complete source code

In DatabaseSelect.java we put together all the code shown above in a single program.

Session Pool

The Cloud Spanner client maintains a session pool, as sessions are expensive to create and are intended to be long-lived. The client automatically takes a session from the pool and uses this executing queries and transactions. See Session Pool and Channel Pool Configuration for in-depth background information about sessions and gRPC channels and how these are handled in the Cloud Spanner Java client.

Metrics

Available client-side metrics:

  • spanner/max_in_use_sessions: This returns the maximum number of sessions that have been in use during the last maintenance window interval, so as to provide an indication of the amount of activity currently in the database.

  • spanner/max_allowed_sessions: This shows the maximum number of sessions allowed.

  • spanner/num_sessions_in_pool: This metric allows users to see instance-level and database-level data for the total number of sessions in the pool at this very moment.

  • spanner/num_acquired_sessions: This metric allows users to see the total number of acquired sessions.

  • spanner/num_released_sessions: This metric allows users to see the total number of released (destroyed) sessions.

  • spanner/get_session_timeouts: This gives you an indication of the total number of get session timed-out instead of being granted (the thread that requested the session is placed in a wait queue where it waits until a session is released into the pool by another thread) due to pool exhaustion since the server process started.

  • spanner/gfe_latency: This metric shows latency between Google's network receiving an RPC and reading back the first byte of the response.

  • spanner/gfe_header_missing_count: This metric shows the number of RPC responses received without the server-timing header, most likely indicating that the RPC never reached Google's network.

Instrument with OpenTelemetry

Cloud Spanner client supports OpenTelemetry Metrics, which gives insight into the client internals and aids in debugging/troubleshooting production issues. OpenTelemetry metrics will provide you with enough data to enable you to spot, and investigate the cause of any unusual deviations from normal behavior.

All Cloud Spanner Metrics are prefixed with spanner/ and uses cloud.google.com/java as Instrumentation Scope. The metrics will be tagged with:

  • database: the target database name.
  • instance_id: the instance id of the target Spanner instance.
  • client_id: the user defined database client id.

By default, the functionality is disabled. You need to add OpenTelemetry dependencies, enable OpenTelemetry metrics and must configure the OpenTelemetry with appropriate exporters at the startup of your application:

OpenTelemetry Dependencies

If you are using Maven, add this to your pom.xml file

<dependency>
      <groupId>io.opentelemetry</groupId>
      <artifactId>opentelemetry-sdk</artifactId>
      <version>{opentelemetry.version}</version>
</dependency>
<dependency>
      <groupId>io.opentelemetry</groupId>
      <artifactId>opentelemetry-sdk-metrics</artifactId>
      <version>{opentelemetry.version}</version>
</dependency>
<dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-exporter-otlp</artifactId>
    <version>{opentelemetry.version}</version>
</dependency>

If you are using Gradle, add this to your dependencies

compile 'io.opentelemetry:opentelemetry-sdk:{opentelemetry.version}'
compile 'io.opentelemetry:opentelemetry-sdk-metrics:{opentelemetry.version}'
compile 'io.opentelemetry:opentelemetry-exporter-oltp:{opentelemetry.version}'

OpenTelemetry Configuration

By default, all metrics are disabled. To enable metrics and configure the OpenTelemetry follow below:

// Enable OpenTelemetry metrics before injecting OpenTelemetry object.
SpannerOptions.enableOpenTelemetryMetrics();

SdkMeterProvider sdkMeterProvider = SdkMeterProvider.builder()
// Use Otlp exporter or any other exporter of your choice.
  .registerMetricReader(PeriodicMetricReader.builder(OtlpGrpcMetricExporter.builder().build())
  .build())
  .build();

OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
        .setMeterProvider(sdkMeterProvider)
        .build()

SpannerOptions options = SpannerOptions.newBuilder()
// Inject OpenTelemetry object via Spanner Options or register OpenTelemetry object as Global
  .setOpenTelemetry(openTelemetry)
  .build();

Spanner spanner = options.getService();

OpenTelemetry SQL Statement Tracing

The OpenTelemetry traces that are generated by the Java client include any request and transaction tags that have been set. The traces can also include the SQL statements that are executed and the name of the thread that executes the statement. Enable this with the enableExtendedTracing option:

SpannerOptions options = SpannerOptions.newBuilder()
  .setOpenTelemetry(openTelemetry)
  .setEnableExtendedTracing(true)
  .build();

This option can also be enabled by setting the environment variable SPANNER_ENABLE_EXTENDED_TRACING=true.

OpenTelemetry API Tracing

You can enable tracing of each API call that the Spanner client executes with the enableApiTracing option. These traces also include any retry attempts for an API call:

SpannerOptions options = SpannerOptions.newBuilder()
.setOpenTelemetry(openTelemetry)
.setEnableApiTracing(true)
.build();

This option can also be enabled by setting the environment variable SPANNER_ENABLE_API_TRACING=true.

Note: The attribute keys that are used for additional information about retry attempts and the number of requests might change in a future release.

Instrument with OpenCensus

Note: OpenCensus project is deprecated. See Sunsetting OpenCensus. We recommend migrating to OpenTelemetry, the successor project.

Cloud Spanner client supports Opencensus Metrics, which gives insight into the client internals and aids in debugging/troubleshooting production issues. OpenCensus metrics will provide you with enough data to enable you to spot, and investigate the cause of any unusual deviations from normal behavior.

All Cloud Spanner Metrics are prefixed with cloud.google.com/java/spanner

The metrics are tagged with:

  • database: the target database name.
  • instance_id: the instance id of the target Spanner instance.
  • client_id: the user defined database client id.
  • library_version: the version of the library that you're using.

By default, the functionality is disabled. You need to include opencensus-impl dependency to collect the data and exporter dependency to export to backend.

Click here for more information.

OpenCensus Dependencies

If you are using Maven, add this to your pom.xml file

<dependency>
  <groupId>io.opencensus</groupId>
  <artifactId>opencensus-impl</artifactId>
  <version>0.30.0</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>io.opencensus</groupId>
  <artifactId>opencensus-exporter-stats-stackdriver</artifactId>
  <version>0.30.0</version>
</dependency>

If you are using Gradle, add this to your dependencies

compile 'io.opencensus:opencensus-impl:0.30.0'
compile 'io.opencensus:opencensus-exporter-stats-stackdriver:0.30.0'

Configure the OpenCensus Exporter

At the start of your application configure the exporter:

import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter;
// Enable OpenCensus exporters to export metrics to Stackdriver Monitoring.
// Exporters use Application Default Credentials to authenticate.
// See https://developers.google.com/identity/protocols/application-default-credentials
// for more details.
// The minimum reporting period for Stackdriver is 1 minute.
StackdriverStatsExporter.createAndRegister();

Enable RPC Views

By default, all session metrics are enabled. To enable RPC views, use either of the following method:

// Register views for GFE metrics, including gfe_latency and gfe_header_missing_count.
SpannerRpcViews.registerGfeLatencyAndHeaderMissingCountViews();

// Register GFE Latency view. 
SpannerRpcViews.registerGfeLatencyView();

// Register GFE Header Missing Count view.
SpannerRpcViews.registerGfeHeaderMissingCountView();

Traces

Cloud Spanner client supports OpenTelemetry Traces, which gives insight into the client internals and aids in debugging/troubleshooting production issues.

By default, the functionality is disabled. You need to add OpenTelemetry dependencies, enable OpenTelemetry traces and must configure the OpenTelemetry with appropriate exporters at the startup of your application.

OpenTelemetry Dependencies

If you are using Maven, add this to your pom.xml file

<dependency>
      <groupId>io.opentelemetry</groupId>
      <artifactId>opentelemetry-sdk</artifactId>
      <version>{opentelemetry.version}</version>
</dependency>
<dependency>
      <groupId>io.opentelemetry</groupId>
      <artifactId>opentelemetry-sdk-trace</artifactId>
      <version>{opentelemetry.version}</version>
</dependency>
<dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-exporter-otlp</artifactId>
    <version>{opentelemetry.version}</version>
</dependency>

If you are using Gradle, add this to your dependencies

compile 'io.opentelemetry:opentelemetry-sdk:{opentelemetry.version}'
compile 'io.opentelemetry:opentelemetry-sdk-trace:{opentelemetry.version}'
compile 'io.opentelemetry:opentelemetry-exporter-oltp:{opentelemetry.version}'

OpenTelemetry Configuration

Note: Enabling OpenTelemetry traces will automatically disable OpenCensus traces.

// Enable OpenTelemetry traces
SpannerOptions.enableOpenTelemetryTraces();

// Create a new tracer provider
SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
      // Use Otlp exporter or any other exporter of your choice.
      .addSpanProcessor(SimpleSpanProcessor.builder(OtlpGrpcSpanExporter
          .builder().build()).build())
          .build();


OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
        .setTracerProvider(sdkTracerProvider)
        .build()

SpannerOptions options = SpannerOptions.newBuilder()
// Inject OpenTelemetry object via Spanner Options or register OpenTelmetry object as Global
  .setOpenTelemetry(openTelemetry)
  .build();

Spanner spanner = options.getService();

OpenTelemetry SQL Statement Tracing

The OpenTelemetry traces that are generated by the Java client include any request and transaction tags that have been set. The traces can also include the SQL statements that are executed and the name of the thread that executes the statement. Enable this with the enableExtendedTracing option:

SpannerOptions options = SpannerOptions.newBuilder()
  .setOpenTelemetry(openTelemetry)
  .setEnableExtendedTracing(true)
  .build();

This option can also be enabled by setting the environment variable SPANNER_ENABLE_EXTENDED_TRACING=true.

OpenTelemetry API Tracing

You can enable tracing of each API call that the Spanner client executes with the enableApiTracing option. These traces also include any retry attempts for an API call:

SpannerOptions options = SpannerOptions.newBuilder()
.setOpenTelemetry(openTelemetry)
.setEnableApiTracing(true)
.build();

This option can also be enabled by setting the environment variable SPANNER_ENABLE_API_TRACING=true.

Note: The attribute keys that are used for additional information about retry attempts and the number of requests might change in a future release.

Migrate from OpenCensus to OpenTelemetry

Using the OpenTelemetry OpenCensus Bridge, you can immediately begin exporting your metrics and traces with OpenTelemetry

Disable OpenCensus metrics

Disable OpenCensus metrics for Spanner by including the following code if you still possess OpenCensus dependencies and exporter.

SpannerOptions.disableOpenCensusMetrics();

Disable OpenCensus traces

Enabling OpenTelemetry traces for Spanner will automatically disable OpenCensus traces.

SpannerOptions.enableOpenTelemetryTraces();

Remove OpenCensus Dependencies and Code

Remove any OpenCensus-related code and dependencies from your codebase if all your dependencies are ready to move to OpenTelemetry.

  • Remove the OpenCensus Exporters which were configured here
  • Remove SpannerRPCViews reference which were configured here
  • Remove the OpenCensus dependencies which were added here

Update your Dashboards and Alerts

Update your dashboards and alerts to reflect below changes

  • Metrics name : cloud.google.com/java prefix has been removed from OpenTelemery metrics and instead has been added as Instrumenation Scope.
  • Metrics namespace : OpenTelmetry exporters uses workload.googleapis.com namespace opposed to custom.googleapis.com with OpenCensus.

Samples

Samples are in the samples/ directory.

Sample Source Code Try it
Add And Drop Database Role source code Open in Cloud Shell
Add Json Column Sample source code Open in Cloud Shell
Add Jsonb Column Sample source code Open in Cloud Shell
Add Numeric Column Sample source code Open in Cloud Shell
Add Proto Column Sample source code Open in Cloud Shell
Alter Sequence Sample source code Open in Cloud Shell
Alter Table With Foreign Key Delete Cascade Sample source code Open in Cloud Shell
Async Dml Example source code Open in Cloud Shell
Async Query Example source code Open in Cloud Shell
Async Query To List Async Example source code Open in Cloud Shell
Async Read Example source code Open in Cloud Shell
Async Read Only Transaction Example source code Open in Cloud Shell
Async Read Row Example source code