博客又断了一段时间,本篇将记录一下基于ignite对jdbc支持的特性在实际使用过程当中的使用。html
因为spring-boot中支持经过spring.datasource.schema
属性指定初始化DDL脚本,spring.datasource.data
指定初始化DML脚本。而ignite支持jdbc协议,测试了一下,发现同样能够经过该配置初始化ignite。java
spring.datasource.url=jdbc:ignite:thin://127.0.0.1/ spring.datasource.driver-class-name=org.apache.ignite.IgniteJdbcThinDriver spring.datasource.schema=classpath:db/schema.sql spring.datasource.data=classpath:db/data.sql
说明ignite数据源一样能够做为一个DataSource
实例。git
建立表github
CREATE TABLE [IF NOT EXISTS] tableName (tableColumn [, tableColumn]... [, PRIMARY KEY (columnName [,columnName]...)]) [WITH "paramName=paramValue [,paramName=paramValue]..."]
WITH语法中支持的选项以及含义以下(可参见xml配置中CacheConfiguration
的相关配置):spring
参数 | 含义 |
---|---|
TEMPLATE | 缓存模式:PARTITIONED或者REPLICATED |
BACKUPS | 备份数量 |
ATOMICITY | 原子模式:ATOMIC或者TRANSACTIONAL |
CACHEGROUP | 缓存组名 |
AFFINITYKEY | 并置键列名 |
CACHE_NAME | 缓存名(若是不设置的话默认会加SQL_前缀) |
KEY_TYPE | 键类型 |
VALUE_TYPE | 值类型 |
DATA_REGION | 内存区名 |
建立索引sql
CREATE [SPATIAL] INDEX [[IF NOT EXISTS] indexName] ON tableName (columnName [ASC|DESC] [,...]) [(index_option [...])]
示例:apache
schema.sql
api
--student学生信息表 CREATE TABLE IF NOT EXISTS PUBLIC.STUDENT ( STUDID INTEGER, NAME VARCHAR, EMAIL VARCHAR, dob Date, PRIMARY KEY (STUDID)) WITH "template=replicated,atomicity=ATOMIC,cache_name=student"; CREATE INDEX IF NOT EXISTS STUDENT_NE_INDEX ON PUBLIC.STUDENT (NAME, EMAIL); -- grade成绩表 CREATE TABLE IF NOT EXISTS PUBLIC.GRADE ( STUDID INTEGER, grade DOUBLE, PRIMARY KEY (STUDID)) WITH "template=replicated,atomicity=ATOMIC,cache_name=grade";
ignite中dml
与标准sql中的基本一致示例以下:缓存
INSERT INTO student (studid, name, email, dob) VALUES (1, 'student_1', 'student_1gmail.com', '2017-09-28');
完整dml初始化脚本:bash
-- student INSERT INTO student (studid, name, email, dob) VALUES (1, 'student_1', 'student_1gmail.com', '2017-09-28'); INSERT INTO student (studid, name, email, dob) VALUES (2, 'student_2', 'student_2gmail.com', '2017-09-28'); ... --grade INSERT INTO grade (studid, grade) VALUES (1, 3); INSERT INTO grade (studid, grade) VALUES (2, 64); ...
<u>注:若是使用了KEY_TYPE
选项配置自定义KEY类型,那么要么在自定义类型中得有对应的构造方法,要么使用java内置类型的时候,insert语句必须带上_key字段的值。</u>
例如: ddl语句中with选项为
WITH "template=replicated,atomicity=ATOMIC,cache_name=student,key_type=java.lang.Long";
那么dml语句就得像下面这么写
INSERT INTO student (_key, studid, name, email, dob) VALUES (1, '1', 'student_1', 'student_1gmail.com', '2017-09-28');
初始化完成后可经过监控程序看到以下监控状况 :
能够发现缓存和数据均已初始化成功。
因为ignite能够做为DataSource
的实例,因此猜测应该也能够经过Mybatis去查询ignite,这样能够替代原来须要SqlFieldsQuery
查询并对结果进行逐行解析的方式。经验证后发现ignite能完美支持myabtis,因此在查询ignite的方式上有了一个更便捷的方式。
与普通使用mybatis的方式同样,定义IgniteMapper.xml
和IgniteMapper.java
IgniteMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.cord.ignite.data.mapper.IgniteMapper"> <resultMap type="org.cord.ignite.data.domain.Student" id="StudentResult"> <result property="studId" column="studid"/> <result property="name" column="name"/> <result property="email" column="email"/> <result property="dob" column="dob"/> </resultMap> <select id="findStudentsById" parameterType="java.lang.String" resultMap="StudentResult"> SELECT * FROM student WHERE studid = #{studentId} </select> <select id="findGradeByName" parameterType="java.lang.String" resultType="java.lang.Double"> SELECT g.grade FROM student s,grade g WHERE s.STUDID=g.STUDID and s.name= #{name} </select> </mapper>
IgniteMapper.java
public interface IgniteMapper { /** * 根据studentId查询学生信息 * @param studentId * @return Student */ Student findStudentsById(String studentId); /** * 根据学生姓名查询学生分数 * @param name * @return 学生分数 */ Double findGradeByName(String name); }
查询:
... @Autowired private IgniteMapper igniteMapper; ... Student student = igniteMapper.findStudentsById(studentId); ... double grade = igniteMapper.findGradeByName(name);
<u>注:因为ignite中能够自定义sql函数,测试过,在mybatis中ignite的自定义sql函数一样支持。</u>
因为ignite中jdbc的方式属于轻客户端,因此性能要比api的方式差,而在经过mybatis查询的方式其性能表现经过测试得出的结果以下:
在相同的sql相同数据的状况下,100并发查询:
/findStudentsById 耗时 [13]ms. /findStudentsById 耗时 [9]ms. /findStudentsById 耗时 [3]ms. /findStudentsById 耗时 [10]ms. /findStudentsById 耗时 [11]ms. /findStudentsById 耗时 [11]ms. /findStudentsById 耗时 [13]ms. /findStudentsById 耗时 [8]ms. /findStudentsById 耗时 [8]ms. /findStudentsById 耗时 [14]ms. /findStudentsById 耗时 [17]ms. /findStudentsById 耗时 [11]ms. /findStudentsById 耗时 [8]ms. /findStudentsById 耗时 [13]ms. /findStudentsById 耗时 [11]ms. /findStudentsById 耗时 [10]ms. /findStudentsById 耗时 [9]ms. /findStudentsById 耗时 [10]ms. /findStudentsById 耗时 [12]ms. /findStudentsById 耗时 [9]ms. /findStudentsById 耗时 [3]ms. /findStudentsById 耗时 [3]ms. ... /findStudentsById 耗时 [1]ms. /findStudentsById 耗时 [2]ms. /findStudentsById 耗时 [2]ms. /findStudentsById 耗时 [2]ms. /findStudentsById 耗时 [2]ms. /findStudentsById 耗时 [2]ms. /findStudentsById 耗时 [2]ms. /findStudentsById 耗时 [2]ms. /findStudentsById 耗时 [1]ms. /findStudentsById 耗时 [1]ms. /findStudentsById 耗时 [1]ms. /findStudentsById 耗时 [0]ms.
吞吐量为537/sec
, 性能有波动状况,稳定后在2ms左右。
/cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [1]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [1]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms. /cpFindStudentsById 耗时 [0]ms.
吞吐量为1256/sec
,性能比较稳定,稳定后在1ms之内。
完整代码请参考:https://github.com/cording/ignite-example
对于不是要求极限性能的场景,mybatis查询方式彻底能知足,这使得对于不少现有基于myabtis的项目代码,能相对平滑的使用ignite做为加速层,而又不用作过多改动。
还有,写博客确实有点费时间....................
原文出处:https://www.cnblogs.com/cord/p/10293813.html