# 在Pivotal GemFire中訪問數據
本指南將引導您完成構建 應用程序的過程 [Apache Geode](https://geode.apache.org/) 數據管理系統 。
## 你會建立什么
您將使用 [Spring Data for Apache Geode](https://spring.io/projects/spring-data-geode) 來存儲和檢索POJO。
## 你需要什么
* 約15分鐘
* 最喜歡的文本編輯器或IDE
* [JDK 1.8](http://www.oracle.com/technetwork/java/javase/downloads/index.html) 或更高版本
* [Gradle 4+](http://www.gradle.org/downloads) 或 [Maven 3.2+](https://maven.apache.org/download.cgi)
* 您還可以將代碼直接導入到IDE中:
* [彈簧工具套件(STS)](https://spring.io/guides/gs/sts)
* [IntelliJ IDEA](https://spring.io/guides/gs/intellij-idea/)
## 如何完成本指南
像大多數Spring 一樣 [入門指南](https://spring.io/guides) ,您可以從頭開始并完成每個步驟,也可以繞過您已經熟悉的基本設置步驟。 無論哪種方式,您最終都可以使用代碼。
要 **從頭開始** ,請繼續進行“ [從Spring Initializr開始”](https://spring.io/guides/gs/accessing-data-gemfire/#scratch) 。
要 **跳過基礎知識** ,請執行以下操作:
* [下載](https://github.com/spring-guides/gs-accessing-data-gemfire/archive/master.zip) 并解壓縮本指南的源存儲庫,或使用 對其進行克隆 [Git](https://spring.io/understanding/Git) : `git clone [https://github.com/spring-guides/gs-accessing-data-gemfire.git](https://github.com/spring-guides/gs-accessing-data-gemfire.git)`
* 光盤進入 `gs-accessing-data-gemfire/initial`
* 繼續 [定義一個簡單的實體](https://spring.io/guides/gs/accessing-data-gemfire/#initial) 。
**完成后** ,您可以根據中的代碼檢查結果 `gs-accessing-data-gemfire/complete`.
## 從Spring Initializr開始
對于所有Spring應用程序,您應該從 開始 [Spring Initializr](https://start.spring.io) 。 Spring Initializr提供了一種快速的方法來提取應用程序所需的所有依賴關系,并為您完成了許多設置。 本示例需要“ *Spring for Apache Geode* ”依賴項。
以下清單顯示了一個示例 `pom.xml` 使用Maven時文件:
~~~
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
</parent>
<groupId>org.springframework</groupId>
<artifactId>gs-accessing-data-gemfire</artifactId>
<version>0.1.0</version>
<properties>
<spring-shell.version>1.2.0.RELEASE</spring-shell.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-geode</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell</artifactId>
<version>${spring-shell.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
~~~
以下清單顯示了一個示例 `build.gradle`使用Gradle時的文件:
~~~
plugins {
id 'org.springframework.boot' version '2.4.1'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id "io.freefair.lombok" version "5.3.0"
id 'java'
}
apply plugin: 'eclipse'
apply plugin: 'idea'
group = "org.springframework"
version = "0.1.0"
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter"
implementation "org.springframework.data:spring-data-geode"
implementation "org.projectlombok:lombok"
runtimeOnly "org.springframework.shell:spring-shell:1.2.0.RELEASE"
}
bootJar {
baseName = 'gs-accessing-data-gemfire'
version = '0.1.0'
}
~~~
## 定義一個簡單的實體
Apache Geode是一個 *內存中數據網格* (IMDG),可將數據映射到區域。 可以配置分布式區域,以在群集中的多個節點之間分區和復制數據。 但是,在本指南中,您將使用 `LOCAL` 區域,因此您不必設置任何額外的東西,例如整個服務器集群。
Apache Geode是鍵/值存儲,而Region則實現了 `java.util.concurrent.ConcurrentMap`界面。 雖然您可以將Region視為 `java.util.Map`,它比簡單的Java要復雜得多 `Map` 給定的數據在區域內進行分發,復制和總體管理。
在此示例中,您存儲 `Person` 僅使用一些注釋的Apache Geode(區域)中的對象。
`src/main/java/hello/Person.java`
~~~
package hello;
import java.io.Serializable;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.gemfire.mapping.annotation.Region;
import lombok.Getter;
@Region(value = "People")
public class Person implements Serializable {
@Id
@Getter
private final String name;
@Getter
private final int age;
@PersistenceConstructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return String.format("%s is %d years old", getName(), getAge());
}
}
~~~
在這里你有一個 `Person` 有兩個領域的課程, `name` 和 `age`。 您還具有一個持久的構造函數,用于在創建新實例時填充實體。 該類使用 [Project Lombok](https://projectlombok.org/) 簡化了實現。
請注意,該類用注釋 `@Region("People")`。 當Apache Geode存儲此類的實例時,將在“人員”區域內創建一個新條目。 此類也標志著 `name` 場與 `@Id`。 這表示用于識別和跟蹤 `Person`Apache Geode中的數據。 本質上, `@Id` 帶注釋的字段(例如 `name`)是關鍵, `Person`instance是鍵/值條目中的值。 Apache Geode中沒有自動生成密鑰的功能,因此您必須設置id(即 `name`),然后再將該實體保存到Apache Geode。
下一個重要的部分是人的年齡。 在本指南的后面,您將使用它來設置一些查詢。
被覆蓋 `toString()` 方法將打印出該人的姓名和年齡。
## 創建簡單的查詢
*適用于Apache Geode的Spring Data* 專注于使用Spring在Apache Geode中存儲和訪問數據。 它還從 繼承了強大的功能 *Spring Data Commons* 項目 ,例如導出查詢的功能。 本質上,您不必學習Apache Geode(OQL)的查詢語言。 您只需編寫一些方法,框架即可為您編寫查詢。
要查看其工作原理,請創建一個查詢接口 `Person` 存儲在Apache Geode中的對象。
`src/main/java/hello/PersonRepository.java`
~~~
package hello;
import org.springframework.data.gemfire.repository.query.annotation.Trace;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<Person, String> {
@Trace
Person findByName(String name);
@Trace
Iterable<Person> findByAgeGreaterThan(int age);
@Trace
Iterable<Person> findByAgeLessThan(int age);
@Trace
Iterable<Person> findByAgeGreaterThanAndAgeLessThan(int greaterThanAge, int lessThanAge);
}
~~~
`PersonRepository` 擴展 `CrudRepository`接口, *Spring Data Commons的* 并為 的值和id(鍵)指定通用類型參數的類型 *存儲庫* 使用 ,即 `Person` 和 `String`, 分別。 該界面開箱即用,具有許多操作,包括基本的CRUD(CREATE,READ UPDATE,DELETE)和簡單的查詢(例如, `findById(..)`)數據訪問操作。
您可以根據需要定義其他查詢,只需聲明它們的方法簽名即可。 在這種情況下,您添加 `findByName`,本質上是搜索類型的對象 `Person` 并找到一個與之匹配的 `name`.
您還有:
* `findByAgeGreaterThan` 尋找一定年齡以上的人
* `findByAgeLessThan` 尋找某個年齡以下的人
* `findByAgeGreaterThanAndAgeLessThan` 尋找某個年齡段的人
讓我們進行連線,看看它是什么樣子!
## 創建一個應用程序類
在這里,您將創建一個具有所有組件的Application類。
`src/main/java/hello/Application.java`
~~~
package hello;
import static java.util.Arrays.asList;
import static java.util.stream.StreamSupport.stream;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.gemfire.config.annotation.ClientCacheApplication;
import org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
@SpringBootApplication
@ClientCacheApplication(name = "AccessingDataGemFireApplication")
@EnableEntityDefinedRegions(
basePackageClasses = Person.class,
clientRegionShortcut = ClientRegionShortcut.LOCAL
)
@EnableGemfireRepositories
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
ApplicationRunner run(PersonRepository personRepository) {
return args -> {
Person alice = new Person("Adult Alice", 40);
Person bob = new Person("Baby Bob", 1);
Person carol = new Person("Teen Carol", 13);
System.out.println("Before accessing data in Apache Geode...");
asList(alice, bob, carol).forEach(person -> System.out.println("\t" + person));
System.out.println("Saving Alice, Bob and Carol to Pivotal GemFire...");
personRepository.save(alice);
personRepository.save(bob);
personRepository.save(carol);
System.out.println("Lookup each person by name...");
asList(alice.getName(), bob.getName(), carol.getName())
.forEach(name -> System.out.println("\t" + personRepository.findByName(name)));
System.out.println("Query adults (over 18):");
stream(personRepository.findByAgeGreaterThan(18).spliterator(), false)
.forEach(person -> System.out.println("\t" + person));
System.out.println("Query babies (less than 5):");
stream(personRepository.findByAgeLessThan(5).spliterator(), false)
.forEach(person -> System.out.println("\t" + person));
System.out.println("Query teens (between 12 and 20):");
stream(personRepository.findByAgeGreaterThanAndAgeLessThan(12, 20).spliterator(), false)
.forEach(person -> System.out.println("\t" + person));
};
}
}
~~~
在配置中,您需要添加 `@EnableGemfireRepositories` 注解。
* 默認, `@EnableGemfireRepositories`將會在當前包中掃描任何擴展了Spring Data 之一的 *Repository* 接口 接口。 使用它的 `basePackageClasses = MyRepository.class`安全地告訴 *Spring Data for Apache Geode* 按類型掃描不同的根包,以查找特定于應用程序的 *存儲庫* 擴展。
需要一個包含1個或多個Regions的Apache Geode緩存來存儲所有數據。 為此,您可以使用 1個 *Spring Data中的 作為Apache Geode* 方便的基于配置的注釋: `@ClientCacheApplication`, `@PeerCacheApplication or `@CacheServerApplication`.
Apache Geode支持不同的緩存拓撲,例如客戶端/服務器,對等(p2p)甚至WAN安排。 在p2p中,對等緩存實例嵌入在應用程序中,您的應用程序將能夠以對等緩存成員的身份參與集群。 但是,您的應用程序受群集中對等成員的所有限制,因此,它不像客戶端/服務器拓撲結構那樣普遍使用。
在我們的情況下,我們將使用 `@ClientCacheApplication`創建一個“客戶端”緩存實例,該實例具有連接到服務器集群并與之通信的能力。 但是,為簡單起見,客戶端只會使用 `LOCAL`客戶端區域,而無需設置或運行任何服務器。
現在,請記住您是如何標記的 `Person` 使用SDG映射注釋存儲在稱為“人”的區域中, `@Region("People")`? 您可以使用 `ClientRegionFactoryBean<String, Person>`Bean定義。 您需要注入剛剛定義的緩存實例,同時將其命名為“ *People* ”。
Apache Geode緩存實例(對等或客戶端)只是Regions的容器,用于存儲數據。 您可以將高速緩存視為RDBMS中的架構,將區域視為表。 但是,緩存還執行其他管理功能,以控制和管理您的所有區域。
類型是 <String, Person>,匹配密鑰類型( String)與值類型( Person).
這 `public static void main`方法使用 *Spring Boot的* `SpringApplication.run()` 啟動應用程序并調用 `ApplicationRunner`(另一個bean定義),它使用應用程序的 在Apache Geode上執行數據訪問操作 *Spring Data* *Repository* 。
該應用程序會自動關聯以下對象的實例 `PersonRepository`您剛剛定義的。 *用于Apache Geode的Spring Data* 將動態創建一個實現該接口的具體類,并插入所需的查詢代碼以滿足接口的義務。 該 *存儲庫* 實例是 `run()`演示功能的方法。
## 存儲和獲取數據
在本指南中,您將創建三個本地 `Person`對象, **愛麗絲(Alice)** , ( **小鮑勃 Baby Bob** )和 ( **青少年卡羅爾 Teen Carol)** 。 最初,它們僅存在于內存中。 創建它們之后,您必須將它們保存到Apache Geode。
現在,您運行幾個查詢。 第一個按名稱查找每個人。 然后,您將執行一些查詢,以使用age屬性查找成人,嬰兒和青少年。 打開日志記錄后,您可以看到 的查詢 *Spring Data for Apache Geode* 代表您編寫 。
改變 @ClientCacheApplication 注解 logLevel屬性設置為“ config ”,以查看由SDG生成的Apache Geode OQL查詢。 因為查詢方法(例如 findByName)加上SDG的注解 @Trace 注釋,這將打開Apache Geode的OQL查詢跟蹤(查詢級日志記錄),該查詢向您顯示生成的OQL,執行時間,查詢是否使用任何Apache Geode索引來收集結果以及該查詢返回的行數。詢問。
## 建立可執行的JAR
您可以使用Gradle或Maven從命令行運行該應用程序。 您還可以構建一個包含所有必需的依賴項,類和資源的可執行JAR文件,然后運行該文件。 生成可執行jar使得在整個開發生命周期中,跨不同環境等等的情況下,都可以輕松地將服務作為應用程序進行發布,版本控制和部署。
如果您使用Gradle,則可以通過使用以下命令運行該應用程序 `./gradlew bootRun`。 或者,您可以通過使用以下命令構建JAR文件: `./gradlew build` 然后運行JAR文件,如下所示:
~~~
java -jar build/libs/gs-accessing-data-gemfire-0.1.0.jar
~~~
如果您使用Maven,則可以通過使用以下命令運行該應用程序 `./mvnw spring-boot:run`。 或者,您可以使用以下命令構建JAR文件: `./mvnw clean package` 然后運行JAR文件,如下所示:
~~~
java -jar target/gs-accessing-data-gemfire-0.1.0.jar
~~~
此處描述的步驟將創建可運行的JAR。 您還可以 構建經典的WAR文件 。
您應該看到類似以下的內容(以及其他類似查詢的內容):
~~~
Before linking up with {apache-geode-name}...
Alice is 40 years old.
Baby Bob is 1 years old.
Teen Carol is 13 years old.
Lookup each person by name...
Alice is 40 years old.
Baby Bob is 1 years old.
Teen Carol is 13 years old.
Adults (over 18):
Alice is 40 years old.
Babies (less than 5):
Baby Bob is 1 years old.
Teens (between 12 and 20):
Teen Carol is 13 years old.
~~~
## 概括
恭喜你! 您設置了一個Apache Geode緩存客戶端,存儲了簡單的實體,并開發了快速查詢。
- springboot概述
- springboot構建restful服務
- spring構建一個RESTful Web服務
- spring定時任務
- 消費RESTful Web服務
- gradle構建項目
- maven構建項目
- springboot使用jdbc
- springboot應用上傳文件
- 使用LDNA驗證用戶
- 使用 spring data redis
- 使用 spring RabbitTemplate消息隊列
- 用no4j訪問nosql數據庫
- springboot驗證web表單
- Spring Boot Actuator構j建服務
- 使用jms傳遞消息
- springboot創建批處理服務
- spring security保護web 安全
- 在Pivotal GemFire中訪問數據
- 使用Spring Integration
- 使用springboot jpa進行數據庫操作
- 數據庫事務操作
- 操作mongodb
- springmvc+tymleaf創建web應用
- 將Spring Boot JAR應用程序轉換為WAR
- 創建異步服務
- spring提交表單
- 使用WebSocket構建交互式Web應用程序
- 使用REST訪問Neo4j數據
- jquery消費restful
- springboot跨域請求
- 消費SOAP Web服務
- springboot使用緩存
- 使用Vaadin創建CRUD UI
- 使用REST訪問JPA數據
- 使用REST訪問Pivotal GemFire中的數據
- 構建soap服務
- 使用rest訪問mongodb數據
- 構建springboot應用docker鏡像
- 從STS部署到Cloud Foundry
- springboot測試web應用
- springboot訪問mysql
- springboot編寫自定義模塊并使用
- 使用Google Cloud Pub / Sub進行消息傳遞
- 構建反應式RESTful Web服務
- 使用Redis主動訪問數據
- Spring Boot 部署到Kubernetes
- 使用反應式協議R2DBC訪問數據
- Spring Security架構
- spring構建Docker鏡像詳解
- Spring Boot和OAuth2
- springboot應用部署到k8s
- spring構建rest服務詳解