BigMemory Max 4.3.4 | Code Samples | Example: Search
 
Example: Search
Note: The following description focuses on code snippets from the full code example, which is available at the /code-samples/src/ location in the installed product kit.
BigMemory Max comes with powerful in-memory search capabilities. This sample shows how to perform basic search operations on your in-memory data using the Search API. You can also issue queries using BigMemory SQL, either from the command line or from the Terracotta Management Console (TMC). This SQL-like language is documented in Searching with BigMemory SQL.
First, create a cache with searchable attributes:
Configuration managerConfig = new Configuration()
.cache(new CacheConfiguration().name("MySearchableDataStore")
.eternal(true)
.maxBytesLocalHeap(512, MemoryUnit.MEGABYTES)
.maxBytesLocalOffHeap(32, MemoryUnit.GIGABYTES)
.searchable(new Searchable()
.searchAttribute(new SearchAttribute().name("age"))
.searchAttribute(new SearchAttribute().name("gender")
.expression("value.getGender()"))
.searchAttribute(new SearchAttribute().name("state")
.expression("value.getAddress().getState()"))
.searchAttribute(new SearchAttribute().name("name")
.className(NameAttributeExtractor.class.getName()))
)
);
CacheManager manager = CacheManager.create(managerConfig);
Ehcache bigMemory = manager.getEhcache("MySearchableDataStore");
Next, insert data:
bigMemory.put(new Element(1, new Person("Jane Doe", 35, Gender.FEMALE,
"eck street", "San Mateo", "CA")));
bigMemory.put(new Element(2, new Person("Marie Antoinette", 23, Gender.FEMALE,
"berry st", "Parsippany", "LA")));
bigMemory.put(new Element(3, new Person("John Smith", 25, Gender.MALE,
"big wig", "Beverly Hills", "NJ")));
bigMemory.put(new Element(4, new Person("Paul Dupont", 45, Gender.MALE,
"cool agent", "Madison", "WI")));
bigMemory.put(new Element(5, new Person("Juliet Capulet", 30, Gender.FEMALE,
"dah man", "Bangladesh", "MN")));
for (int i = 6; i < 1000; i++) {
bigMemory.put(new Element(i, new Person("Juliet Capulet" + i, 30,
Person.Gender.MALE, "dah man", "Bangladesh", "NJ")));
}
Create some search attributes and construct a query:
Attribute<Integer> age = bigMemory.getSearchAttribute("age");
Attribute<Gender> gender = bigMemory.getSearchAttribute("gender");
Attribute<String> name = bigMemory.getSearchAttribute("name");
Attribute<String> state = bigMemory.getSearchAttribute("state");
Query query = bigMemory.createQuery();
query.includeKeys();
query.includeValues();
query.addCriteria(name.ilike("Jul*").and(gender.eq(Gender.FEMALE)))
.addOrderBy(age, Direction.ASCENDING).maxResults(10);
Execute the query and look at the results:
Results results = query.execute();
System.out.println(" Size: " + results.size());
System.out.println("----Results-----\n");
for (Result result : results.all()) {
System.out.println("Maxt: Key[" + result.getKey()
+ "] Value class [" + result.getValue().getClass()
+ "] Value [" + result.getValue() + "]");
}
You can use Aggregators to perform computations across query results. Here is an example that computes the average age of all people in the data set:
Query averageAgeQuery = bigMemory.createQuery();
averageAgeQuery.includeAggregator(Aggregators.average(age));
System.out.println("Average age: "
+ averageAgeQuery.execute().all().iterator().next()
.getAggregatorResults());
You can also restrict the calculation to a subset based on search attributes. Here is an example that computes the average age of all people in the data set between the ages of 30 and 40:
Query agesBetween = bigMemory.createQuery();
agesBetween.addCriteria(age.between(30, 40));
agesBetween.includeAggregator(Aggregators.average(age));
System.out.println("Average age between 30 and 40: "
+ agesBetween.execute().all().iterator().next()
.getAggregatorResults());
Using Aggregators, you can also find number of entries that match your search criteria. Here is an example that finds the number of people in the data set who live in New Jersey:
Query newJerseyCountQuery = bigMemory.createQuery().addCriteria(
state.eq("NJ"));
newJerseyCountQuery.includeAggregator(Aggregators.count());
System.out.println("Count of people from NJ: "
+ newJerseyCountQuery.execute().all().iterator().next()
.getAggregatorResults());

Copyright © 2010 - 2019 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.