DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

  1. DZone
  2. Refcards
  3. Getting Started with Infinispan
refcard cover
Refcard #115

Getting Started with Infinispan

Enhance Performance With Scalable, Highly Available Data Stores

Infinispan is an open-source, ASL 2.0-licensed, in-memory data grid platform based on Java 8. This newly updated Refcard offers tips for implementing Infinispan, gives a practical example for using it in embedded mode, and lists key APIs and cache features. Learn more about running Infinispan in containers and how to integrate the platform with Hibernate ORM, Apache Hadoop, Apache Spark, and Apache Camel.

Download Refcard
Free PDF for Easy Reference
refcard cover

Written By

author avatar Tristan Tarrant
Project Lead, Infinispan, Red Hat
author avatar Manik Surtani
Chief architect, JBoss
Table of Contents
► What Is Infinispan ► How Can I Get It? ► Embedded Mode ► A Practical Example ► Key APIs ► Cache Features ► Caching Modes ► Caches and the Streams API ► JCache (JSR-107) ► Client/server mode ► Containers and Cloud environments ► Integrations ► Additional resources
Section 1

What Is Infinispan

Infinispan is an open-source, ASL 2.0-licensed, in-memory data grid platform. In-memory data grids are commonly used as low-latency, highly available and elastic data storage backends, often as NoSQL solutions. A common use is in addition to traditional databases, as a distributed cache for fast data access.

Infinispan can be used either embedded in your application, or remotely, via an efficient high-performance protocol known as Hot Rod, or over more ubiquitous protocols like Memcached. Your choice on which mode of interaction to use will depend on a number of factors, including whether you are using Infinispan as a clustering toolkit to cluster your own framework, whether you intend to use Infinispan to cache database lookups, or whether you plan to interact with Infinispan from a non-JVM environment. You can interact with Infinispan directly through its rich API, or use one of several integrations such as JCache, Spring Cache, and CDI.

Section 2

How Can I Get It?

Depending on how you intend to use Infinispan, there are different ways you can download it. If you are embedding Infinispan in your application, the most convenient way is to use Maven.

Infinispan is available from Maven Central at the following coordinates:

1
<dependency>
2
  <groupId>org.infinispan</groupId>
3
  <artifactId>infinispan-core</artifactId>
4
  <version>LATEST_INFINISPAN_VERSION</version>
5
</dependency>

If instead you prefer to manage your project dependencies by yourself, or you want to use Infinispan Server, head over to http://infinispan.org/download/ to download your preferred distribution.

Section 3

Embedded Mode

When used in this mode, Infinispan instances reside within your JVM alongside your code. You start up multiple instances of your application on different servers and each one starts and initializes an Infinispan instance. These Infinispan instances discover each other, form a data grid, and start sharing and distributing data. This mode is what you will want to use if you want an in-memory cache (either local or clustered) to front a database or some other expensive data source or if you are building an application or a framework that needs to be cluster-aware.

Section 4

A Practical Example

To see this in action, make sure the Infinispan libraries are available to your code—either via Maven, as above, or via downloading the zipped distribution and extracting the JAR files.

Starting Infinispan instances from your code is simple. To start a local, non-clustered cache instance:

3
1
DefaultCacheManager m = new DefaultCacheManager();
2
Cache<String, String> c = m.getCache();
3
c.put("hello", "world");

Infinispan’s core construct is an instance of the Cache interface. Extending java.util.concurrent.ConcurrentMap, Cache exposes well-known methods to store and retrieve objects.

To start a cluster-aware instance capable of detecting neighboring instances on the same local area network and sharing state between them:

9
1
GlobalConfigurationBuilder global = GlobalConfigurationBuilder.getClusteredDefault();
2
ConfigurationBuilder config = new ConfigurationBuilder();
3
config.clustering().cacheMode(CacheMode.DIST_SYNC);
4
DefaultCacheManager m = new DefaultCacheManager(global.build(), config.build());
5
Cache<String, String> cache = m.getCache();
6
cache.put("hello", "world");
7
cache.containsKey("hello"); // returns true
8
cache.get("hello"); // returns "world"
9
cache.remove("hello"); // returns "world"

This can also be done using a configuration file on the classpath:

2
1
DefaultCacheManager m = new DefaultCacheManager("path/to/my/infinispan_config.xml");
2
Cache<String, String> c = m.getCache();

Where the configuration file “infinispan_config.xml” contains:

6
1
<infinispan>
2
  <cache-container default-cache="default">
3
  <transport cluster="mycluster"/>
4
  <distributed-cache name="default" mode="SYNC"/>
5
</cache-container>
6
</infinispan>

Important: Infinispan’s clustering is implemented on top of the powerful JGroups library, which supports a large number of discovery protocols, depending on your environment. Check it out at http://jgroups.org.

Section 5

Key APIs

org.infinispan.Cache<?,?> The ma in cache interface
org.infinispan.manager.DefaultCacheManager The cache manager implementation. Responsible for managing the configuration and lifecycle of all aspects of Infinispan (caches, clustering, etc.)
org.infinispan.configuration.cache.ConfigurationBuilder The cache configuration builder, used to programmatically define the features of each cache.
org.infinispan.configuration.global.GlobalConfigurationBuilder The cache manager configuration builder, used to programmatically define global features such as thread pools, clustering, etc.
@org.infinispan.notifications.Listener The annotation to put on event listeners
org.infinispan.notifications.cachelistener.event All interfaces in this package define a type of Cache event which can be processed by a custom event listener
.*
org.infinispan.notifications.cachemanagerlistener.event.* A ll interfaces in this package define a type of CacheManager event which can be processed by a custom event listener
org.infinispan.commons.marshall.Marshaller The interface that defines a marshaller (serializer/deserializer) for custom data put in a cache
org.infinispan.query.SearchManager The entry point to create queries on top of a cache
Section 6

Cache Features

Infinispan caches are very powerful and can be configured and tuned to suit the needs of most applications. Let’s look at some of the features you can enable:

Expiration

Usually the purpose of a cache is to store short-lived data that needs to be refreshed regularly. You can tell Infinispan to store an entry only for a certain period of time:

1
1
cache.put("mortal", "human", 10, TimeUnit.MINUTES);

You can also set a default expiration lifespan for all entries in a cache via the configuration:

configurationBuilder.expiration().lifespan(10, TimeUnit.MINUTES);

or in XML:

4
1
<distributed-cache name="mortaldata">
2
  <expiration lifespan="600000" /> 
3
  <!-- lifespan in milliseconds -->
4
</distributed-cache>

Eviction

Normally, caches are unbounded, i.e. they grow indefinitely and it is up to the application to remove unneeded data. In Infinispan you can also set a maximum size for a cache: when this limit is reached, entries accessed least recently will be evicted. You can set the size based on number of entries:

1
1
configurationBuilder.eviction().strategy(EvictionStrategy.LRU).type(EvictionType.COUNT).size(1000);

or in XML:

4
1
<distributed-cache name="boundedcache">
2
  <eviction strategy="LIRS" type="COUNT" size="1000" />
3
  <!-- maximum 1000 entries -->
4
</distributed-cache>

You can also set a maximum memory occupation. This currently only works for some "well-known" data-types (String, byte[]) or when the storeAsBinary configuration option is enabled:

1
1
configurationBuilder.eviction().strategy(EvictionStrategy.LIRS).type(EvictionType.MEMORY).size(1000000);

or in XML:

4
1
<distributed-cache name="boundedcache">
2
  <eviction strategy="LIRS" type="MEMORY" size="1000000" />
3
  <!-- maximum 1MB -->
4
</distributed-cache>

Persistence

A cache, being an in-memory data structure, will lose data when stopped. You can overcome this limitation by backing a cache with a persistent store. Depending on configuration the store will act either as a persistent copy of your in-memory data or, in combination with eviction, as an overflow for excess data that you don’t want to keep in memory. There is a multitude of available cache stores available, depending on the features you need, ranging from file-based, to JDBC, to popular NoSQL and Cloud stores like MongoDB, S3, and Cassandra. Here are some examples:

Simple write-through file-based store:

1
1
configurationBuilder.persistence().addSingleFileStore().location("/path/to/persistent/data");

or in XML:

5
1
<distributed-cache name="filecache">
2
  <persistence>
3
    <file-store path="/path/to/persistent/data" />
4
  </persistence>
5
</distributed-cache>

Write-behind JDBC store with keys represented as strings using the internal connection pool:

7
1
configurationBuilder.persistence()
2
  .addStore(JdbcStringBasedStoreConfigurationBuilder.class)
3
  .connectionPool()
4
  .connectionUrl("jdbc:postgresql://dbhost:5432/infinispan")
5
  .username("infinispan").password("infinispan").driverClass(org.postgresql.Driver.class)
6
  .table().tableNamePrefix("ISPN_STRING_TABLE").createOnStart(true)
7
  .async().enabled();

or in XML:

9
1
<distributed-cache name="filecache">
2
  <persistence>
3
    <string-keyed-jdbc-store>
4
      <connection-pool connection-url="jdbc:postgresql://dbhost:5432/infinispan" username="infinispan" password="infinispan" driver="org.postgresql.Driver"/>
5
      <string-keyed-table create-on-start="true" prefix="ISPN_STRING_TABLE" />
6
      <write-behind />
7
    </string-keyed-jdbc-store>
8
  </persistence>
9
</distributed-cache>

Transactions

Infinispan can be configured to use and participate in JTA transactions. This means that you can perform multiple operations on a cache (or multiple caches) and commit them (or roll them back) atomically. You can either use the internal (but limited) DummyTransactionManager or an external transaction manager, like the one provided by your container. Configure a transactional cache with:

configurationBuilder.transaction().transactionMode(TransactionMode.TRANSACTIONAL);

or in XML:

3
1
<distributed-cache name="transactional">
2
  <transaction mode="NON_XA" />
3
</distributed-cache>

and then you interact with a cache transactionally as follows:

5
1
TransactionManager tm = cache.getAdvancedCache().getTransactionManager();
2
tm.begin();
3
int i = cache.get("k1");
4
cache.put("k1", i++);
5
tm.commit();

Queries and indexing

Infinispan supports searching of Java objects stored in the grid using powerful search APIs which complement its main Cache API. Supposing your cache contains Person objects:

5
1
public class Person {
2
  String name;
3
  String surname;
4
  Date birthday;
5
}

you can query it by obtaining a QueryFactory:

1
1
QueryFactory qf = org.infinispan.query.Search.getQueryFactory(cache);

then use the query DSL to build the query which will retrieve all Person objects whose name starts with J born between 1970 and 1975:

5
1
org.infinispan.query.dsl.Query query = qf.from(Person.class)
2
  .having("name").like("J%")
3
  .and().having("birthday").between(Year.of(1970), Year.of(1975))
4
  .toBuilder().build();
5
List<Person> list = query.list();

By default, queries will apply the predicates to all of the entries to find matches. You can also enable the use of Lucene indexes which will provide a significant performance boost along with a bunch of additional features.

First, enable indexing as follows:

1
1
configurationBuilder.indexing().enable().index(Index.LOCAL).autoConfig(true);

or in XML:

3
1
<distributed-cache name="indexedcache">
2
  <indexing index="LOCAL" auto-config="true" />
3
</distributed-cache>

then annotate the entities you are going to store in the cache by specifying which fields to index:

6
1
@Indexed
2
public class Person {
3
  @Field String name;
4
  @Field String surname;
5
  @Field Date birthday;
6
}

Events and Listeners

Infinispan offers a listener API, where clients can register for and get notified when events such as entries being modified or nodes joining/leaving the cluster take place. You don’t need to configure anything in order to be able to register events on a cache. For example, the following class defines a listener to print out some information every time a new entry is added to the cache:

8
1
@Listener
2
public class PrintWhenAdded {
3
  @CacheEntryCreated
4
  public void print(CacheEntryCreatedEvent event) {
5
    if (!event.isPre())
6
    System.out.println("New entry " + event.getKey() + " created in the cache");
7
  }
8
}

And you register the above listener on a cache with:

1
1
cache.addListener(new PrintWhenAdded());

Listener annotations have some attributes which affect their behavior. Most events will fire both before and after the actual operation is performed. You can tune this behavior by specifying an observation attribute:

1
1
@Listener(observation = Observation.POST)

Normally, listeners are local, i.e. they will receive events for affected entries only if they are owned by the node where the listener has been registered. You can register a listener that will listen to events happening on the entire cluster by using:

1
1
@Listener(clustered = true)

but be aware that this will increase cluster traffic considerably. Note that cluster listeners only support POST observation.

Executors

You can execute arbitrary code across your cluster using the ClusterExecutor. Just submit a java.util.function.Function, which receives the local CacheManager as input and can return any value to the invoker:

10
1
manager.executor().submitConsumer(localManager -> {
2
  String s = ... ; // Perform some meaningful computation local to each node
3
  return s;
4
}, (address, value, throwable) -> {
5
  if (throwable != null) {
6
    log.fatal("Encountered an error while processing on node " + address, t);
7
  } else {
8
    log.info(“Node %s has returned %s”, address, value);
9
  }
10
}).join();

The code you run doesn’t necessarily need to interact with the caches themselves.

Section 7

Caching Modes

Infinispan offers four caching modes: local, invalidation, replicated, and distributed. All of the modes support all of the features described above.

Local caches

As the name implies, these are entirely local, i.e. they do not share data with any other node, even when the CacheManager is being clustered. Because of this, they are also the fastest type of cache. A local cache is configured as follows:

2
1
cacheManager.defineConfiguration("localcache", new ConfigurationBuilder().build());
2
Cache<String, String> cache = cacheManager.getCache("localcache");

or in XML:

1
1
<local-cache name="localcache" />

Local caches can use all of the features of Infinispan (persistence, transactions, indexing, etc.). If, however, you don’t need any of those, you can use "simple caches" to squeeze even more performance. Simple caches support listeners, expiration, eviction, and security. Turn them on with:

2
1
cacheManager.defineConfiguration("simplecache", new ConfigurationBuilder().simpleCache(true).build());
2
Cache<String, String> cache = cacheManager.getCache("simplecache");

or in XML:

1
1
<local-cache name="simplecache" simple-cache="true" />

Invalidation caches

An invalidation cache is a special type of clustered cache that does not share any data: only invalidation messages are sent to other members in the cluster when a node modifies/removes an entry, thus avoiding stale data. An invalidation cache is configured as follows:

4
1
ConfigurationBuilder cfg = new ConfigurationBuilder();
2
cfg.clustering().cacheMode(CacheMode.INVALIDATION_SYNC);
3
cacheManager.defineConfiguration("invcache", cfg.build());
4
Cache<String, String> cache = cacheManager.getCache("invcache");

or in XML:

1
1
<invalidation-cache name="invcache" mode="SYNC"/>

Replicated caches

In replicated caches, entries added on any node are replicated to all other nodes in the cluster. This ensures that reads will always be performed locally. Use this type of cache for read-heavy data (e.g. lookup tables) where writes are infrequent and when you don’t need to scale capacity horizontally. A replicated cache is configured as follows:

4
1
ConfigurationBuilder cfg = new ConfigurationBuilder();
2
cfg.clustering().cacheMode(CacheMode.REPL_SYNC);
3
cacheManager.defineConfiguration("replcache", cfg.build());
4
Cache<String, String> cache = cacheManager.getCache("replcache");

or in XML:

1
1
<replicated-cache name="replcache" mode="SYNC" />

Distributed caches

Distributed caches allow you to scale capacity linearly by adding new nodes to the cluster. You only need to specify the number of owners (i.e. copies of data) you require. Infinispan will perform automatic rebalancing of data when nodes are added/removed to ensure that availability isn’t compromised. A distributed cache is configured as follows:

4
1
ConfigurationBuilder cfg = new ConfigurationBuilder();
2
cfg.clustering().cacheMode(CacheMode.DIST_SYNC).hash().numOwners(2);
3
cacheManager.defineConfiguration("distcache", cfg.build());
4
Cache<String, String> cache = cacheManager.getCache("distcache");

or in XML:

1
1
<distributed-cache name="distcache" mode="SYNC" num-owners="2" />

Synchronous and asynchronous clustering

For each of the clustered modes (invalidation, replicated, and distributed) you may choose whether you want communication between the nodes to be performed synchronously or asynchronously. Synchronous mode is slower, but safer, since write operations wait for completion on the remote nodes before returning. If you want more performance and can afford potentially losing writes on other nodes in case of failures, use asynchronous mode.

Which mode should I use?

The following table summarizes the characteristics of each cache mode, so that you can choose the one that more closely meets your application’s needs:

Mode Clustered Maximum capacity Availability Performance
Local No Limited by the the individual node’s memory Lowest, as there is only one copy of the data Maximum in reads and writes
Invalidation Yes, but without sharing data Limited by each node’s individual memory Low Fast reads, writes require remote calls
Replicated Yes, all nodes have the same data Limited by the node with the least memory Maximum, can survive failures of all nodes but one Fast reads, slowest writes
Distributed Yes, data is distributed in the cluster Limited only by the number of available nodes High, can survive concurrent failures of owners-1 nodes Reads and writes might require remote calls
Section 8

Caches and the Streams API

Since Infinispan 8 is based on Java 8, you can use the very powerful Java Streams API to perform mass filtering and computations on a Cache, by taking advantage of data distribution and locality. For example, the following piece of code:

6
1
cache.entrySet().stream()
2
  .map(e -> e.getValue()) // we only need the value
3
  .map(v -> v.toLowerCase()) // convert it to lowercase
4
  .map(v -> v.split("s+")) // split each value in single words
5
  .flatMap(a -> Arrays.stream(a)) // create a new stream from the array of single words
6
  .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); // count equal words

performs the usual "word count" on all values of a cache. On a distributed Infinispan cache this will run in parallel on all nodes, using all the computing power of your cluster.

Section 9

JCache (JSR-107)

Aside from the very powerful Cache API, you may also wish to use the standard JCache API, which offers an implementation-independent way to create, retrieve and manipulate caches. Ensure that your application depends on the infinispan-jcache JAR and use it as follows:

8
1
CachingProvider jcacheProvider = Caching.getCachingProvider();
2
CacheManager cacheManager = jcacheProvider.getCacheManager();
3
MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
4
configuration.setTypes(String.class, String.class);
5
Cache<String, String> cache = cacheManager.createCache("myCache", configuration);
6
cache.put("key", "value");
7
System.out.printf("key = %sn", cache.get("key"));
8
cacheManager.close();
Section 10

Client/server mode

You may not always want Infinispan instances to reside in the same JVM as your application. Sometimes this is for security, sometimes for architectural reasons to maintain a separate data layer, but this can also be because your client application is not on a JVM platform. For example, Node.js, C++, or .NET clients can also make use of Infinispan if it is run as a remote server.

Infinispan Server comes with several different server endpoints, speaking a variety of protocols. Here is a comparison of the protocols that can be used:

Protocol Format Clients Clustered Smart routing Balancing/Failover
REST Text Lots Yes No Any HTTP balancer
Memcached Text Lots Yes No Only with predefined list of servers
Hot Rod Binary Java, C++, .NET, JavaScript Yes Yes Dynamic, via Hot Rod clients
WebSocket Text JavaScript Yes No Any HTTP balancer

Infinispan Server is built on the robust foundation of WildFly, so it comes out of the box complete with management, monitoring and security features. To start a two-node cluster on your machine simply unzip the distribution and run the "domain" script:

1
1
$ bin/domain.sh

Infinispan Server comes with an easy to use administration console. Before using it you need to create an admin user:

1
1
$ bin/add-user.sh -u admin -p secret

At this point you can connect to the console by using the URL http://localhost:9990 in your browser.

Using Infinispan over the Hot Rod protocol is similar to using the embedded API. First of all, you need to depend on the client JAR:

9
1
<dependencies>
2
  …
3
  <dependency>
4
    <groupId>org.infinispan</groupId>
5
    <artifactId>infinispan-client-hotrod</artifactId>
6
    <version>LATEST_INFINISPAN_VERSION</version>
7
  </dependency>
8
  …
9
</dependencies>

And then use it in your application as follows:

4
1
RemoteCacheManager rcm = new RemoteCacheManager("infinispan_host");
2
RemoteCache rc = rcm.getCache();
3
rc.put("hello", "world");
4
String value = rc.get("hello");

The Hot Rod protocol has been designed specifically with load balancing and failover in mind. Clients can be written to take advantage of the server topology that is provided to clients and regularly kept up-to-date. Clients can even be made aware of hash functions used on the backend, so routing requests to a cluster of backend nodes can be done in an intelligent fashion to minimize latency and remote lookup on the backend. All of the available clients (Java, C++, .NET, and Node.js) make use of such features and provide built-in load-balancing, failover, discovery of new backend nodes, as well as intelligent routing of requests.

If you need to share data among different languages/platforms, the use of a common marshalling format is recommended. While Infinispan does not impose a default, one option is ProtoBuf, a compact format with wide support.

Section 11

Containers and Cloud environments

If you like running your applications in containers, Infinispan comes with a well-maintained container image that makes it easy to run Infinispan Servers in clustered, domain, and standalone modes, with different protocol stacks.

By default, the container runs in clustered mode, and to start a node simply execute:

1
1
docker run -it jboss/infinispan-server

Starting a second container will cause it to form a cluster with the first one. You can check the membership status of the cluster by running a command directly in the newly launched container:

1
1
docker exec -it $(docker ps -q -l) /opt/jboss/infinispan-server/bin/ispn-cli.sh -c "/subsystem=datagrid-infinispan/cache-container=clustered:read-attribute(name=members)"

Which should output something like this:

4
1
{
2
  "outcome" => "success",
3
  "result" => "[57b9a116303f, 7648d4b62987]"
4
}

The above examples will use UDP Multicast to perform discovery and cluster communication. If, however, you’re running in a cloud environment or another environment that does not allow the use of multicast, you can use the JGroups Gossip Router as an alternative discovery mechanism. Employing a gossip router will enable discovery via TCP, where the router acts as a registry: each member will register itself in this registry upon start and also discover other members.

Launch the gossip router container as follows:

1
1
docker run -ti -p 12001:12001 jboss/jgroups-gossip:3.6.10.Final

Take note of the address where the router will bind to, it's needed by the Infinispan nodes. The address can be easily obtained by:

1
1
docker inspect --format '\{\{ .NetworkSettings.IPAddress \}\}' $(docker ps -q -l)

Finally we can now launch our cluster specifying the tcp-gossip stack with the location of the gossip router:

1
1
docker run -it jboss/infinispan-server -Djboss.default.jgroups.stack=tcp-gossip -Djgroups.gossip.initial_hosts=172.17.0.2[12001]
Section 12

Integrations

Infinispan can share data in lots of interesting and powerful ways.

Hibernate ORM

Infinispan can be a provider for Hibernate’s second-level cache. Just add the hibernate-infinispan module as a dependency and specify the appropriate region factory in the persistence.xml file:

​x
1
<property name="hibernate.cache.region.factory_class"
2
​
3
value="org.hibernate.cache.infinispan.InfinispanRegionFactory" />

If you are using the WildFly application server, you only need to add the following to the persistence.xml file:

1
1
<property name="hibernate.cache.use_second_level_cache" value="true" />

Apache Hadoop

The Hadoop Connector allows you to run your Hadoop jobs on data stored in Infinispan using InputFormat and OutputFormat. Here’s how you’d set up a map/reduce job with Hadoop and Infinispan:

14
1
Configuration configuration = new Configuration();
2
String hosts = "172.17.0.2:11222;172.17.0.3:11222";
3
// Configures input/output caches
4
configuration.set(InfinispanConfiguration.INPUT_REMOTE_CACHE_SERVER_LIST, hosts);
5
configuration.set(InfinispanConfiguration.OUTPUT_REMOTE_CACHE_SERVER_LIST, hosts);
6
configuration.set(InfinispanConfiguration.INPUT_REMOTE_CACHE_NAME, "map-reduce-in");
7
configuration.set(InfinispanConfiguration.OUTPUT_REMOTE_CACHE_NAME, "map-reduce-out");
8
​
9
Job job = Job.getInstance(configuration, "Infinispan job");
10
// Map and Reduce implementation
11
job.setMapperClass(MapClass.class);
12
job.setReducerClass(ReduceClass.class);
13
job.setInputFormatClass(InfinispanInputFormat.class);
14
job.setOutputFormatClass(InfinispanOutputFormat.class);

Apache Spark

If you’d like to do the same but with Apache Spark, Infinispan comes with RDD and DStream integrations. The following Scala example creates a filtered RDD by supplying an Infinispan query:

10
1
val config = new Properties
2
config.put("infinispan.rdd.cacheName","my-cache")
3
config.put("infinispan.client.hotrod.server_list","10.9.0.8:11222")
4
val infinispanRDD = new InfinispanRDD[String, MyEntity](sc, configuration = config)
5
// Assuming MyEntity is already stored in myCache
6
val query = Search.getQueryFactory(myCache)
7
  .from(classOf[MyEntity])
8
  .having("field").equal("some value")
9
  .toBuilder[RemoteQuery].build
10
val filteredRDD = rdd.filterByQuery(query, classOf[User])

Apache Camel

Infinispan can also participate in Apache Camel routes acting as both a producer and consumer in both embedded and remote scenarios. Add the appropriate dependency via Maven:

6
1
<dependency>
2
  <groupId>org.apache.camel</groupId>
3
  <artifactId>camel-infinispan</artifactId>
4
  <version>x.x.x</version>
5
  <!-- use the same version as your Camel core version -->
6
</dependency>

Retrieving a value from a cache using a specific key:

4
1
from("direct:start")
2
  .setHeader(InfinispanConstants.OPERATION, constant(InfinispanConstants.GET))
3
  .setHeader(InfinispanConstants.KEY, constant("123"))
4
  .to("infinispan://localhost?cacheContainer=#cacheContainer");
Section 13

Additional resources

This Refcard only skims the surface of what can be achieved with Infinispan. For additional information, tutorials, examples, etc., please refer to the Infinispan website at http://infinispan.org.

Bio: Tristan Tarrant, Principal Software Engineer

Tristan Tarrant is the lead for the Infinispan project. Starting with a background in computer science specializing in parallel architectures, he has been involved in the design and implementation of software architectures for customers at all scales. An active contributor to JBoss Community and other open source projects for many years, he is a passionate advocate of open source and open standards.

Like This Refcard? Read More From DZone

related article thumbnail

DZone Article

A Look at DataSynapse GridServer (With Example)
related article thumbnail

DZone Article

Cutting-Edge Object Detection for Autonomous Vehicles: Advanced Transformers and Multi-Sensor Fusion
related article thumbnail

DZone Article

Power BI Embedded Analytics — Part 1.1: Power BI Authoring Data Federation
related article thumbnail

DZone Article

Cloud Cost Optimization for ML Workloads With NVIDIA DCGM
related refcard thumbnail

Free DZone Refcard

Getting Started With Vector Databases
related refcard thumbnail

Free DZone Refcard

MongoDB Essentials
related refcard thumbnail

Free DZone Refcard

PostgreSQL Essentials
related refcard thumbnail

Free DZone Refcard

NoSQL Migration Essentials

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: