Apache Ignite:如何从持久化存储中读取数据
来源:互联网 发布:ubuntu虚拟机怎么联网 编辑:程序博客网 时间:2024/05/18 01:07
数据可以直接从任何持久存储区加载到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网站。
- Apache Ignite:如何从持久化存储中读取数据
- 数据持久化之文件存储读取
- Ignite缓存持久化例子
- 内存数据组织Apache Ignite
- Android--持久化技术之文件存储-数据读取
- Android从外部存储设备中读取,或存储数据
- 数据持久化存储
- 数据持久化 ( 存储 )
- 如何从数据库中读取数据
- 如何从property文件中读取数据
- 数据存储到文件和从文件中读取数据
- Apache Ignite
- Apache Ignite
- Apache Ignite
- 数据存储:数据持久化
- 数据存储---数据持久化
- 如何从文件夹中读取或存储文件
- iphone数据持久化存储
- halcon学习笔记——机器视觉工程应用的开发思路
- MyBatis传入参数与parameterType
- JAVA List与数组互转
- 最近工作,发发牢骚
- p2p行业整理
- Apache Ignite:如何从持久化存储中读取数据
- 【刷题之路】有序矩阵查找问题
- Android:两个Fragment在同一个Framelayout上切换显示
- arcengine onmouseup使用contextmenustrip添加右键菜单
- cocos code ide单步调试
- Linux环境下查看CPU是否支持VT虚拟化 - VT-X或VT-D
- 什么样的人不适合当程序员呢?
- 2016-06-13,csdn博客启用,记录纪念之。
- Mybatis传多个参数(三种解决方案)