Skip to content Skip to sidebar Skip to footer

Apache Ignite Loadcache Read Data From Multiple Tables Oracle

Data can be loaded directly from any persistent store into Apache Ignite caches. This instance shows you how to load data from a MySQL database into an Ignite distributed cache. Here, I am assuming that you already accept Apache Ignite installed on your organization. If not, y'all can go through this tutorial first.

ane. Sample PERSON Table

To start with, here is what the PERSON data in my database looks like:

Image title

ii. Model

Here is a samplePerson.java grade corresponding to the PERSON table in the database.

              public grade Person {      private long id;      private long orgId;      individual String proper noun;      private int salary;      // Constructor     …     // Getter and Setter methods     … }            

3. Maven Dependency

I accept specified the following dependencies in my project's pom.xml:

              <dependency>     <groupid>org.apache.ignite</groupid>     <artifactid>ignite-core</artifactid>     <version>ane.5.0.final</version> </dependency>  <dependency>     <groupid>org.apache.ignite</groupid>     <artifactid>ignite-spring</artifactid>     <version>ane.v.0.final</version> </dependency>  <dependency>     <groupid>mysql</groupid>     <artifactid>mysql-connector-java</artifactid>     <version>5.1.6</version> </dependency>            

4. Configure Read-Through

To load the data from the database, y'all need to enable the read-through mode and set thecacheStoreFactoryproperty ofCacheConfiguration. You can prepare these values either in your Jump XML configuration file or programmatically.

              <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">     <property proper noun="driverClassName" value="com.mysql.jdbc.Commuter"></property>     <property proper noun="url" value="jdbc:mysql://localhost:3306/mydbname"></property>     <property proper noun="username" value="username"></property>     <holding name="password" value="passwd"></property> </bean>  <edible bean form="org.apache.ignite.configuration.IgniteConfiguration" id="ignite.cfg">     <property name="cacheConfiguration">         <list>             <bean grade="org.apache.ignite.configuration.CacheConfiguration">                 <property name="name" value="personCache"></property>                 <!-- Enable readThrough-->                 <property name="readThrough" value="truthful"></property>                 <property proper name="writeThrough" value="truthful"></holding>                 <!-- Set cacheStoreFactory-->                 <property proper noun="cacheStoreFactory">                     <edible bean class="javax.cache.configuration.FactoryBuilder" factory-method="factoryOf">                         <constructor-arg value="myexamples.store.PersonStore"></constructor-arg>                     </bean>                 </property>                 <holding proper noun="queryEntities">                     <list>                         <bean grade="org.apache.ignite.cache.QueryEntity">                             <property proper name="keyType" value="coffee.lang.Long"></property>                             <property name="valueType" value="ignite.myexamples.model.Person"></property>                             <property name="fields">                                 <map>                                     <entry key="id" value="coffee.lang.Long"></entry>                                     <entry primal="name" value="java.lang.Cord"></entry>                                     <entry primal="orgId" value="coffee.lang.Long"></entry>                                     <entry fundamental="bacon" value="coffee.lang.Integer"></entry>                                 </map>                             </property>                         </bean>                     </list>                 </property>             </bean>         </list>     </property>      <property name="peerClassLoadingEnabled" value="true"></holding>      <!-- Other Ignite configurations-->     ...  </bean>            

5. Implement CacheStore

Now that nosotros have our model, maven dependencies, and cache configurations in place, it'due south time to implement the shop. To load the data from the database,loadCache() andload() methods of theCacheStore interface should be implemented.

              public course PersonStore implements CacheStore<Long, Person> {      @SpringResource(resourceName = "dataSource")     private DataSource dataSource;      // This method is called whenever IgniteCache.loadCache() method is chosen.     @Override     public void loadCache(IgniteBiInClosure<Long, Person> clo, @Nullable Object... objects) throws CacheLoaderException {         System.out.println(">> Loading enshroud from store...");          endeavour (Connection conn = dataSource.getConnection()) {             try (PreparedStatement st = conn.prepareStatement("select * from PERSON")) {                 try (ResultSet rs = st.executeQuery()) {                     while (rs.next()) {                         Person person = new Person(rs.getLong(ane), rs.getLong(2), rs.getString(3), rs.getInt(iv));                          clo.utilize(person.getId(), person);                     }                 }             }         }         catch (SQLException e) {             throw new CacheLoaderException("Failed to load values from enshroud store.", eastward);         }     }      // This method is chosen whenever IgniteCache.become() method is called.     @Override     public Person load(Long key) throws CacheLoaderException {         System.out.println(">> Loading person from store...");          try (Connection conn = dataSource.getConnection()) {             try (PreparedStatement st = conn.prepareStatement("select * from PERSON where id = ?")) {                 st.setString(1, key.toString());                  ResultSet rs = st.executeQuery();                  return rs.next() ? new Person(rs.getLong(1), rs.getLong(two), rs.getString(three), rs.getInt(4)) : null;             }         }         catch (SQLException due east) {             throw new CacheLoaderException("Failed to load values from cache store.", due east);         }     }      // Other CacheStore method implementations.     … }            

For convenience purposes, Ignite also provides it users withCacheStoreAdapter class that has a default implementation for some of theCacheStore methods -loadAll(),writeAll(), anddeleteAll().

6. Load Enshroud

Hither is a samplePersonStoreExample.java class that makes a call toIgniteCache.loadCache()  method which internally delegates the call toCacheStore.loadCache() method (which we implemented in the previous step).

              public form PersonStoreExample {     public static void principal(Cord[] args) throws IgniteException {         Ignition.setClientMode(true);          effort (Ignite ignite = Ignition.start("config/cluster-config.xml")) {             try (IgniteCache<Long, Person> cache = ignite.getOrCreateCache("personCache")) {                 // Load cache with information from the database.                 cache.loadCache(null);                  // Execute query on cache.                 QueryCursor<Listing<?>> cursor = cache.query(new SqlFieldsQuery(                         "select id, name from Person"));                  Organization.out.println(cursor.getAll());             }         }     } }            

vii. Commencement Ignite Cluster

From the control shell, lead yourself to the Ignite installation folder and start the server nodes, using the post-obit command:

              $ bin/ignite.sh <path-to-Spring-XML-configuration-file>            

Make sure thatPersonStore.coffee is in the class path of Ignite. To do then, you can set the USER_LIBS environment variable, or drop the project jar into thelibs folder of your Ignite installation.

8. Output

From your IDE, runPersonStoreExample.coffee.

Image title

For more information, documentation, and screencasts visit the Apache Ignite website.

Topics:

caching, apache ignite, tutorial, database, data grid, compute grid, sql

Published at DZone with permission of Dmitriy Setrakyan, DZone MVB . See the original article here.

Opinions expressed by DZone contributors are their own.

lawrenceoldideady.blogspot.com

Source: https://dzone.com/articles/apache-ignite-how-to-read-data-from-persistent-sto

Post a Comment for "Apache Ignite Loadcache Read Data From Multiple Tables Oracle"