Apache Ignite:如何从持久化存储中读取数据 您所在的位置:网站首页 ignite官网 Apache Ignite:如何从持久化存储中读取数据

Apache Ignite:如何从持久化存储中读取数据

2023-12-17 01:21| 来源: 网络整理| 查看: 265

       数据可以直接从任何持久存储区加载到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 中指定了以下依赖项 :

org.apache.ignite ignite-core 1.5.0.final org.apache.ignite ignite-spring 1.5.0.final mysql mysql-connector-java 5.1.6

4.Read-Through配置

从数据库中加载数据,你需要启用 read-through 模式和设置CacheConfiguration的cacheStoreFactory属性。你可以在pring XML配置文件或程序中设置这些值。

...

5.实现CacheStore

现在我们有我们的模型,Maven依赖关系和缓存已配置到位,那么,现在是时候来实现存储。若从数据库加载数据,应实现CacheStore接口的 loadCache()和 load()的方法。

public class PersonStore implements CacheStore { @SpringResource(resourceName = "dataSource") private DataSource dataSource; // This method is called whenever IgniteCache.loadCache() method is called. @Override public void loadCache(IgniteBiInClosure 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 cache = ignite.getOrCreateCache("personCache")) { // Load cache with data from the database. cache.loadCache(null); // Execute query on cache. QueryCursor


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有