Crux Intelligence
Published in

Crux Intelligence

Querying Cache in Apache Ignite

Cache Queries
  • Predicate-based Scan Query
    A scan query is a simple search query used to retrieve data from a cache in a distributed manner. When executed without parameters, a scan query returns all entries from the cache. Scan queries return entries that match a predicate, if specified. The predicate is applied to the remote nodes.
  • ANSI 99-compliant SQL Query
    Apache Ignite SQL Grid is a distributed data grid where you can execute ANSI SQL-99-compliant SQLs (SELECT, UPDATE, INSERT, MERGE, and DELETE queries) to manipulate a cache.
  • Lucene Index-based Text Query
    Lucene is a full-text search library in Java which makes it easy to add search functionality to an application or website. Ignite supports full-text queries based on the Apache Lucene engine.
  • Continuous query
    Continuous queries allow registering a remote filter and a local listener for cache updates. Once the cache is updated and passes the filter criteria, then the cache update events will be sent to the node that executed the query, and the local listener will be notified. This helps us to take action in case the cache is updated on Ignite Cluster.
  1. Integrating Apache Ignite Server Node With Spring Boot
  2. Enabling Caching
  3. Query and QueryCursor
  4. Scan Queries
  5. SQL Queries
  6. Text Queries
  7. Testing the Application

Prerequisites

spring-boot-starter
spring-boot-starter-web
spring-boot-starter-test
<dependency>     
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.2</version>
</dependency>

1. Integrating Apache Ignite Server Node With Spring Boot

IgniteCacheManagerConfiguration
ignite-configuration.xml

2. Enabling Caching

ignite-cache-configuration.xml
Player-Cache

3. Query and QueryCursor

4. Scan Queries

ScanQuery
  • We started off by creating a public method called scanQuerySearch in line 1 with search text input, which we will use to search in Player cache.
  • In line 3, we retrieved an instance of the Player cache from Ignite node.
  • Then we created an instance of ScanQuery in line 4, which takes IgniteBiPredicate as an argument. IgniteBiPredicate is a functional interface that accepts two parameters and returns a boolean. It is usually used to filter a collection of objects and can be used in lambda expression also. Here, we are going to use a Java 8 lambda expression to represent IgniteBiPredicate. The k represents the key of the cache and v is the value of Player. Our IgniteBiPredicate returns True only if any player stored in the cache qualifies the expression player.getTeam() EQ input text. The result is returned as a QueryCursor. It stores all qualified entries (key-value pairs).
  • From line 7 to 14 we iterated through the results to locate if such an entry exists in the cache and then populated a local List of filtered Players.
  • In line 15 we are returning the filtered list of Players to the caller.

5. SQL Queries

SqlQuery
  • We started off by creating a public method called sqlQuerySearch in line 1 with search text input, which we will use to search in Player cache.
  • In line 3, we retrieved an instance of the Player cache from Ignite node.
  • Next, we extracted the table name from the cache in line 4 to 5. Once we have the table name, then we can form a SQL Query using the table name to search the cache.
  • We created an instance of SqlFieldsQuery in line 6.
    The SqlFieldsQuery class provides another implementation of Query API. It is used for executing SQL statements and navigating through the results. It accepts a standard SQL query as its constructor​ parameter and executes it. SqlFieldsQuery is executed through the IgniteCache.query(SqlFieldsQuery) method, which returns a QueryCursor.
  • From line 8 to 9 we iterated and extracted the results using getAll() method. If an entry exists in the cache, then we collect the results and return the response to the caller.

6. Text Queries

TextQuery
  • In line 1 we created a method called textSearch, which takes a text as an input and then searches that text in Player cache. It searches the fields in Player cache annotated with QueryTextField.
  • Then in line 3, we retrieved an instance of the Player cache from Ignite node.
  • We created an instance of TextQuery in line 4. Once we have an instance of TextQuery and IgniteCache we called the method searchPlayers to search the text in the cache.
  • From line 25 to 29 we iterated through the results to locate if such an entry exists in the cache and then populated a local List of filtered Players.
  • In line 30 we are returning the filtered list of Players to the caller.
  • Similar to textSearch we also created a method called fuzzySearch from line 8 to 13 to perform Fuzzy search on Player cache. Fuzzy search determines whether there is any similarity between elements of the data. Let’s say we want to fetch players where the team name is ‘Barcelona’ or ‘Barcenola’. Then we just need to add a ‘~’ at the end of our search string and pass it to the TextQuery constructor. We are going to find the players where the text ‘Barcenola’ is present, and the fuzzy search will find a match between nola and lona and return all ‘Barcelona’ players.
  • We can also perform Fuzzy search on specific fields. For this reason we added another method called fuzzySearchOnSpecificField from line 15 to 20 which takes fieldName and text as an input.
  • In the previous methods textSearch and fuzzySearch, during search operation Ignite looked at all the indexes, name and team, to find data. However, we can also ask Ignite to look into specific index. We can see that in line 18 . In this method we ask Ignite to look at a specific index. It will search the input text only in the field provided by user. For example, if we are interested in name = ‘Neymar’, then the TextQuery can be configured as name:”Neymar”.

7. Testing the Application

PopulateCache
PlayerController
Swagger-UI
SQLQuery
TextQuery
SsanQuery
Fuzzy-Search
TextQuery-with-field

Conclusion

References & Useful Readings

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store