BAEL-1466 Added a new module for apache-geode

This commit is contained in:
Sandip Singh 2018-08-14 11:29:29 +05:30
parent 2e2406d1dc
commit f0d3a4ed4a
5 changed files with 374 additions and 0 deletions

46
apache-geode/pom.xml Normal file
View File

@ -0,0 +1,46 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>apache-geode</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<geode.core>1.6.0</geode.core>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.geode</groupId>
<artifactId>geode-core</artifactId>
<version>${geode.core}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,78 @@
package com.baeldung.geode;
import java.io.Serializable;
import java.util.Objects;
public class Customer implements Serializable {
private static final long serialVersionUID = -7482516011038799900L;
private CustomerKey key;
private String firstName;
private String lastName;
private Integer age;
public Customer() {
}
public Customer(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public Customer(CustomerKey key, String firstName, String lastName, int age) {
this(firstName, lastName, age);
this.key = key;
}
// setters and getters
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Customer{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", age=" + age + '}';
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Customer customer = (Customer) o;
return Objects.equals(firstName, customer.firstName) && Objects.equals(lastName, customer.lastName) && Objects.equals(age, customer.age);
}
@Override
public int hashCode() {
return Objects.hash(firstName, lastName, age);
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.geode;
import java.io.Serializable;
public class CustomerKey implements Serializable {
private static final long serialVersionUID = -3529253035303792458L;
private long id;
private String country;
public CustomerKey(long id) {
this.id = id;
this.country = "USA";
}
public CustomerKey(long id, String country) {
this.id = id;
this.country = country;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
CustomerKey that = (CustomerKey) o;
if (id != that.id)
return false;
return country != null ? country.equals(that.country) : that.country == null;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + (country != null ? country.hashCode() : 0);
return result;
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.geode.functions;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.execute.Function;
import org.apache.geode.cache.execute.FunctionContext;
import org.apache.geode.cache.execute.RegionFunctionContext;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class PrimeNumber implements Function {
public static final String ID = PrimeNumber.class.getSimpleName();
@Override
public void execute(FunctionContext context) {
RegionFunctionContext regionContext = (RegionFunctionContext) context;
Region<Integer, String> region = regionContext.getDataSet();
List<Integer> primes = new ArrayList<>();
Set<Integer> keys = region.keySet();
for (Integer key : keys) {
if (isPrime(key)) {
primes.add(key);
}
}
Collections.sort(primes);
context.getResultSender()
.lastResult(primes);
}
@Override
public String getId() {
return ID;
}
private boolean isPrime(int number) {
int limit = (int) Math.floor(Math.sqrt(number));
for (int divisor = 2; divisor <= limit; ++divisor) {
if (number % divisor == 0) {
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,144 @@
package com.baeldung.geode;
import com.baeldung.geode.functions.PrimeNumber;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.ClientCacheFactory;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.apache.geode.cache.execute.Execution;
import org.apache.geode.cache.execute.FunctionService;
import org.apache.geode.cache.execute.ResultCollector;
import org.apache.geode.cache.query.*;
import org.junit.Test;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class GeodeSamplesIntegrationTest {
@Test
public void whenSendMessageToRegion_thenMessageSavedSuccessfully() {
ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334)
.create();
Region<String, String> region = cache.<String, String> createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
.create("baeldung");
region.put("1", "Hello");
region.put("2", "Baeldung");
assertEquals("Hello", region.get("1"));
assertEquals("Baeldung", region.get("2"));
cache.close();
}
@Test
public void whenPutMultipleValuesAtOnce_thenValuesSavedSuccessfully() {
ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334)
.create();
Region<String, String> region = cache.<String, String> createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
.create("baeldung");
Map<String, String> values = IntStream.rangeClosed(1, 5)
.mapToObj(String::valueOf)
.collect(Collectors.toMap(Function.identity(), i -> "value" + i));
region.putAll(values);
IntStream.rangeClosed(1, 5)
.mapToObj(String::valueOf)
.forEach(e -> {
assertEquals("value".concat(e), region.get(e));
});
cache.close();
}
@Test
public void whenSaveCustomerDataOnPartitionedRegion_thenDataSavedCorrectly() {
ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334)
.create();
Region<Integer, Customer> region = cache.<Integer, Customer> createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
.create("baeldung-partition");
Customer customer1 = new Customer(new CustomerKey(1l), "Gheorge", "Manuc", 36);
Customer customer2 = new Customer(new CustomerKey(2l), "Allan", "McDowell", 43);
Customer customer3 = new Customer(new CustomerKey(3l), "Alan", "McClean", 23);
Customer customer4 = new Customer(new CustomerKey(4l), "Allan", "Donald", 46);
Map<Integer, Customer> customerData = new HashMap<>();
customerData.put(1, customer1);
customerData.put(2, customer2);
customerData.put(3, customer3);
customerData.put(4, customer4);
region.putAll(customerData);
// assert the size on the cache server.
assertEquals(4, region.sizeOnServer());
cache.close();
}
@Test
public void whenFindACustomerUsingOQL_thenCorrectCustomerObject() throws NameResolutionException, TypeMismatchException, QueryInvocationTargetException, FunctionDomainException {
ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334)
.create();
Region<Integer, Customer> region = cache.<Integer, Customer> createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
.create("baeldung-oql");
Customer customer1 = new Customer("Gheorge", "Manuc", 36);
Customer customer2 = new Customer("Allan", "McDowell", 43);
Customer customer3 = new Customer("Alan", "McClean", 23);
Customer customer4 = new Customer("Allan", "Donald", 46);
Map<Integer, Customer> customerData = new HashMap<>();
customerData.put(1, customer1);
customerData.put(2, customer2);
customerData.put(3, customer3);
customerData.put(4, customer4);
region.putAll(customerData);
// assert the size on the cache server.
assertEquals(4, region.sizeOnServer());
QueryService queryService = cache.getQueryService();
String query = "select * from /baeldung-oql c where c.firstName = 'Allan'";
SelectResults<Customer> queryResults = (SelectResults<Customer>) queryService.newQuery(query)
.execute();
assertEquals(2, queryResults.size());
cache.close();
}
@Test
public void whenExecutePrimeNumberFunction_thenReturnOnlyPrimeNumbers() {
// connect to the locator using default port 10334
ClientCache cache = new ClientCacheFactory().addPoolLocator("localhost", 10334)
.create();
// create a local region that matches the server region
Region<Integer, String> region = cache.<Integer, String> createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
.create("baeldung-function");
Execution execution = FunctionService.onRegion(region);
IntStream.rangeClosed(1, 5)
.forEach(i -> region.put(i, String.valueOf(i)));
ResultCollector<Integer, List> results = execution.execute(PrimeNumber.ID);
Set<Integer> primes = new HashSet<>();
List resultList = results.getResult();
assertNotNull(resultList);
assertEquals(1, resultList.size());
primes.addAll((List<? extends Integer>) resultList.iterator()
.next());
assertEquals(4, primes.size());
cache.close();
}
}