数据可以直接从任何持久存储区加载到Apache Ignite缓存。这个例子展示了如何从一个MySQL数据库加载数据到另一个Ignite分布式缓存。在这里,假设你已经在你的系统上安装了Apache Ignite。如果没有,你可以先通过本教程学习下。
中文网:https://www.zybuluo.com/liyuj/note/230739
1.Sample PERSON Table
首先,这是我数据库中PERSON的数据:
2.模型
这是一个Person.java类对应数据库中PERSON表的例子。
public class Person {
private long id;
private long orgId;
private String name;
private int salary;
// Constructor
…
// Getter and Setter methods
…
}
3.Maven Dependency
我已在我的项目 pom.xml 中指定了以下依赖项 :
<dependency>
<groupid>org.apache.ignite</groupid>
<artifactid>ignite-core</artifactid>
<version>1.5.0.final</version>
</dependency>
<dependency>
<groupid>org.apache.ignite</groupid>
<artifactid>ignite-spring</artifactid>
<version>1.5.0.final</version>
</dependency>
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<version>5.1.6</version>
</dependency>
4.Read-Through配置
从数据库中加载数据,你需要启用 read-through 模式和设置CacheConfiguration的cacheStoreFactory属性。你可以在pring XML配置文件或程序中设置这些值。
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mydbname"></property>
<property name="username" value="username"></property>
<property name="password" value="passwd"></property>
</bean>
<bean class="org.apache.ignite.configuration.IgniteConfiguration" id="ignite.cfg">
<property name="cacheConfiguration">
<list>
<bean class="org.apache.ignite.configuration.CacheConfiguration">
<property name="name" value="personCache"></property>
<!-- Enable readThrough-->
<property name="readThrough" value="true"></property>
<property name="writeThrough" value="true"></property>
<!-- Set cacheStoreFactory-->
<property name="cacheStoreFactory">
<bean class="javax.cache.configuration.FactoryBuilder" factory-method="factoryOf">
<constructor-arg value="myexamples.store.PersonStore"></constructor-arg>
</bean>
</property>
<property name="queryEntities">
<list>
<bean class="org.apache.ignite.cache.QueryEntity">
<property name="keyType" value="java.lang.Long"></property>
<property name="valueType" value="ignite.myexamples.model.Person"></property>
<property name="fields">
<map>
<entry key="id" value="java.lang.Long"></entry>
<entry key="name" value="java.lang.String"></entry>
<entry key="orgId" value="java.lang.Long"></entry>
<entry key="salary" value="java.lang.Integer"></entry>
</map>
</property>
</bean>
</list>
</property>
</bean>
</list>
</property>
<property name="peerClassLoadingEnabled" value="true"></property>
<!-- Other Ignite configurations-->
...
</bean>
5.实现CacheStore
现在我们有我们的模型,Maven依赖关系和缓存已配置到位,那么,现在是时候来实现存储。若从数据库加载数据,应实现CacheStore接口的 loadCache()和 load()的方法。
public class PersonStore implements CacheStore<Long, Person> {
@SpringResource(resourceName = "dataSource")
private DataSource dataSource;
// This method is called whenever IgniteCache.loadCache() method is called.
@Override
public void loadCache(IgniteBiInClosure<Long, Person> clo, @Nullable Object... objects) throws CacheLoaderException {
System.out.println(">> Loading cache from store...");
try (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(1), rs.getLong(2), rs.getString(3), rs.getInt(4));
clo.apply(person.getId(), person);
}
}
}
}
catch (SQLException e) {
throw new CacheLoaderException("Failed to load values from cache store.", e);
}
}
// This method is called whenever IgniteCache.get() 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(2), rs.getString(3), rs.getInt(4)) : null;
}
}
catch (SQLException e) {
throw new CacheLoaderException("Failed to load values from cache store.", e);
}
}
// Other CacheStore method implementations.
…
}
为了方便起见,Ignite还为用户提供了一些具有默认实现CacheStore方法的CacheStoreAdapter类—— loadAll()、writeAll()和deleteAll()。
6.加载缓存
这是一个PersonStoreExample.java类调用IgniteCache.loadCache()方法,在内部将调用CacheStore.loadCache()方法的示例(在上一步中我们实现了)。
public class PersonStoreExample {
public static void main(String[] args) throws IgniteException {
Ignition.setClientMode(true);
try (Ignite ignite = Ignition.start("config/cluster-config.xml")) {
try (IgniteCache<Long, Person> cache = ignite.getOrCreateCache("personCache")) {
// Load cache with data from the database.
cache.loadCache(null);
// Execute query on cache.
QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery(
"select id, name from Person"));
System.out.println(cursor.getAll());
}
}
}
}
7.开始Ignite集群
从shell命令中,使用下面的命令,自己安装Ignite文件夹并启动服务器节点:
$ bin/ignite.sh <path-to-Spring-XML-configuration-file>
确保personstore.java是在类路径中Ignite。这样做,你可以设置USER_LIBS环境变量,或将项目jar放入Ignite安装的libs文件夹中。
8.输出
从你的IDE,运行PersonStoreExample.java。
更多的信息和文档,用户可以访问ApacheIgnite网站。