Spring Data Cloud Spanner

 




Spring Data Cloud Spanner

Spring Data Cloud Spanner is an extension of the Spring Data framework that provides a convenient and familiar way to interact with Google Cloud Spanner, a globally distributed, strongly consistent, and horizontally scalable relational database. This library simplifies common data access tasks by leveraging the power of Spring's conventions over configuration, allowing developers to write less boilerplate code.

Key Features and Concepts

  • Repository Abstraction: At its core, Spring Data Cloud Spanner provides a repository abstraction that allows you to define data access methods as simple interface declarations. The framework automatically generates the implementation at runtime.

  • CRUD Operations: It offers standard CrudRepository functionality for saving, retrieving, updating, and deleting entities.

  • Query Method Support: You can define custom queries by simply naming the repository method using a specific convention (e.g., findByNameAndEmail). The framework parses the method name and constructs the appropriate Cloud Spanner query.

  • Declarative Transactions: Integration with Spring's transaction management allows you to use the @Transactional annotation to ensure operations are atomic, consistent, isolated, and durable (ACID).

  • Spring Boot Integration: When used with Spring Boot, the library provides auto-configuration, making it easy to set up and get started by simply adding the dependency and configuring your Cloud Spanner connection properties.

Code Example

The following example demonstrates how to define an entity and a repository to perform basic CRUD operations with Cloud Spanner.

1. The Entity

import com.google.cloud.spring.data.spanner.core.mapping.Column;
import com.google.cloud.spring.data.spanner.core.mapping.PrimaryKey;
import com.google.cloud.spring.data.spanner.core.mapping.Table;

@Table(name = "users")
public class User {

    @PrimaryKey
    private String id;

    @Column(name = "user_name")
    private String name;

    private String email;

    public User(String id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }

    // Getters and setters omitted for brevity
}

2. The Repository Interface

import com.google.cloud.spring.data.spanner.repository.SpannerRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends SpannerRepository<User, String> {
    
    // Custom query method derived from the method name
    User findByName(String name);
}

3. Main Application Class

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpannerApplication implements CommandLineRunner {

    @Autowired
    private UserRepository userRepository;

    public static void main(String[] args) {
        SpringApplication.run(SpannerApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // Save a new user
        User newUser = new User("user-1", "John Doe", "john.doe@example.com");
        userRepository.save(newUser);
        System.out.println("Saved user: " + newUser.getName());

        // Find the user by ID
        userRepository.findById("user-1").ifPresent(user -> {
            System.out.println("Found user by ID: " + user.getName());
        });

        // Find the user by name using a custom query method
        User foundByName = userRepository.findByName("John Doe");
        System.out.println("Found user by name: " + foundByName.getEmail());
    }
}

Dependencies

To use Spring Data Cloud Spanner, you'll need to add the following Maven dependency to your project:

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-data-spanner</artifactId>
</dependency>

References and Further Reading

Comments