- Notifications
You must be signed in to change notification settings - Fork25
Accessing Data in Pivotal GemFire :: Learn how to build an application using Gemfire's data fabric.
License
spring-attic/gs-accessing-data-gemfire
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This guide walks you through the process of building an application theApache Geodedata management system.
You will useSpring Data for Apache Geodeto store and retrieve POJOs.
For all Spring applications, you should start with theSpring Initializr. Spring Initializroffers a fast way to pull in all the dependencies you need for an application and does a lot of the set up for you.This example needs Spring for Apache Geode dependency.
You can use thispre-initialized project and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.
To manually initialize the project:
In a web browser, navigate tohttps://start.spring.io.This service pulls in all the dependencies you need for an application and does most of the setup for you.
Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.
ClickDependencies and selectSpring for Apache Geode.
ClickGenerate.
Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.
Note | If your IDE has the Spring Initializr integration, you can complete this process from your IDE. |
Note | You can also fork the project from Github and open it in your IDE or other editor. |
Apache Geode is an In-Memory Data Grid (IMDG) that maps data to regions. You can configuredistributed regions that partition and replicate data across multiple nodes in a cluster. However, in this guide,we use aLOCAL
region so that you need not set up anything extra, such as an entire cluster of servers.
Apache Geode is a key/value store, and a region implements thejava.util.concurrent.ConcurrentMap
interface. Thoughyou can treat a region as ajava.util.Map
, it is quite a bit more sophisticated than just a simple JavaMap
,given that data is distributed, replicated, and generally managed inside the region.
In this example, you storePerson
objects in Apache Geode (a region) by using only a few annotations.
src/main/java/hello/Person.java
link:complete/src/main/java/hello/Person.java[role=include]
Here you have aPerson
class with two fields:name
andage
. You also have a single persistent constructor topopulate the entities when creating a new instance. The class usesProject Lombokto simplify the implementation.
Notice that this class is annotated with@Region("People")
. When Apache Geode stores an instance of this class,a new entry is created inside thePeople
region. This class also marks thename
field with@Id
. This signifiesthe identifier used to identify and track thePerson
data inside Apache Geode. Essentially, the@Id
annotatedfield (such asname
) is the key, and thePerson
instance is the value in the key/value entry. There is no automatedkey generation in Apache Geode, so you must set the ID (thename
) prior to persisting the entity to Apache Geode.
The next important piece is the person’s age. Later in this guide, we use it to fashion some queries.
The overriddentoString()
method prints out the person’s name and age.
Spring Data for Apache Geode focuses on storing and accessing data in Apache Geode using Spring. It also inheritspowerful functionality from the Spring Data Commons project, such as the ability to derive queries. Essentially, youneed not learn the query language of Apache Geode (OQL). You can write a handful of methods,and the framework writes the queries for you.
To see how this works, create an interface that queriesPerson
objects stored in Apache Geode:
src/main/java/hello/PersonRepository.java
link:complete/src/main/java/hello/PersonRepository.java[role=include]
PersonRepository
extends theCrudRepository
interface from Spring Data Commons and specifies types forthe generic type parameters for both the value and the ID (key) with which theRepository works (Person
andString
, respectively). This interface comes with many operations, including basic CRUD(create, read, update, delete) and simple query data access operations (such afindById(..)
).
You can define other queries as needed by declaring their method signature. In this case, we addfindByName
,which essentially searches for objects of typePerson
and finds one that matches onname
.
You also have:
findByAgeGreaterThan
: To find people above a certain agefindByAgeLessThan
: To find people below a certain agefindByAgeGreaterThanAndAgeLessThan
: To find people in a certain age range
Let’s wire this up and see what it looks like!
The following example creates an application class with all the components:
src/main/java/hello/Application.java
link:complete/src/main/java/hello/Application.java[role=include]
In the configuration, you need to add the@EnableGemfireRepositories
annotation.
By default,
@EnableGemfireRepositories
scans the current package for any interfaces that extend oneof Spring Data’s repository interfaces. You can use itsbasePackageClasses = MyRepository.class
to safely tellSpring Data for Apache Geode to scan a different root package by type for application-specificRepositoryextensions.
A Apache Geode cache containing one or more regions is required to store all the data. For that, you use oneof Spring Data for Apache Geode’s convenient configuration-based annotations:@ClientCacheApplication
,@PeerCacheApplication
, or@CacheServerApplication
.
Apache Geode supports different cache topologies, such as client/server, peer-to-peer (p2p), and even WAN arrangements.In p2p, a peer cache instance is embedded in the application, and your application would have the ability to participatein a cluster as a peer cache member. However, your application is subject to all the constraints of being a peer memberin the cluster, so this is not as commonly used as, say, the client/server topology.
In our case, we use@ClientCacheApplication
to create a “client” cache instance, which has the ability toconnect to and communicate with a cluster of servers. However, to keep things simple, the client storesdata locally by using aLOCAL
client region, without the need to setup or run any servers.
Now, remember how you taggedPerson
to be stored in a region calledPeople
by using the SDG mapping annotation,@Region("People")
? You define that region here by using theClientRegionFactoryBean<String, Person>
bean definition.You need to inject an instance of the cache you just defined while also naming itPeople
.
Note | A Apache Geode cache instance (whether a peer or client) is just a container for regions, which store your data.You can think of the cache as a schema in an RDBMS and regions as the tables. However, a cache also performs otheradministrative functions to control and manage all your Regions. |
Note | The types are<String, Person> , matching the key type (String ) with the value type (Person ). |
Thepublic static void main
method uses Spring Boot’sSpringApplication.run()
to launch the applicationand invoke theApplicationRunner
(another bean definition) that performs the data access operations on Apache Geodeusing the application’s Spring Data repository.
The application autowires an instance ofPersonRepository
that you just defined. Spring Data for Apache Geodedynamically creates a concrete class that implements this interface and plugs in the needed query code to meetthe interface’s obligations. This repository instance is used by therun()
method to demonstratethe functionality.
In this guide, you create three localPerson
objects:Alice,Baby Bob, andTeen Carol. Initially,they only exist in memory. After creating them, you have to save them to Apache Geode.
Now you can run several queries. The first looks up everyone by name. Then you can run a handful of queries to find adults,babies, and teens, all by using the age attribute. With logging turned on, you can see the queriesSpring Data for Apache Geode writes on your behalf.
Tip | To see the Apache Geode OQL queries that are generated by SDG, change the@ClientCacheApplication annotationlogLevel attribute toconfig .Because the query methods (such asfindByName ) are annotated with SDG’s@Trace annotation, this turns on Apache Geode’s OQL query tracing (query-level logging), which shows you the generated OQL,execution time, whether any Apache Geode indexes were used by the query to gather the results, and the number of rowsreturned by the query. |
You should see something like this (with other content, such as queries):
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.
Congratulations! You set up an Apache Geode cache client, stored simple entities, and developed quick queries.
About
Accessing Data in Pivotal GemFire :: Learn how to build an application using Gemfire's data fabric.
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors10
Uh oh!
There was an error while loading.Please reload this page.