一、迁移工具介绍
1、数据迁移工具
金仓支持应用系统离线迁移、在线迁移。
- 离线迁移(业务停机)一般使用 KDTS 完成全量迁移(所有对象定义和数据的迁移);
- 在线迁移(业务不停机)使用 KFS,实现全量数据搬迁、增量数据同步,将源数据库的对象定义和数据搬迁到目的数据库。
| 工具特性对比 | KDTS | KFS |
|---|---|---|
| 是否支持迁移存储过程等对象定义迁移 | 支持所有对象定义的迁移 | 支持表结构和主键的定义的迁移 |
| 同步一次,还是持续同步 | 一次同步 | 持续同步 |
| 是否需要初始状态同步 | 不需要 | 需要 |
| 跨平台 | 支持 | 支持 |
| 迁移部分表 | 支持 | 支持 |
| 迁移一个表的部分数据 | 支持 | 支持 |
| 断点续传 | 不支持 | 支持 |
2、调试工具
- 命令行的ksql
- 图形化的KStudio
| 工具特性对比 | ksql | Kstudio |
|---|---|---|
| 执行 SQL | 支持 | 支持 |
| 直接编辑数据 | 需要写 SQL 语句 | 支持,通过图形界面直接编辑 |
| 执行存储过程 | 支持 | 支持 |
| 查看 SQL 执行计划 | 支持 | 支持 |
| 调试 plsql | 支持 | 支持 |
| 数据库状态分析 | 不支持 | 不支持 |
| 慢 SQL 分析 | 不支持 | 不支持 |
3、数据同步工具
- 自带的物理同步
- KFS
| 工具特性对比 | 物理同步功能 | KFS |
|---|---|---|
| 跨 CPU | 支持 | 支持 |
| 断点续传 | 支持 | 支持 |
| 同步部分对象 | 不支持 | 支持 |
| 同步所有操作 | 支持 | 支持 |
- 自带物理同步,用于两套 KingbaseES 之间同步,以整个 data 目录为单位,无法单独同步部分对象;可同步全部用户操作,支持断点续传与事务重放。
- KFS 用于 V8R3 与 V8R6 跨版本同步,支持筛选部分对象,同步粒度更灵活。业务割接需双轨运行时,可按需选用物理同步或 KFS。
4、备份工具
- 逻辑备份:sys_dump
- 物理备份:sys_rman
| 工具特性对比 | sys_dump | sys_rman |
|---|---|---|
| 异构数据源 | 不支持 | 不支持 |
| 支持增量 | 不支持 | 支持 |
| 支持根据日志恢复到指定时间点 | 不支持 | 支持 |
| 备份单个表 | 支持 | 不支持 |
| 可以不备份指定表 | 支持 | 不支持 |
- sys_dump 是逻辑备份,对象选择灵活、支持单表备份,无增量能力,不能配合 redo 日志;
- sys_rman 为物理备份,整实例备份,不支持单表,支持增量。
二、支持产品能力
1、模式对象和数据类型
模式对象
| 序号 | 模式下对象 | 创建示例 SQL |
|---|---|---|
| 1 | 表和分区 | create table t1(id int) partition by range(id); |
| 2 | 视图(包含 force view) | create force view v1 as select * from t1; |
| 3 | 可更新视图 | create view v_update as select id from t1; |
| 4 | 触发器 | create trigger tr1 before insert on t1 for each row execute function func_test(); |
| 5 | 规则 | create rule r1 as on insert to t1 do nothing; |
| 6 | 约束 | alter table t1 add constraint pk_t1 primary key(id); |
| 7 | 索引 | create index idx_t1 on t1(id); |
| 8 | 数据库连接 dblink | create extension dblink; create database link link1 connect to system using 'host=127.0.0.1 port=5432 dbname=test'; |
| 9 | 函数 | create function func_test() returns void as $$ begin end; $$ language plsql; |
| 10 | 存储过程 | create procedure proc_test() as $$ begin end; $$ language plsql; |
| 11 | 操作符 | create operator +> (leftarg=int, rightarg=int, procedure=int4add); |
| 12 | 包 | create package pkg_test is procedure p1; end pkg_test; |
| 13 | 序列 | create sequence seq1 start with 1; |
| 14 | 同义词 | create synonym syn_t1 for t1; |
| 15 | 外部数据源 | create foreign data wrapper fdw_test; create server srv1 foreign data wrapper fdw_test; |
| 16 | 数据类型 | create type typ1 as (col1 int, col2 text); |
| 17 | 标记和策略 | alter table t1 enable row level security; create policy pol1 on t1 for select using(true); |
| 18 | 域 domain | create domain domain_int as int check(value>0); |
| 19 | 扩展包 | create extension pg_stat_statements; |
| 20 | 全局临时表 | create global temporary table gtt1(id int) on commit preserve rows; |
| 21 | DUAL 表 | 内置,无需创建,查询示例:select 1 from dual; |
| 22 | ROWNUM, LEVEL 等伪列 | 内置,无需创建,查询示例:select rownum,level from t1 connect by level<=10; |
| 23 | 匿名块 | do $$ begin raise notice 'hello'; end $$; |
| 24 | 表达式,包括条件表达式等 | 内置,无需创建,示例:select case when id>1 then 1 else 0 end from t1; |
数据类型
| 序号 | 分类 | KingbaseES 数据类型 | 作用 | 建表示例 SQL |
|---|---|---|---|---|
| 1 | 数值型 | smallint | 短整型,占用 2 字节,存储小范围整数 | create table t(id smallint); |
| 2 | 数值型 | integer | 整型,占用 4 字节,常用整数类型 | create table t(id integer); |
| 3 | 数值型 | bigint | 长整型,占用 8 字节,存储大范围整数 | create table t(id bigint); |
| 4 | 数值型 | decimal | 定点高精度数值,适合金额财务数据 | create table t(val decimal(10,2)); |
| 5 | 数值型 | numeric | 和 decimal 等价,任意精度小数,无精度丢失 | create table t(val numeric(10,2)); |
| 6 | 数值型 | number | Oracle 兼容定点数,支持精度与刻度定义 | create table t(val number(10,2)); |
| 7 | 数值型 | real | 单精度浮点数,4 字节,存在精度损失 | create table t(val real); |
| 8 | 数值型 | float | 浮点数,可指定精度,Oracle 兼容浮点类型 | create table t(val float); |
| 9 | 数值型 | double precision | 双精度浮点数,8 字节 | create table t(val double precision); |
| 10 | 数值型 | double | 等价 double precision,双精度浮点简写形式 | create table t(val double); |
| 11 | 数值型 | smallserial | 自增短整型,自动生成递增序列号 | create table t(id smallserial); |
| 12 | 数值型 | serial | 自增整型,常用主键自增类型 | create table t(id serial); |
| 13 | 数值型 | bigserial | 自增长整型,适合海量数据主键自增 | create table t(id bigserial); |
| 14 | 字符型 | character(nchar|byte), char(nchar|byte) | 定长字符,不足长度自动补空格 | create table t(name char(10)); |
| 15 | 字符型 | character varying(nchar|byte), varchar(nchar|byte), varchar2(nchar|byte) | 变长字符,varchar2 为 oracle 兼容变长字符串 | create table t(name varchar2(50)); |
| 16 | 字符型 | nvarchar(nchar|byte) | unicode 变长字符,存储多语言中文 | create table t(name nvarchar(50)); |
| 17 | 字符型 | text | 无上限变长文本,存储大段文字内容 | create table t(content text); |
| 18 | 大对象型 | clob, nclob | 字符大对象,oracle 兼容,存储超大文本 | create table t(big_text clob); |
| 19 | 大对象型 | blob | 二进制大对象,存储文件、图片二进制流 | create table t(big_data blob); |
| 20 | 日期时间型 | timestamp § without time zone | 不带时区时间戳,保存年月日时分秒 | create table t(ts timestamp); |
| 21 | 日期时间型 | timestamp § with time zone | 带时区时间戳,跨时区业务使用 | create table t(ts timestamptz); |
| 22 | 日期时间型 | date | 仅保存年月日日期,不含时分秒 | create table t(dt date); |
| 23 | 日期时间型 | time § without time zone | 不带时区时间,仅存时分秒 | create table t(tm time); |
| 24 | 日期时间型 | time § with time zone | 带时区的时分秒时间类型 | create table t(tm timetz); |
| 25 | 日期时间型 | interval year | 时间间隔,以年为单位 | create table t(itv interval year); |
| 26 | 日期时间型 | interval month | 时间间隔,以月为单位 | create table t(itv interval month); |
| 27 | 日期时间型 | interval day | 时间间隔,以天为单位 | create table t(itv interval day); |
| 28 | 日期时间型 | interval hour | 时间间隔,以小时为单位 | create table t(itv interval hour); |
| 29 | 日期时间型 | interval minute | 时间间隔,以分钟为单位 | create table t(itv interval minute); |
| 30 | 日期时间型 | interval second | 时间间隔,以秒为单位 | create table t(itv interval second); |
| 31 | 日期时间型 | interval year to month | 时间间隔,年 + 月组合 | create table t(itv interval year to month); |
| 32 | 日期时间型 | interval day to second | 时间间隔,天 + 时 + 分 + 秒组合 | create table t(itv interval day to second); |
| 33 | 布尔型 | boolean | 布尔类型,取值 true/false/null | create table t(flag boolean); |
| 34 | 二进制型 | bit(n) | 定长二进制位串,存储比特数据 | create table t(bits bit(8)); |
| 35 | 二进制型 | bit varying(n) | 变长二进制位串 | create table t(bits bit varying(100)); |
| 36 | XML 型 | xml | xml 数据专用类型,支持 xml 解析查询 | create table t(xml_data xml); |
| 37 | 数组型 | array | 数组类型,一列存储多个同类型元素 | create table t(arr int[]); |
| 38 | 枚举型 | enum | 自定义枚举,限定字段只能取预设值 | create type color as enum('red','blue'); create table t(c color); |
| 39 | JSON 型 | json | json 文本存储,原始 json 字符串 | create table t(j_data json); |
| 40 | JSON 型 | jsonb | 二进制 json,支持索引,查询性能更高 | create table t(j_data jsonb); |
| 41 | UUID 型 | uuid | 全局唯一 id,分布式场景主键 | create table t(uid uuid); |
| 42 | GIS 型 | point | 空间点坐标类型,地理信息 | create table t(geo point); |
| 43 | GIS 型 | line | 空间线段类型 | create table t(geo line); |
| 44 | GIS 型 | box | 空间矩形框类型 | create table t(geo box); |
| 45 | GIS 型 | path | 空间路径类型 | create table t(geo path); |
| 46 | GIS 型 | circle | 空间圆形类型 | create table t(geo circle); |
| 47 | GIS 型 | polygon | 空间多边形类型 | create table t(geo polygon); |
| 48 | GIS 型 | geometry | 通用空间几何类型,postgis 扩展使用 | create table t(geo geometry); |
2、SQL
| 分类 | 语法项 | 作用 | 示例 sql |
|---|---|---|---|
| 数据操作 | insert | update | delete | 新增、修改、删除表内数据 | insert into t1(id) values(1); update t1 set id=2 where id=1; delete from t1 where id=2; |
| 数据操作 | truncate | 快速清空整张表数据,不记录单行日志 | truncate table t1; |
| 数据操作 | merge into | 合并数据,匹配则更新、不匹配则插入(upsert) | merge into t1 using t2 on (t1.id = t2.id) when matched then update set name=t2.name when not matched then insert(id,name) values(t2.id,t2.name); |
| 数据操作 | insert first/all | 多条件分支插入数据,first 匹配第一条即终止;all 匹配全部条件 | insert first when id<10 then into t1(id) values(id) else into t2(id) values(id) select id from t_src; |
| 数据操作 | copy | 批量导入 / 导出表数据,适合大批量文件数据交换 | copy t1 from '/data/file.csv' with (format csv); |
| 数据操作 | cluster | 根据索引物理重排表数据,使数据按索引有序存储 | cluster t1 using idx_t1; |
| 数据查询 | select | 查询数据表中的数据 | select id,name from t1 where id>1; |
| 数据查询 | 层次查询 connect by | 树形、层级递归查询(oracle 风格层级查询) | select id,level from t1 connect by prior id = pid start with id=1; |
| 数据查询 | lateral 子句 | 支持关联前面表的字段,执行子查询 | select * from t1 left join lateral (select name from t2 where t2.t1_id = t1.id limit 1) sub on true; |
| 数据查询 | 外连接操作符 (+) | oracle 风格简写外连接语法,标记一侧为驱动表的附属表 | select * from t1,t2 where t1.id = t2.t1_id(+); |
| 数据查询 | with recursive 子句 | cte 公用表表达式,recursive 支持递归查询 | with recursive cte as (select 1 as n union all select n+1 from cte where n<10) select * from cte; |
| 数据查询 | prepare, execute | 预编译 sql,一次编译多次执行,提升重复查询性能 | prepare p1(int) as select * from t1 where id=$1; execute p1(1); |
| 数据查询 | values 子句 | 构造临时数据集,可直接用于查询、关联 | select * from values (1,'a'),(2,'b') as tmp(id,name); |
| 数据查询 | explain | 查看 sql 执行计划,用于 sql 性能分析调优 | explain select * from t1 where id>1; |
| 数据整理 | analyze | 收集表、索引统计信息,供优化器生成执行计划 | analyze t1; |
| 数据整理 | vacuum | 清理表过期元组,回收存储空间,更新可见性映射 | vacuum t1; |
| 数据整理 | reindex | 重建索引,修复索引碎片、损坏索引 | reindex table t1; |
| 事务控制语句 | begin, start transaction | 开启数据库事务 | begin; |
| 事务控制语句 | commit | 提交事务,持久化事务内所有修改 | commit; |
| 事务控制语句 | rollback | 回滚事务,撤销事务内所有未提交修改 | rollback; |
| 事务控制语句 | savepoint | release savepoint | rollback to savepoint | 设置保存点,支持局部回滚到指定节点,不回滚整个事务 | begin; savepoint sp1; insert into t1 values(1); rollback to savepoint sp1; release savepoint sp1; commit; |
| 事务控制语句 | set transaction { read only | read write } | 设置事务读写属性,只读事务禁止 dml 修改 | set transaction read only; |
| 事务控制语句 | prepare transaction | 两阶段事务提交,分布式事务预提交 | prepare transaction 'tx_001'; |
| 权限语句 | grant | 授予用户对象权限或系统权限 | grant select on t1 to user1; |
| 权限语句 | revoke | 回收已经授予用户的权限 | revoke select on t1 from user1; |
3、PL/SQL
| 序号 | 分类 | 语法项 | 作用 | 示例 sql |
|---|---|---|---|---|
| 1 | PL/SQL 基础语句 | 赋值语句 | 给变量赋予指定值,支持变量之间赋值 | declare v_num integer; begin v_num := 10; end; |
| 2 | PL/SQL 基础语句 | if-then-else 语句 | 条件分支判断,满足条件执行对应代码块 | declare v_num integer:=5; begin if v_num>0 then raise notice '大于0'; else raise notice '小于等于0'; end if; end; |
| 3 | PL/SQL 基础语句 | case 语句 | 多分支条件判断,简化多 if 嵌套场景 | declare v_type integer:=1; begin case v_type when 1 then raise notice '类型1'; when 2 then raise notice '类型2'; end case; end; |
| 4 | 循环语句 | loop、while-loop、for loop | 循环执行代码块;loop 无条件循环,while 按条件循环,for 遍历集合循环 | declare v_i integer:=1; begin loop raise notice '%',v_i; v_i:=v_i+1; exit when v_i>3; end loop; end; |
| 5 | 变量属性 | % type 属性和 % rowtype 属性 | % type 引用表字段类型;% rowtype 直接引用表一行记录结构 | declare v_name t1.name%type; v_row t1%rowtype; begin select name into v_name from t1 limit 1; end; |
| 6 | 游标 | ref cursor 游标 | 动态游标,运行时绑定查询,可作为参数传递 | declare type ref_cur is ref cursor; cur ref_cur; v_id integer; begin open cur for select id from t1; fetch cur into v_id; close cur; end; |
| 7 | 异常处理 | 预定义异常和自定义异常 | 捕获程序运行错误,可自定义业务异常,异常发生时执行容错逻辑 | declare exp_custom exception; begin raise exp_custom; exception when exp_custom then raise notice '捕获自定义异常'; end; |
| 8 | 游标属性 | % notfound、% found、% isopen 和 % rowcount | 游标状态属性:% found 是否读到行;% notfound 无数据;% isopen 游标是否打开;% rowcount 返回已读取行数 | declare cur cursor for select id from t1; v_id integer; begin open cur; fetch cur into v_id; if cur%found then raise notice '有数据'; end if; close cur; end; |
| 9 | DML 扩展 | returning into 语句 | dml 执行后,直接把修改的数据返回存入变量 | declare v_id integer; begin insert into t1(name) values('test') returning id into v_id; end; |
| 10 | 动态 SQL | execute immediate 语句 | 执行动态拼接的 sql 字符串,支持动态表名、条件 | declare v_sql text; begin v_sql:='select count(*) from t1'; execute immediate v_sql; end; |
| 11 | 批量处理 | bulk collect | 批量读取查询结果,一次性把多行数据存入集合,减少循环开销 | declare type t_arr is table of t1.id%type; v_arr t_arr; begin select id bulk collect into v_arr from t1; end; |
| 12 | 消息输出 | raise 语句 | 打印提示消息、抛出异常,用于调试输出信息 | begin raise notice 'hello plsql'; end; |
| 13 | 集合类型 | PL/SQL 集合类型(关联数组、嵌套表、可变数组) | 存储一组同类型数据,类似数组,用于批量数据处理 | declare type nt is table of integer; v_nt nt:=nt(1,2,3); begin raise notice '%',v_nt(1); end; |
| 14 | PL/SQL 对象 | 触发器(行级触发器,列级触发器,事件触发器) | 表发生增删改时自动执行代码,实现数据约束、审计 | create trigger tr_t1 before insert on t1 for each row begin raise notice '触发行级触发器'; end; |
| 15 | PL/SQL 对象 | 函数,嵌套函数 | 有返回值,可在 sql 中调用;嵌套函数在函数内部定义子函数 | create function f_get_id() returns integer as $$ begin return 1; end; $$ language plsql; |
| 16 | PL/SQL 对象 | 存储过程 | 封装业务逻辑,无返回值,用于复杂事务处理 | create procedure proc_insert() as $$ begin insert into t1 values(1); end; $$ language plsql; |
| 17 | PL/SQL 对象 | 匿名块 | 临时执行的 PLSQL 代码,不持久化存储,一次性脚本 | declare v_msg text; begin v_msg:='test'; raise notice '%',v_msg; end; |
| 18 | PL/SQL 对象 | package | 程序包,把过程、函数、变量封装成一个模块,管理一组相关逻辑 | create package pkg_test as procedure p_test(); end pkg_test; |
| 19 | PL/SQL 对象 | object type | 自定义对象类型,可定义属性与成员方法,面向对象特性 | create or replace type obj_test as object(id integer,name varchar2(10)); |
4、客户端编程
接口编程
| 序号 | 编程语言 | KingbaseES 对应支持 |
|---|---|---|
| 1 | JAVA | JDBC |
| 2 | .Net | .Net Data Provider |
| 3 | C/C++ | Pro*C |
| 4 | C/C++ | DCI |
| 5 | ODBC | |
| 6 | OCCI | |
| 7 | Php | pdo |
| 8 | Perl | pdi |
| 9 | Python | ksycopg2 |
| 10 | golang | gokb |
| 11 | Node.js | kb |
开发框架
| 序号 | 开发框架 | KingbaseES 对应支持 | 简介 |
|---|---|---|---|
| 1 | Hibernate | 支持 | Java 老牌 ORM 持久化框架,将对象映射到数据库表,屏蔽原生 SQL,简化数据库操作 |
| 2 | Activiti | 支持 | Java 工作流引擎,用于业务流程审批(工单、流程流转),可将流程数据存入金仓 |
| 3 | Mybatis | 支持 | Java 主流 ORM 框架,半自动映射,自定义 SQL 灵活,国内项目使用最多 |
| 4 | Mybatis-Plus | 支持 | 基于 Mybatis 增强封装,自带 CRUD、分页、条件构造器,减少重复基础 SQL 代码 |
| 5 | Flyway | 支持 | 数据库版本迁移工具,管理 SQL 脚本版本,自动升级 / 降级数据库结构 |
| 6 | Liquibase | 支持 | 数据库变更管理工具,多数据库兼容,支持 XML/YAML/SQL 格式记录表结构变更 |
| 7 | Dbunit | 支持 | 数据库单元测试工具,测试前后自动导入 / 清理测试数据,用于单元测试 |
| 8 | Hibernate-Spatial | 支持 | Hibernate 空间扩展框架,用于地理空间数据的对象映射、空间查询 |
| 9 | Qt | 支持 | C++ 跨平台 GUI 开发框架,内置数据库模块,可连接金仓做桌面客户端开发 |
| 10 | Ef6 | 支持 | .NET Framework 的 ORM 框架(Entity Framework 6),C# 对象和数据库映射 |
| 11 | Efcore | 支持 | 跨平台新版 Entity Framework,.NET Core/.NET5 + 的 ORM,.NET 项目主流 |
| 12 | Django | 支持 | Python 全栈 Web 框架,自带 ORM 模块,快速开发 Web 业务系统 |
| 13 | SQLAlchemy | 支持 | Python 成熟 ORM 框架,灵活度高,Python 后端常用数据库对象映射工具 |
连接池
| 序号 | 连接池 | KingbaseES 对应支持 | 简介 |
|---|---|---|---|
| 1 | C3P0 | 支持 | Java 老牌数据库连接池,管理数据库连接复用,防止频繁创建销毁连接,老项目常用 |
| 2 | DBCP | 支持 | Apache 出品的 Java 连接池,稳定可靠,早期传统项目广泛使用 |
| 3 | Druid | 支持 | 阿里开源 Java 连接池,自带监控、防 SQL 注入,国内 Java 项目使用非常普遍 |
| 4 | HikariCP | 支持 | 高性能轻量级 Java 连接池,SpringBoot 默认连接池,速度快、开销小 |