用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
# 在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中的架构,将区域视为表。 但是,缓存还执行其他管理功能,以控制和管理您的所有区域。 类型是 &lt;String, Person&gt;,匹配密钥类型( 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缓存客户端,存储了简单的实体,并开发了快速查询。