Skip to content

Commit 037c3f4

Browse files
sandy03934jzheaux
authored andcommitted
apache-geode code samples
Issue: BAEL-1466
1 parent b343e82 commit 037c3f4

5 files changed

Lines changed: 325 additions & 0 deletions

File tree

apache-geode/pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>apache-geode</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<properties>
18+
<geode.core>1.6.0</geode.core>
19+
</properties>
20+
<build>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.apache.maven.plugins</groupId>
24+
<artifactId>maven-compiler-plugin</artifactId>
25+
<configuration>
26+
<source>1.8</source>
27+
<target>1.8</target>
28+
</configuration>
29+
</plugin>
30+
</plugins>
31+
</build>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.apache.geode</groupId>
36+
<artifactId>geode-core</artifactId>
37+
<version>${geode.core}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<version>RELEASE</version>
43+
</dependency>
44+
</dependencies>
45+
46+
</project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.geode;
2+
3+
import java.io.Serializable;
4+
import java.util.Objects;
5+
6+
public class Customer implements Serializable {
7+
8+
private static final long serialVersionUID = -7482516011038799900L;
9+
10+
private CustomerKey key;
11+
private String firstName;
12+
private String lastName;
13+
private Integer age;
14+
15+
public Customer() {
16+
}
17+
18+
public Customer(String firstName, String lastName, int age) {
19+
this.firstName = firstName;
20+
this.lastName = lastName;
21+
this.age = age;
22+
}
23+
24+
public Customer(CustomerKey key, String firstName, String lastName, int age) {
25+
this(firstName, lastName, age);
26+
this.key = key;
27+
}
28+
29+
// setters and getters
30+
31+
public static long getSerialVersionUID() {
32+
return serialVersionUID;
33+
}
34+
35+
public String getFirstName() {
36+
return firstName;
37+
}
38+
39+
public void setFirstName(String firstName) {
40+
this.firstName = firstName;
41+
}
42+
43+
public String getLastName() {
44+
return lastName;
45+
}
46+
47+
public void setLastName(String lastName) {
48+
this.lastName = lastName;
49+
}
50+
51+
public Integer getAge() {
52+
return age;
53+
}
54+
55+
public void setAge(Integer age) {
56+
this.age = age;
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return "Customer{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", age=" + age + '}';
62+
}
63+
64+
@Override
65+
public boolean equals(Object o) {
66+
if (this == o)
67+
return true;
68+
if (o == null || getClass() != o.getClass())
69+
return false;
70+
Customer customer = (Customer) o;
71+
return Objects.equals(firstName, customer.firstName) && Objects.equals(lastName, customer.lastName) && Objects.equals(age, customer.age);
72+
}
73+
74+
@Override
75+
public int hashCode() {
76+
return Objects.hash(firstName, lastName, age);
77+
}
78+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.geode;
2+
3+
import java.io.Serializable;
4+
5+
public class CustomerKey implements Serializable {
6+
7+
private static final long serialVersionUID = -3529253035303792458L;
8+
private long id;
9+
private String country;
10+
11+
public CustomerKey(long id) {
12+
this.id = id;
13+
this.country = "USA";
14+
}
15+
16+
public CustomerKey(long id, String country) {
17+
this.id = id;
18+
this.country = country;
19+
}
20+
21+
public long getId() {
22+
return id;
23+
}
24+
25+
public void setId(long id) {
26+
this.id = id;
27+
}
28+
29+
public String getCountry() {
30+
return country;
31+
}
32+
33+
public void setCountry(String country) {
34+
this.country = country;
35+
}
36+
37+
@Override
38+
public boolean equals(Object o) {
39+
if (this == o)
40+
return true;
41+
if (o == null || getClass() != o.getClass())
42+
return false;
43+
44+
CustomerKey that = (CustomerKey) o;
45+
46+
if (id != that.id)
47+
return false;
48+
return country != null ? country.equals(that.country) : that.country == null;
49+
}
50+
51+
@Override
52+
public int hashCode() {
53+
int result = (int) (id ^ (id >>> 32));
54+
result = 31 * result + (country != null ? country.hashCode() : 0);
55+
return result;
56+
}
57+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.geode.functions;
2+
3+
import com.baeldung.geode.Customer;
4+
import com.baeldung.geode.CustomerKey;
5+
import org.apache.geode.cache.Region;
6+
import org.apache.geode.cache.execute.Function;
7+
import org.apache.geode.cache.execute.FunctionContext;
8+
import org.apache.geode.cache.execute.RegionFunctionContext;
9+
10+
import java.util.Map;
11+
12+
public class UpperCaseNames implements Function<Boolean> {
13+
private static final long serialVersionUID = -8946294032165677602L;
14+
15+
@Override
16+
public void execute(FunctionContext<Boolean> context) {
17+
RegionFunctionContext regionContext = (RegionFunctionContext) context;
18+
Region<CustomerKey, Customer> region = regionContext.getDataSet();
19+
20+
for (Map.Entry<CustomerKey, Customer> entry : region.entrySet()) {
21+
Customer customer = entry.getValue();
22+
customer.setFirstName(customer.getFirstName()
23+
.toUpperCase());
24+
}
25+
26+
context.getResultSender()
27+
.lastResult(true);
28+
}
29+
30+
@Override
31+
public String getId() {
32+
return getClass().getName();
33+
}
34+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.baeldung.geode;
2+
3+
import com.baeldung.geode.functions.UpperCaseNames;
4+
import org.apache.geode.cache.Region;
5+
import org.apache.geode.cache.client.ClientCache;
6+
import org.apache.geode.cache.client.ClientCacheFactory;
7+
import org.apache.geode.cache.client.ClientRegionShortcut;
8+
import org.apache.geode.cache.execute.Execution;
9+
import org.apache.geode.cache.execute.FunctionService;
10+
import org.apache.geode.cache.query.*;
11+
import org.junit.After;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
import java.util.function.Function;
18+
import java.util.function.Supplier;
19+
import java.util.stream.Collectors;
20+
import java.util.stream.Stream;
21+
22+
import static org.junit.Assert.assertEquals;
23+
24+
public class GeodeSamplesIntegrationTest {
25+
26+
ClientCache cache = null;
27+
Region<String, String> region = null;
28+
Region<Integer, Customer> queryRegion = null;
29+
Region<CustomerKey, Customer> customerRegion = null;
30+
31+
@Before
32+
public void connect() {
33+
this.cache = new ClientCacheFactory().addPoolLocator("localhost", 10334)
34+
.create();
35+
this.region = this.cache.<String, String> createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
36+
.create("baeldung");
37+
this.customerRegion = this.cache.<CustomerKey, Customer> createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
38+
.create("baeldung-customers");
39+
}
40+
41+
@After
42+
public void cleanup() {
43+
this.cache.close();
44+
}
45+
46+
@Test
47+
public void whenSendMessageToRegion_thenMessageSavedSuccessfully() {
48+
49+
this.region.put("1", "Hello");
50+
this.region.put("2", "Baeldung");
51+
52+
assertEquals("Hello", region.get("1"));
53+
assertEquals("Baeldung", region.get("2"));
54+
55+
}
56+
57+
@Test
58+
public void whenPutMultipleValuesAtOnce_thenValuesSavedSuccessfully() {
59+
60+
Supplier<Stream<String>> keys = () -> Stream.of("A", "B", "C", "D", "E");
61+
Map<String, String> values = keys.get()
62+
.collect(Collectors.toMap(Function.identity(), String::toLowerCase));
63+
64+
this.region.putAll(values);
65+
66+
keys.get()
67+
.forEach(k -> assertEquals(k.toLowerCase(), this.region.get(k)));
68+
69+
}
70+
71+
@Test
72+
public void whenPutCustomKey_thenValuesSavedSuccessfully() {
73+
CustomerKey key = new CustomerKey(123);
74+
Customer customer = new Customer(key, "William", "Russell", 35);
75+
76+
Map<CustomerKey, Customer> customerInfo = new HashMap<>();
77+
customerInfo.put(key, customer);
78+
79+
this.customerRegion.putAll(customerInfo);
80+
81+
Customer storedCustomer = this.customerRegion.get(key);
82+
assertEquals("William", storedCustomer.getFirstName());
83+
assertEquals("Russell", storedCustomer.getLastName());
84+
85+
}
86+
87+
@Test
88+
public void whenFindACustomerUsingOQL_thenCorrectCustomerObject() throws NameResolutionException, TypeMismatchException, QueryInvocationTargetException, FunctionDomainException {
89+
90+
Map<CustomerKey, Customer> data = new HashMap<>();
91+
data.put(new CustomerKey(1), new Customer("Gheorge", "Manuc", 36));
92+
data.put(new CustomerKey(2), new Customer("Allan", "McDowell", 43));
93+
this.customerRegion.putAll(data);
94+
95+
QueryService queryService = this.cache.getQueryService();
96+
String query = "select * from /baeldung-customers c where c.firstName = 'Allan'";
97+
SelectResults<Customer> queryResults = (SelectResults<Customer>) queryService.newQuery(query)
98+
.execute();
99+
assertEquals(1, queryResults.size());
100+
101+
}
102+
103+
@Test
104+
public void whenExecuteUppercaseNames_thenCustomerNamesAreUppercased() {
105+
Execution execution = FunctionService.onRegion(this.customerRegion);
106+
execution.execute(UpperCaseNames.class.getName());
107+
Customer customer = this.customerRegion.get(new CustomerKey(1));
108+
assertEquals("GHEORGE", customer.getFirstName());
109+
}
110+
}

0 commit comments

Comments
 (0)