创建临时表空间=====================================
create temporary tablespace test_temp
tempfile 'e:\oracle\oradata\cdctest\test_temp01.dbf'
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;
create tablespace test_data
logging
datafile 'e:\oracle\oradata\cdctest\test_data01.dbf'
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;
create user appuser identified by appuser
default tablespace test_data
temporary tablespace test_temp;
grant connect,resource, create view to appuser
create user cdc_pub identified by cdc
default tablespace test_data
temporary tablespace test_temp;
给用户授予权限=======================================
grant connect,resource to cdc_pub -- connect 连接权限,resource用于给开发人员用的角色
grant SELECT_CATALOG_ROLE TO cdc_pub --可以查看一些数据字典的视图·
GRANT EXECUTE_CATALOG_ROLE TO cdc_pub--执行目录角色,能够执行所有系统包
GRANT EXECUTE ON DBMS_CDC_PUBLISH TO cdc_pub--用于定义发布操作
grant create job to cdc_pub --创建任务
create user cdc_sub identified by cdc
default tablespace test_data
temporary tablespace test_temp;
grant connect,resource to cdc_sub -- connect 连接权限,resource用于给开发人员用的角色
grant execute on DBMS_CDC_SUBSCRIBE TO CDC_SUB --用于定义订阅操作
2.创建业务表:以业务用户账户(APPUSER)登录
create table SalesOrder(
orderId int not null,
customerId int not null,
duedate date not null,
deliverTo int not null,
createddttm date default sysdate,
constraint pk_salesOrder primary key (orderId)
)
select * from salesorder
create table salesorderdetail(
solineId int not null,
orderId int not null,
itemNumber varchar2(20) not null,
quantity decimal(13,4),
linePrice decimal(13,4),
constraint pk_sodetail primary key (solineId)
)
select * from salesorderdetail
BEGIN
DBMS_CDC_PUBLISH.CREATE_CHANGE_SET(
change_set_name => 'CDC_SET_SO', --改变集
description => 'Change set for SalesOrder, SalesOrderDetail',
change_source_name => 'SYNC_SOURCE');
END;
BEGIN
DBMS_CDC_PUBLISH.CREATE_CHANGE_TABLE(
DDL_MARKERS=>'n',
owner => 'cdc_pub', --发布表的Owner!
change_table_name => 'CT_SalesOrder', --发布表名
change_set_name => 'CDC_SET_SO', --改变集
source_schema => 'appuser', --业务表的Owner
source_table => 'SalesOrder', --业务表
column_type_list => 'OrderID int, CustomerID int, DueDate Date, DeliverTo int,
CreateDTTM Date', --发布表中的列定义
capture_values => 'new', -- 获取更改的值
rs_id => 'n',
row_id => 'n',
user_id => 'n',
timestamp => 'n',
object_id => 'n',
source_colmap => 'n',
target_colmap => 'n',
options_string => null );
END;
BEGIN
DBMS_CDC_PUBLISH.CREATE_CHANGE_TABLE(
DDL_MARKERS=>'n',
owner => 'cdc_pub',
change_table_name => 'CT_SalesOrderDetail',
change_set_name => 'CDC_SET_SO',
source_schema => 'appuser',
source_table => 'SalesOrderDetail',
column_type_list => 'SOLineID int, OrderID int, ItemNumber
varchar2(20), Quantity decimal(13,4), LinePrice decimal(18,4)',
capture_values => 'new',
rs_id => 'n',
row_id => 'n',
user_id => 'n',
timestamp => 'n',
object_id => 'n',
source_colmap => 'n',
target_colmap => 'n',
options_string => null );
END;
grant select on CT_SALESORDER to cdc_sub
grant select on CT_SalesOrderDetail to cdc_sub
BEGIN
DBMS_CDC_SUBSCRIBE.CREATE_SUBSCRIPTION(
change_set_name => 'CDC_SET_SO', --改变集
description => 'Change data for salesOrder, salesOrderDetail',
subscription_name => 'CDC_SUB_SO'); --订阅的名称
END;
begin
DBMS_CDC_SUBSCRIBE.SUBSCRIBE(
subscription_name => 'CDC_SUB_SO', --订阅的名称
source_schema => 'APPUSER', --业务数据表的Owner
source_table => 'SALESORDER', --业务数据表名
--订阅的列
column_list => 'OrderID, CustomerID, DueDate, DeliverTo,
CreateDTTM',
--订阅试图的名称
subscriber_view => 'V_CDC_SalesOrder');
END;
begin
DBMS_CDC_SUBSCRIBE.SUBSCRIBE(
subscription_name => 'CDC_SUB_SO',
source_schema => 'APPUSER',
source_table => 'SALESORDERDetail',
column_list => 'SOLINEID,ORDERID,ITEMNUMBER,QUANTITY,LINEPRICE',
subscriber_view => 'V_CDC_SalesOrderDetail');
END;
BEGIN
DBMS_CDC_SUBSCRIBE.ACTIVATE_SUBSCRIPTION(
subscription_name => 'CDC_SUB_SO');
END;
insert into SalesOrder ( orderId, customerId, dueDate, deliverTo )
values (1, 1, trunc(sysdate)+10, 1)
insert into SalesOrderDetail( SoLineId, OrderId, ItemNumber, Quantity, linePrice)
values ( 1, 1, 'Desk001', 2, 500)
insert into SalesOrderDetail( SoLineId, OrderId, ItemNumber, Quantity, linePrice)
values ( 2, 1, 'Chair001', 2, 350)
--注意,可以试一下,在没有提交之前,在发布表中是没有数据的。这一点似乎也和Oracle 10g不同。
commit
begin
dbms_cdc_subscribe.extend_window(
subscription_name=>'CDC_SUB_SO'); --订阅名
end;
-- 查询订阅视图
select * from V_CDC_SalesOrder order by commit_timestamp$
select * from V_CDC_SalesOrderDetail order by commit_timestamp$
--完成本次订阅
begin
DBMS_CDC_SUBSCRIBE.PURGE_WINDOW(
subscription_name => 'CDC_SUB_SO'); --订阅名
END;
oracle 文档 ——
https://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_cdcpub.htm#ARPLS023