Giter Site home page Giter Site logo

spring-attic / gs-accessing-data-gemfire Goto Github PK

View Code? Open in Web Editor NEW
9.0 25.0 25.0 538 KB

Accessing Data in Pivotal GemFire :: Learn how to build an application using Gemfire's data fabric.

Home Page: https://spring.io/guides/gs/accessing-data-gemfire/

License: Apache License 2.0

Java 90.15% Shell 9.85%

gs-accessing-data-gemfire's Introduction

This repository is no longer maintained.

This guide walks you through the process of building an application the Apache Geode data management system.

What You Will build

You will use Spring Data for Apache Geode to store and retrieve POJOs.

Starting with Spring Initializr

For all Spring applications, you should start with the Spring Initializr. Spring Initializr offers 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 this pre-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:

  1. In a web browser, navigate to https://start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you.

  2. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.

  3. Click Dependencies and select Spring for Apache Geode.

  4. Click Generate.

  5. 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.

Define a Simple Entity

Apache Geode is an In-Memory Data Grid (IMDG) that maps data to regions. You can configure distributed regions that partition and replicate data across multiple nodes in a cluster. However, in this guide, we use a LOCAL 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 the java.util.concurrent.ConcurrentMap interface. Though you can treat a region as a java.util.Map, it is quite a bit more sophisticated than just a simple Java Map, given that data is distributed, replicated, and generally managed inside the region.

In this example, you store Person 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 a Person class with two fields: name and age. You also have a single persistent constructor to populate the entities when creating a new instance. The class uses Project Lombok to 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 the People region. This class also marks the name field with @Id. This signifies the identifier used to identify and track the Person data inside Apache Geode. Essentially, the @Id annotated field (such as name) is the key, and the Person instance is the value in the key/value entry. There is no automated key generation in Apache Geode, so you must set the ID (the name) 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 overridden toString() method prints out the person’s name and age.

Create Simple Queries

Spring Data for Apache Geode focuses on storing and accessing data in Apache Geode using Spring. It also inherits powerful functionality from the Spring Data Commons project, such as the ability to derive queries. Essentially, you need 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 queries Person objects stored in Apache Geode:

src/main/java/hello/PersonRepository.java

link:complete/src/main/java/hello/PersonRepository.java[role=include]

PersonRepository extends the CrudRepository interface from Spring Data Commons and specifies types for the generic type parameters for both the value and the ID (key) with which the Repository works (Person and String, respectively). This interface comes with many operations, including basic CRUD (create, read, update, delete) and simple query data access operations (such a findById(..)).

You can define other queries as needed by declaring their method signature. In this case, we add findByName, which essentially searches for objects of type Person and finds one that matches on name.

You also have:

  • findByAgeGreaterThan: To find people above a certain age

  • findByAgeLessThan: To find people below a certain age

  • findByAgeGreaterThanAndAgeLessThan: To find people in a certain age range

Let’s wire this up and see what it looks like!

Create an Application Class

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 one of Spring Data’s repository interfaces. You can use its basePackageClasses = MyRepository.class to safely tell Spring Data for Apache Geode to scan a different root package by type for application-specific Repository extensions.

A Apache Geode cache containing one or more regions is required to store all the data. For that, you use one of 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 participate in a cluster as a peer cache member. However, your application is subject to all the constraints of being a peer member in 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 to connect to and communicate with a cluster of servers. However, to keep things simple, the client stores data locally by using a LOCAL client region, without the need to setup or run any servers.

Now, remember how you tagged Person to be stored in a region called People by using the SDG mapping annotation, @Region("People")? You define that region here by using the ClientRegionFactoryBean<String, Person> bean definition. You need to inject an instance of the cache you just defined while also naming it People.

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 other administrative functions to control and manage all your Regions.
Note
The types are <String, Person>, matching the key type (String) with the value type (Person).

The public static void main method uses Spring Boot’s SpringApplication.run() to launch the application and invoke the ApplicationRunner (another bean definition) that performs the data access operations on Apache Geode using the application’s Spring Data repository.

The application autowires an instance of PersonRepository that you just defined. Spring Data for Apache Geode dynamically creates a concrete class that implements this interface and plugs in the needed query code to meet the interface’s obligations. This repository instance is used by the run() method to demonstrate the functionality.

Store and fetch data

In this guide, you create three local Person objects: Alice, Baby Bob, and Teen 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 queries Spring Data for Apache Geode writes on your behalf.

Tip
To see the Apache Geode OQL queries that are generated by SDG, change the @ClientCacheApplication annotation logLevel attribute to config. Because the query methods (such as findByName) 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 rows returned 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.

Summary

Congratulations! You set up an Apache Geode cache client, stored simple entities, and developed quick queries.

gs-accessing-data-gemfire's People

Contributors

bclozel avatar btalbott avatar buzzardo avatar cbeams avatar gregturn avatar habuma avatar jxblum avatar robertmcnees avatar royclarkson avatar spring-operator avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gs-accessing-data-gemfire's Issues

Confused about the Pivotal GemFire cluster this example connects to?

Hi,

I this example, I haven't found any locator, query server start up. I see it says, the region is "LOCAL"?
Local here means the region type of gemfire?
How to understand this local?
I think pivot gemfire is a commerical software, people pay for it, but in this example, seems there an embedded one in spring-data-jap?

Please help. Thanks.

Errors running the application

I tried with both my version and the 'complete' version and got this:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
ERROR SpringApplication Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at hello.Application.main(Application.java:25)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:203)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153)
... 16 more

No idea why this happened.

Connect local gemfire locator

Hi,I use gfsh create locator,Can you tell me how using spring-data-gemfire connect ?
I found in google search,but most example is using built-in gemfire。

SpringApplication Application run failed

I got this error.

ERROR SpringApplication Application run failed
 org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
	at hello.Application.main(Application.java:25)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:203)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153)
	... 8 more

And after I added this dependency [spring-boot-starter-web], it works well.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

Maven build/config issue

Downloaded project through STS "Importing getting started content" and get error:

Missing artifact com.gemstone.gemfire:gemfire:jar:7.0.2

Also tried cloning repo with Git, same error. Was not able to resolve this artifact with:

com.gemstone.gemfire
gemfire
7.0.2

or find the artifact (any version) in nexus

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.