-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(main): Migrate spanner spring data r2dbc (#2080)
- Loading branch information
Showing
36 changed files
with
2,588 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
:spring-data-commons-ref: https://docs.spring.io/spring-data/data-commons/docs/current/reference/html | ||
|
||
[#spring-data-cloud-spanner-r2dbc] | ||
== Cloud Spanner Spring Data R2DBC | ||
|
||
The Spring Data R2DBC Dialect for Cloud Spanner enables the usage of https://github.com/spring-projects/spring-data-r2dbc[Spring Data R2DBC] with Cloud Spanner. | ||
|
||
The goal of the Spring Data project is to create easy and consistent ways of using data access technologies from Spring Framework applications. | ||
|
||
=== Setup | ||
|
||
Maven coordinates, using <<getting-started.adoc#bill-of-materials, Spring Framework on Google Cloud BOM>>: | ||
|
||
[source,xml] | ||
---- | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>spring-cloud-spanner-spring-data-r2dbc</artifactId> | ||
</dependency> | ||
---- | ||
|
||
Gradle coordinates: | ||
|
||
[source,subs="normal"] | ||
---- | ||
dependencies { | ||
implementation("com.google.cloud:spring-cloud-spanner-spring-data-r2dbc") | ||
} | ||
---- | ||
=== Overview | ||
|
||
Spring Data R2DBC allows you to use the convenient features of Spring Data in a reactive application. | ||
These features include: | ||
|
||
* Spring configuration support using Java based `@Configuration` classes. | ||
* Annotation based mapping metadata. | ||
* Automatic implementation of Repository interfaces. | ||
* Support for Reactive Transactions. | ||
* Schema and data initialization utilities. | ||
|
||
See the https://docs.spring.io/spring-data/r2dbc/docs/current/reference/html/[Spring Data R2DBC documentation] for more information on how to use Spring Data R2DBC. | ||
|
||
=== Sample Application | ||
|
||
We provide a https://github.com/GoogleCloudPlatform/spring-cloud-gcp/tree/main/spring-cloud-gcp-samples/spring-cloud-spanner-r2dbc-samples[sample application] which demonstrates using the Spring Data R2DBC framework with Cloud Spanner in https://docs.spring.io/spring-framework/reference/web/webflux.html[Spring WebFlux]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
spring-cloud-gcp-samples/spring-cloud-spanner-r2dbc-samples/README.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Cloud Spanner Spring Data R2DBC sample | ||
|
||
This sample creates a table called `BOOK` on application startup, and deletes it prior to application shutdown. | ||
|
||
## Running the Sample | ||
|
||
image:http://gstatic.com/cloudssh/images/open-btn.svg[link=https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fcloud-spanner-r2dbc&cloudshell_open_in_editor=cloud-spanner-r2dbc-samples/cloud-spanner-spring-data-r2dbc-sample/README.adoc] | ||
|
||
|
||
1. Run the sample from the command line, providing `spanner.instance`, `spanner.database` and `gcp.project` properties: | ||
``` | ||
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspanner.instance=[SPANNER-INSTANCE] -Dspanner.database=[SPANNER-DATABASE] -Dgcp.project=GCP-PROJECT" | ||
``` | ||
|
||
2. Visit http://localhost:8080/index.html | ||
3. Try the different actions available | ||
- Listing all books. | ||
- Adding a new book with only title. | ||
- Adding a new book with extra details (a `Map` field in `Book` entity) stored as a spanner JSON column. | ||
- Adding a new book with a review (a custom class field in `Book` entity) stored as a spanner JSON column | ||
- Searching for a book by its ID. |
65 changes: 65 additions & 0 deletions
65
spring-cloud-gcp-samples/spring-cloud-spanner-r2dbc-samples/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<parent> | ||
<!-- Your own application should inherit from spring-boot-starter-parent --> | ||
<artifactId>spring-cloud-gcp-samples</artifactId> | ||
<groupId>com.google.cloud</groupId> | ||
<version>4.6.1-SNAPSHOT</version><!-- {x-version-update:spring-cloud-gcp:current} --> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>spring-cloud-spanner-r2dbc-samples</artifactId> | ||
<name>Spring Framework on Google Cloud Code Sample - Spanner-r2dbc</name> | ||
|
||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>spring-cloud-gcp-dependencies</artifactId> | ||
<version>${project.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>spring-cloud-spanner-spring-data-r2dbc</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-webflux</artifactId> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.projectreactor</groupId> | ||
<artifactId>reactor-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<configuration> | ||
<skip>true</skip> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
98 changes: 98 additions & 0 deletions
98
...-cloud-gcp-samples/spring-cloud-spanner-r2dbc-samples/src/main/java/com/example/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.domain.Persistable; | ||
import org.springframework.data.relational.core.mapping.Column; | ||
import org.springframework.data.relational.core.mapping.Table; | ||
|
||
/** Book entity. */ | ||
@Table | ||
public class Book implements Persistable { | ||
|
||
@Id | ||
@Column("ID") | ||
private String id; | ||
|
||
@Column("TITLE") | ||
private String title; | ||
|
||
@Column("EXTRADETAILS") | ||
private Map<String, String> extraDetails; | ||
|
||
@Column("REVIEWS") | ||
private Review review; | ||
|
||
@Column("CATEGORIES") | ||
private List<String> categories; | ||
|
||
public Book(String title, Map<String, String> extraDetails, Review review) { | ||
this.id = UUID.randomUUID().toString(); | ||
this.title = title; | ||
this.extraDetails = extraDetails; | ||
this.review = review; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
@Override | ||
public boolean isNew() { | ||
return true; | ||
} | ||
|
||
public String getTitle() { | ||
return this.title; | ||
} | ||
|
||
public Map<String, String> getExtraDetails() { | ||
return extraDetails; | ||
} | ||
|
||
public Review getReview() { | ||
return review; | ||
} | ||
|
||
public List<String> getCategories() { | ||
return categories; | ||
} | ||
|
||
public void setCategories(List<String> categories) { | ||
this.categories = categories; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Book{" | ||
+ "id='" | ||
+ id | ||
+ '\'' | ||
+ ", title='" | ||
+ title | ||
+ '\'' | ||
+ ", extraDetails=" | ||
+ (extraDetails == null ? "" : extraDetails.toString()) | ||
+ ", categories=" | ||
+ (categories == null ? "" : categories) | ||
+ '}'; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...-samples/spring-cloud-spanner-r2dbc-samples/src/main/java/com/example/BookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example; | ||
|
||
import org.springframework.data.repository.reactive.ReactiveCrudRepository; | ||
|
||
/** | ||
* Spring Data repository for books. | ||
* | ||
* <p>Query derivation is not supported yet. | ||
*/ | ||
interface BookRepository extends ReactiveCrudRepository<Book, String> {} |
Oops, something went wrong.