• Apache Ignite 作为 MySql的加速层


    可以将 Ignite 用作现有数据库(例如 RDBMS 或 NoSQL 数据库,例如 Apache Cassandra 或 MongoDB)之上的缓存层。这个用例通过使用内存处理来加速底层数据库。

    Ignite 提供与 Apache Cassandra 的开箱即用集成。对于其他没有现成集成的 NoSQL 数据库,您可以提供自己的CacheStore接口实现。

    可以使用外部存储的两个主要用例包括:

    • 现有数据库的缓存层。在这种情况下,您可以通过将数据加载到内存中来提高处理速度。您还可以将 SQL 支持引入没有它的数据库(当所有数据都加载到内存中时)。

    • 您希望将数据持久保存在外部数据库中(而不是使用本机持久性

    读写策略

    直读Read-Through和直写Write-Through

    通读意味着如果数据在缓存中不可用,则从底层持久存储中读取数据。请注意,这仅适用于通过键值 API 进行的 get 操作;SELECT 查询从不读取外部数据库中的数据。loadCache()要执行选择查询,必须通过调用该方法将数据从数据库预加载到缓存中。

    直写意味着数据在缓存中更新时会自动持久化。所有的通读和通写操作都参与缓存事务,并作为一个整体提交或回滚。

    后写缓存Write-Behind

    在简单的直写模式下,每个 put 和 remove 操作都涉及对持久存储的相应请求;因此,更新操作的总持续时间可能相对较长。此外,密集的高速缓存更新率会导致极高的存储负载。

    对于这种情况,您可以启用write-behind模式,其中更新操作是异步执行的。这种方法的关键概念是累积更新并将它们作为批量操作异步刷新到底层数据库。您可以基于基于时间的事件(数据条目可以驻留在队列中的最长时间受到限制)、队列大小的事件(当队列大小达到某个特定点时刷新队列)或这两者来触发数据刷新(以先发生者为准)。

    关系型数据库集成

    要将 RDBMS 用作底层存储,您可以使用以下实现之一CacheStore

    • CacheJdbcPojoStore — 使用反射将对象存储为一组字段。如果您在现有数据库之上添加 Ignite 并希望使用基础表中的特定字段(或所有字段),请使用此实现。

    • CacheJdbcBlobStore — 将对象以 Blob 格式存储在底层数据库中。当您将外部数据库用作持久存储并希望以简单格式存储数据时,此选项非常有用。

    测试代码

    在MySql中创建测试表,导入测试数据

    1. -- ignite.PERSON definition
    2. CREATE TABLE `PERSON` (
    3. `id` int(11) NOT NULL,
    4. `name` varchar(128) DEFAULT NULL,
    5. PRIMARY KEY (`id`)
    6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    7. INSERT INTO ignite.PERSON
    8. (id, name)
    9. VALUES(1, 'sn');

    创建Java Bean,与MySql中的表做映射

    1. class Person implements Serializable {
    2. private static final long serialVersionUID = 0L;
    3. private int id;
    4. private String name;
    5. public Person() {
    6. }
    7. public String getName() {
    8. return name;
    9. }
    10. public void setName(String name) {
    11. this.name = name;
    12. }
    13. public int getId() {
    14. return id;
    15. }
    16. public void setId(int id) {
    17. this.id = id;
    18. }
    19. @Override
    20. public String toString() {
    21. return "Person{" +
    22. "id=" + id +
    23. ", name='" + name + '\'' +
    24. '}';
    25. }
    26. }

    创建测试Main

    1. public class JdbcStoreDemo implements Serializable {
    2. public static void main(String[] args) {
    3. IgniteConfiguration igniteCfg = new IgniteConfiguration();
    4. CacheConfiguration<Integer, Person> personCacheCfg = new CacheConfiguration<>();
    5. personCacheCfg.setName("PersonCache");
    6. personCacheCfg.setCacheMode(CacheMode.PARTITIONED);
    7. personCacheCfg.setAtomicityMode(CacheAtomicityMode.ATOMIC);
    8. personCacheCfg.setReadThrough(true);
    9. personCacheCfg.setWriteThrough(true);
    10. CacheJdbcPojoStoreFactory<Integer, Person> factory = new CacheJdbcPojoStoreFactory<>();
    11. factory.setDialect(new MySQLDialect());
    12. factory.setDataSourceFactory(new JDBCFactory());
    13. // factory.setDataSourceFactory(new Factory<DataSource>() {
    14. // @Override
    15. // public DataSource create() {
    16. // MysqlDataSource mysqlDataSrc = new MysqlDataSource();
    17. // mysqlDataSrc.setURL("jdbc:mysql://192.168.165.43:3306/ignite");
    18. // mysqlDataSrc.setUser("root");
    19. // mysqlDataSrc.setPassword("1q2w3eROOT!");
    20. // return mysqlDataSrc;
    21. // }
    22. // });
    23. JdbcType personType = new JdbcType();
    24. personType.setCacheName("PersonCache");
    25. personType.setKeyType(Integer.class);
    26. personType.setValueType(Person.class);
    27. personType.setDatabaseTable("PERSON");
    28. personType.setKeyFields(new JdbcTypeField(java.sql.Types.INTEGER, "id", Integer.class, "id"));
    29. personType.setValueFields(new JdbcTypeField(java.sql.Types.INTEGER, "id", Integer.class, "id"));
    30. personType.setValueFields(new JdbcTypeField(java.sql.Types.VARCHAR, "name", String.class, "name"));
    31. factory.setTypes(personType);
    32. personCacheCfg.setCacheStoreFactory(factory);
    33. QueryEntity qryEntity = new QueryEntity();
    34. qryEntity.setKeyType(Integer.class.getName());
    35. qryEntity.setValueType(Person.class.getName());
    36. qryEntity.setKeyFieldName("id");
    37. Set<String> keyFields = new HashSet<>();
    38. keyFields.add("id");
    39. qryEntity.setKeyFields(keyFields);
    40. LinkedHashMap<String, String> fields = new LinkedHashMap<>();
    41. fields.put("id", "java.lang.Integer");
    42. fields.put("name", "java.lang.String");
    43. qryEntity.setFields(fields);
    44. personCacheCfg.setQueryEntities(Collections.singletonList(qryEntity));
    45. igniteCfg.setCacheConfiguration(personCacheCfg);
    46. //igniteCfg.setClientMode(true);
    47. // Classes of custom Java logic will be transferred over the wire from this app.
    48. igniteCfg.setPeerClassLoadingEnabled(true);
    49. // igniteCfg.setDeploymentMode(DeploymentMode.CONTINUOUS);
    50. // Setting up an IP Finder to ensure the client can locate the servers.
    51. // TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
    52. // ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));
    53. // igniteCfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));
    54. TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
    55. ArrayList<String> strings = new ArrayList<>();
    56. ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));
    57. ipFinder.setAddresses(strings);
    58. igniteCfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));
    59. // UriDeploymentSpi deploymentSpi = new UriDeploymentSpi();
    60. // deploymentSpi.setUriList(Arrays.asList("file:/Users/wangkai/sourceFromGit/flink-sources/target"));
    61. // igniteCfg.setDeploymentSpi(deploymentSpi);
    62. // Starting the node
    63. Ignite ignite = Ignition.start(igniteCfg);
    64. //ignite.active();
    65. // System.out.println(ignite.cacheNames());
    66. IgniteCache<Integer, Person> personCache = ignite.cache("PersonCache");
    67. Person person = personCache.get(3);
    68. System.out.println(person);
    69. // Person person1 = new Person();
    70. // person1.setId(3);
    71. // person1.setName("aaa");
    72. // personCache.put(3,person1);
    73. // Iterator<Cache.Entry<Integer, Person>> iterator = personCache.iterator();
    74. // while (iterator.hasNext()){
    75. // System.out.println(iterator.next().getValue());
    76. // }
    77. }
    JDBCFactory类
    1. import javax.cache.configuration.Factory;
    2. import javax.sql.DataSource;
    3. import java.io.Serializable;
    4. public class JDBCFactory implements Factory<DataSource>, Serializable {
    5. @Override
    6. public DataSource create() {
    7. MysqlDataSource mysqlDataSrc = new MysqlDataSource();
    8. mysqlDataSrc.setURL("jdbc:mysql://127.0.0.1:3306/ignite");
    9. mysqlDataSrc.setUser("root");
    10. mysqlDataSrc.setPassword("xxx");
    11. return mysqlDataSrc;
    12. }
    13. }

  • 相关阅读:
    Bert不完全手册8. 预训练不要停!Continue Pretraining
    量子力学的应用:量子通信和量子感应
    golang生成可供python调用的库(python调用Go代码)
    java基于RestTemplate的微服务发起http请求
    力扣labuladong——一刷day39
    从零开始配置vim(29)——DAP 配置
    AutoIt Window Info 使用方法
    Docker专题(二)之 操作Docker容器
    WebRTC系列-网络传输之4Connection排序
    第一季:18es与solr的区别【Java面试题】
  • 原文地址:https://blog.csdn.net/wank1259162/article/details/125409346