Working with the POJO strategy 

In our example, we have a primitive integer as a key and the Conference class as value. In the persistence settings .xml file, the keyPersistence node represents the key of our cache. A key can be a concrete object, but in our case the key is just an integer. Therefore, we'll set the id as key in keyPersistence. We are using the POJO strategy, so we won't set the column attribute for the valuePersistence node. The <field > markup node is used to map the Java members to Cassandra columns.

  1. Create a conference-persistence-settings.xml file and keep it in the /src/main/resourcecs/META-INF/ folder:
      <persistence keyspace="persistence" table="conference">
<keyPersistence class="java.lang.Integer" strategy="PRIMITIVE" column="id"/>
<valuePersistence class="com.persistence.cassendra.Conference"
strategy="POJO">
<field name="id" column="id"/>
<field name="name" column="name"/>
<field name="startDateTime" column="startdatetime"/>
<field name="endDateTime" column="enddatetime"/>
</valuePersistence>
</persistence>
  1. Create a class to configure Cassandra's data source and a cache store factory to persist the cached objects in our Cassandra datastore:
      public class CassandraPersistencePOJOTest { 
public static void main(String[] args) throws IOException {
  1. Configure Cassandra's DataSource:
      DataSource cassandraDataSource = new DataSource();
cassandraDataSource.setContactPoints("127.0.0.1");
RoundRobinPolicy robinPolicy = new RoundRobinPolicy();
cassandraDataSource.setLoadBalancingPolicy(robinPolicy);
cassandraDataSource.setReadConsistency("ONE");
cassandraDataSource.setWriteConsistency("ONE");
  1. Read the persistence settings for Cassandra's key/value persistence:
      String file = CassandraPersistencePOJOTest.class.getClassLoader()
.getResource("META-INF//conference-persistence-settings.xml")
.getFile();
String persistenceSettingsXml = FileUtils.readFileToString
(new File(file), "utf-8");
KeyValuePersistenceSettings persistenceSettings = new
KeyValuePersistenceSettings(persistenceSettingsXml);
  1. Define Cassandra's cache store factory and set the Cassandra DataSource and persistence settings:
      CassandraCacheStoreFactory<Integer, Conference> 
cassandraCacheStoreFactory = new CassandraCacheStoreFactory
<Integer, Conference>();
cassandraCacheStoreFactory.setDataSource(cassandraDataSource);
cassandraCacheStoreFactory.setPersistenceSettings(persistenceSettings);
  1. Define the cache configuration, set the cassandraCacheStoreFactory to cacheStoreFactory, and enable write-through, write-behind, and read-through:
      CacheConfiguration<Integer, Conference> configuration = 
new CacheConfiguration<>();
configuration.setName("cassandra");
configuration.setCacheStoreFactory(cassandraCacheStoreFactory);
configuration.setWriteThrough(true);
configuration.setWriteBehindEnabled(true);
configuration.setReadThrough(true);
  1. Set the cache configuration:
      IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setCacheConfiguration(configuration);
  1. Start the Ignite server with the Cassandra cache configuration:
      Ignite ignite = Ignition.start(cfg);
System.out.println("done");
final IgniteCache<Integer, Conference> cache =
ignite.getOrCreateCache("cassandra");
  1. Put a Conference object into the cache:
      cache.put(1, new Conference(1, "GOTO", new Date(), new Date()));

  1. Get the object from the cache:
        Conference gotoConf = cache.get(1);
System.out.println("Value: " + gotoConf);
}
}
  1. Run the program and query the Cassandra table. The CQL is SELECT * from conference;:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.249.210