oracle cdc机制异步模式hotlog使用讲解
大致流程:
发布者配置:准备改变表、创建改变集、创建改变表、激活改变集、将改变表授权给订阅者
订阅者配置:创建订阅集、创建订阅表、激活订阅表、更新或情况扩展窗口、查询扩展窗口上的数据(可查询增量数据)
sys用户操作:
shutdown immediate;
startup mount;
alter database archivelog;
alter database open;
archive log list;
是归档模式后就行了
GRANT CREATE TABLE TO scott;
GRANT CREATE TABLESPACE TO scott;
GRANT UNLIMITED TABLESPACE TO scott;
GRANT SELECT_CATALOG_ROLE TO scott;
GRANT EXECUTE_CATALOG_ROLE TO scott;
GRANT EXECUTE ON DBMS_CDC_PUBLISH TO scott;//system权限不足,需要sys
GRANT CREATE SESSION TO scott;
GRANT CREATE SEQUENCE TO scott;
GRANT DBA TO scott;
//流程中可能执行多次的
create table dog(
id number,
name varchar2(20)
);
//为了方便创建一个同样结构的表
create table cat(
id number,
name varchar2(20)
);
insert into dog values(1,'xx');
insert into dog values(2,'hh');
select * from dog;
insert into cat values(1,'xx');
insert into cat values(2,'hh');
select * from cat;
create user sub identified by sub;
grant create session to sub;
//流程中可能执行多次的
grant all on dog to /发布者/;
grant all on dog to sub;
grant all on cat to /发布者/;
grant all on cat to sub;
BEGIN
DBMS_CAPTURE_ADM.PREPARE_TABLE_INSTANTIATION(
TABLE_NAME => 'scott.dog');
END;
/
BEGIN
DBMS_CAPTURE_ADM.PREPARE_TABLE_INSTANTIATION(
TABLE_NAME => 'scott.cat');
END;
/
scott(发布者):
//创建改变集
BEGIN
DBMS_CDC_PUBLISH.CREATE_CHANGE_SET(
change_set_name => 'test2_cs',
description => 'Change set for scott.table info',
change_source_name => 'HOTLOG_SOURCE');
END;
/
/*删除改变集*/
Begin
Dbms_cdc_publish.drop_change_set(
Change_set_name => 'test_cs');
End;
/
//在改变集中加入改变表1
BEGIN
DBMS_CDC_PUBLISH.CREATE_CHANGE_TABLE(
owner => 'scott',
change_table_name => 'dog_ct',
change_set_name => 'test2_cs',
source_schema => 'scott',
source_table => 'dog',
column_type_list => 'id number,name varchar(20) ',
capture_values => 'both',
rs_id => 'y',
row_id => 'n',
user_id => 'n',
timestamp => 'n',
object_id => 'n',
source_colmap => 'n',
target_colmap => 'y',
options_string => 'tablespace users');
END;
/
//在改变集中加入改变表2
BEGIN
DBMS_CDC_PUBLISH.CREATE_CHANGE_TABLE(
owner => 'scott',
change_table_name => 'cat_ct',
change_set_name => 'test2_cs',
source_schema => 'scott',
source_table => 'cat',
column_type_list => 'id number,name varchar(20) ',
capture_values => 'both',
rs_id => 'y',
row_id => 'n',
user_id => 'n',
timestamp => 'n',
object_id => 'n',
source_colmap => 'n',
target_colmap => 'y',
options_string => 'tablespace users');
END;
/
//删除改变表
begin
DBMS_CDC_PUBLISH.DROP_CHANGE_TABLE(
owner => 'scott',
change_table_name => 'person_ct',
force_flag => 'y');
end;
//激活改变集
BEGIN
DBMS_CDC_PUBLISH.ALTER_CHANGE_SET(
change_set_name => 'test2_cs',
enable_capture => 'y');
END;
/
grant select on system.person to sub;
grant select on scott.dog_ct to sub;//至关重要的一步
grant select on scott.cat_ct to sub;
=======================================
insert into scott.dog values(3,'mm');
commit;
此时sub已经可查询到:
select t.operation$,t.commit_timestamp$,t.id,t.name from scott.dog_ct t;
但是为了增量数据查询,需要实现以下步骤:
=======================================
sub:
//创建订阅集
BEGIN
dbms_cdc_subscribe.create_subscription(
change_set_name=>'test2_cs',
description=>'for scott.dog',
subscription_name=>'dog_sub');
END;
/
//订阅改变集中的改变表1
BEGIN
DBMS_CDC_SUBSCRIBE.SUBSCRIBE(
subscription_name => 'dog_sub',
source_schema => 'scott',
source_table => 'dog',
column_list => 'id,name',
subscriber_view => 'dog_view');
END;
/
//订阅改变集中的改变表2(按该执行顺序执行时会报错:不能修改活动的订阅集)
//解决方案1:删除订阅集,重新创建订阅集,添加改变表1和改变表2,然后继续以下的流程。
//解决方案2:查阅官方文档,查看是否有取消激活订阅集。
BEGIN
DBMS_CDC_SUBSCRIBE.SUBSCRIBE(
subscription_name => 'dog_sub',
source_schema => 'scott',
source_table => 'cat',
column_list => 'id,name',
subscriber_view => 'cat_view');
END;
/
//激活订阅集
Begin
dbms_cdc_subscribe.activate_subscription(subscription_name => 'dog_sub');
END;
/
//清空扩展窗口
BEGIN
DBMS_CDC_SUBSCRIBE.PURGE_WINDOW(
subscription_name => 'dog_SUB');
END;
/
//更新扩展窗口
BEGIN
DBMS_CDC_SUBSCRIBE.EXTEND_WINDOW(
subscription_name => 'dog_sub');
END;
/
//查询表的变化情况 查询dog_view(可以操作表,实现查看增量数据)或者直接查询scott.dog_ct
select t.operation$,t.commit_timestamp$,t.id,t.name from dog_view t;
//删除发布的数据实际上就是删除提供给订阅者能访问的订阅表,即处理掉dog_ct。
实验:
1.如果表名改变,不会报错,但抓不到更新内容,表名更改后的修改数据不会捕捉,只能捕捉到表名为设定发布订阅表中的表名。
2.如果表结构改变,不会报错,能抓到更新内容,但只能抓到发布订阅设定中的表结构内容,若表不存在设置列,填空。