• Apache Ignite 使用SQL模式


    Ignite 带有符合 ANSI-99、水平可扩展和容错的分布式 SQL 数据库。根据用例,通过跨集群节点对数据进行分区或完全复制来提供分布。

    作为 SQL 数据库,Ignite 支持所有 DML 命令,包括 SELECT、UPDATE、INSERT 和 DELETE 查询,并且还实现了与分布式系统相关的 DDL 命令子集。

    您可以通过连接来自外部工具和应用程序的JDBCODBC驱动程序与 Ignite 进行交互,就像与任何其他启用了 SQL 的存储一样。Java、.NET 和 C++ 开发人员可以利用本机 SQL API

    在内部,SQL 表与键值缓存具有相同的数据结构。这意味着您可以更改数据的分区分布并利用亲和力托管技术来获得更好的性能。

    Schema定义

    系统模式

    ignite 有许多默认模式并支持创建自定义模式(SCHEMA)。

    默认情况下有两种可用的模式:

    • SYS 模式,其中包含许多带有集群节点信息的系统视图。您不能在此架构中创建表。有关详细信息,请参阅系统视图页面。

    • PUBLIC 架构,在未指定架构时默认使用。

    自定义模式

    可以通过 的sqlSchemas属性设置自定义模式IgniteConfiguration。您可以在启动集群之前在配置中指定模式列表,然后在运行时在这些模式中创建对象。

    1. IgniteConfiguration cfg = new IgniteConfiguration();
    2. SqlConfiguration sqlCfg = new SqlConfiguration();
    3. sqlCfg.setSqlSchemas("MY_SCHEMA", "MY_SECOND_SCHEMA" );
    4. cfg.setSqlConfiguration(sqlCfg);

    JDBC 驱动程序

    Ignite 附带 JDBC 驱动程序,允许使用标准 SQL 语句(如SELECTINSERTUPDATEDELETE直接从 JDBC 端处理分布式数据。

    目前,Ignite 支持两种驱动程序:本文档中描述的轻量级且易于使用的 JDBC Thin Driver 和通过客户端节点与集群交互的JDBC Client Driver 。

    驱动程序类的名称是org.apache.ignite.IgniteJdbcThinDriver。例如,您可以通过以下方式打开与侦听 IP 地址 192.168.0.50 的集群节点的 JDBC 连接:

    1. // Register JDBC driver.
    2. Class.forName("org.apache.ignite.IgniteJdbcThinDriver");
    3. // Open the JDBC connection.
    4. Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1");

    代码测试

    创建JavaBean

    1. import java.io.Serializable;
    2. import java.util.concurrent.atomic.AtomicLong;
    3. import org.apache.ignite.cache.affinity.AffinityKey;
    4. import org.apache.ignite.cache.query.annotations.QuerySqlField;
    5. import org.apache.ignite.cache.query.annotations.QueryTextField;
    6. /**
    7. * Person class.
    8. */
    9. public class PersonNew implements Serializable {
    10. /** */
    11. private static final AtomicLong ID_GEN = new AtomicLong();
    12. /** Name of index by two fields (orgId, salary). */
    13. public static final String ORG_SALARY_IDX = "ORG_SALARY_IDX";
    14. /** Person ID (indexed). */
    15. @QuerySqlField(index = true)
    16. public Long id;
    17. /** Organization ID (indexed). */
    18. @QuerySqlField(index = true, orderedGroups = @QuerySqlField.Group(name = ORG_SALARY_IDX, order = 0))
    19. public Long orgId;
    20. /** First name (not-indexed). */
    21. @QuerySqlField
    22. public String firstName;
    23. /** Last name (not indexed). */
    24. @QuerySqlField
    25. public String lastName;
    26. /** Resume text (create LUCENE-based TEXT index for this field). */
    27. @QueryTextField
    28. public String resume;
    29. /** Salary (indexed). */
    30. @QuerySqlField(index = true, orderedGroups = @QuerySqlField.Group(name = ORG_SALARY_IDX, order = 1))
    31. public double salary;
    32. /** Custom cache key to guarantee that person is always colocated with its organization. */
    33. private transient AffinityKey<Long> key;
    34. /**
    35. * Default constructor.
    36. */
    37. public PersonNew() {
    38. // No-op.
    39. }
    40. // /**
    41. // * Constructs person record.
    42. // *
    43. // * @param org Organization.
    44. // * @param firstName First name.
    45. // * @param lastName Last name.
    46. // * @param salary Salary.
    47. // * @param resume Resume text.
    48. // */
    49. // public Person(Organization org, String firstName, String lastName, double salary, String resume) {
    50. // // Generate unique ID for this person.
    51. // id = ID_GEN.incrementAndGet();
    52. //
    53. // orgId = org.id();
    54. //
    55. // this.firstName = firstName;
    56. // this.lastName = lastName;
    57. // this.salary = salary;
    58. // this.resume = resume;
    59. // }
    60. /**
    61. * Constructs person record.
    62. *
    63. * @param id Person ID.
    64. * @param orgId Organization ID.
    65. * @param firstName First name.
    66. * @param lastName Last name.
    67. * @param salary Salary.
    68. * @param resume Resume text.
    69. */
    70. public PersonNew(Long id, Long orgId, String firstName, String lastName, double salary, String resume) {
    71. this.id = id;
    72. this.orgId = orgId;
    73. this.firstName = firstName;
    74. this.lastName = lastName;
    75. this.salary = salary;
    76. this.resume = resume;
    77. }
    78. /**
    79. * Constructs person record.
    80. *
    81. * @param id Person ID.
    82. * @param firstName First name.
    83. * @param lastName Last name.
    84. */
    85. public PersonNew(Long id, String firstName, String lastName) {
    86. this.id = id;
    87. this.firstName = firstName;
    88. this.lastName = lastName;
    89. }
    90. /**
    91. * Gets cache affinity key. Since in some examples person needs to be collocated with organization, we create
    92. * custom affinity key to guarantee this collocation.
    93. *
    94. * @return Custom affinity key to guarantee that person is always collocated with organization.
    95. */
    96. public AffinityKey<Long> key() {
    97. if (key == null)
    98. key = new AffinityKey<>(id, orgId);
    99. return key;
    100. }
    101. /**
    102. * {@inheritDoc}
    103. */
    104. @Override public String toString() {
    105. return "Person [id=" + id +
    106. ", orgId=" + orgId +
    107. ", lastName=" + lastName +
    108. ", firstName=" + firstName +
    109. ", salary=" + salary +
    110. ", resume=" + resume + ']';
    111. }
    112. }

    创建测试类

    1. package ignite.sql;
    2. import org.apache.ignite.Ignite;
    3. import org.apache.ignite.IgniteCache;
    4. import org.apache.ignite.Ignition;
    5. import org.apache.ignite.cache.query.FieldsQueryCursor;
    6. import org.apache.ignite.cache.query.SqlFieldsQuery;
    7. import org.apache.ignite.configuration.CacheConfiguration;
    8. import org.apache.ignite.configuration.IgniteConfiguration;
    9. import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
    10. import org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder;
    11. import javax.cache.Cache;
    12. import java.util.ArrayList;
    13. import java.util.Iterator;
    14. import java.util.List;
    15. public class PersonBean {
    16. /**
    17. * Persons cache name.
    18. */
    19. private static final String PERSON_CACHE = PersonBean.class.getSimpleName() + "PersonNew";
    20. public static void main(String[] args) {
    21. // Preparing IgniteConfiguration using Java APIs
    22. IgniteConfiguration cfg = new IgniteConfiguration();
    23. // The node will be started as a client node.
    24. cfg.setClientMode(true);
    25. // Classes of custom Java logic will be transferred over the wire from this app.
    26. cfg.setPeerClassLoadingEnabled(true);
    27. // Setting up an IP Finder to ensure the client can locate the servers.
    28. TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
    29. ArrayList<String> strings = new ArrayList<>();
    30. strings.add("127.0.0.1:47500..47509");
    31. //ipFinder.setAddresses(Collections.singletonList("192.168.165.42:47500..47509"));
    32. ipFinder.setAddresses(strings);
    33. cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));
    34. try (Ignite ignite = Ignition.start(cfg)) {
    35. CacheConfiguration<Long, PersonNew> personCacheCfg = new CacheConfiguration<>(PERSON_CACHE);
    36. personCacheCfg.setIndexedTypes(Long.class, PersonNew.class);
    37. IgniteCache<Long, PersonNew> personCache = ignite.getOrCreateCache(personCacheCfg);
    38. // Insert persons.
    39. // SqlFieldsQuery qry = new SqlFieldsQuery(
    40. // "insert into PersonNew (_key, id, orgId, firstName, lastName, salary, resume) values (?, ?, ?, ?, ?, ?, ?)");
    41. //
    42. // personCache.query(qry.setArgs(1L, 1L, 1L, "John", "Doe", 4000.00, "Master"));
    43. // personCache.query(qry.setArgs(2L, 2L, 1L, "Jane", "Roe", 2000.00, "Bachelor"));
    44. // personCache.query(qry.setArgs(3L, 3L, 2L, "Mary", "Major", 5000.00, "Master"));
    45. // personCache.query(qry.setArgs(4L, 4L, 2L, "Richard", "Miles", 3000.00, "Bachelor"));
    46. PersonNew personNew = personCache.get(1L);
    47. System.out.println(personNew);
    48. PersonNew p2 = new PersonNew(5L, 5L, "city", "bob", 5000.00, "Master");
    49. personCache.put(5L, p2);
    50. SqlFieldsQuery query = new SqlFieldsQuery("SELECT * FROM PersonNew ");
    51. FieldsQueryCursor<List<?>> cursor = personCache.query(query);
    52. Iterator<List<?>> iterator = cursor.iterator();
    53. System.out.println("Query result:");
    54. while (iterator.hasNext()) {
    55. List<?> row = iterator.next();
    56. // System.out.println(">>> " + row.get(0) + ", " + row.get(1));
    57. //System.out.println(">>> " + row.get(0));
    58. System.out.println(row);
    59. }
    60. // Cache<Long, PersonNew> cache = Ignition.ignite().cache();
    61. // Create query which selects salaries based on range for all employees
    62. // that work for a certain company.
    63. // CacheQuery<Map.Entry<Long, Person>> qry = cache.queries().createSqlQuery(Person.class,
    64. // "from Person, Organization where Person.orgId = Organization.id " +
    65. // "and Organization.name = ? and Person.salary > ? and Person.salary <= ?");
    66. // Query all nodes to find all cached Ignite employees
    67. // with salaries less than 1000.
    68. //qry.execute("Ignition", 0, 1000);
    69. // Query only remote nodes to find all remotely cached Ignite employees
    70. // with salaries greater than 1000 and less than 2000.
    71. // qry.projection(grid.remoteProjection()).execute("Ignition", 1000, 2000);
    72. }
    73. }
    74. }

    通过DBeaver查看创建的表,默认会创建一个库。

    创建连接

     查看表结构

     

    查看表数据

     

  • 相关阅读:
    智云通CRM:让客户需要你,比你去寻找客户更重要?
    HDMI2.1 Redriver 信号增强 支持8K60
    Spring AOP
    软件测评中心进行安全测试有哪些流程?安全测试报告如何收费?
    Hbase之动态切换HMaster
    一文搞懂容器运行时 Containerd
    8个独立键盘驱动程
    Windows上安装pyenv,以及pyenv切换环境不生效的问题
    Qt 5中文乱码问题
    电脑办公助手之桌面便签,助力高效率办公
  • 原文地址:https://blog.csdn.net/wank1259162/article/details/125412035