Oracle GoldenGate: 使用宏

来源:互联网 发布:淘宝卖家违规条例 编辑:程序博客网 时间:2024/06/06 03:02

    OGG宏与C语言中的宏一样,提供了函数封装的功能,即可以将一些配置参数整理为一个宏,然后在多个参数文件中共用,针对环境复杂或多个复制点的情况尤其有用。下面我们将介绍如何创建一个宏的库,以及在OGG参数文件中如何使用创建的这个宏 library.

一个宏函数库是宏的集合,宏可以是一个库文件中包含多个宏的定义,或多个库文件,包括不同的宏定义。建议将库文件存放在OGG的dirmac目录下,同时,库文件使用.mac为后缀。这样可以在引用时方便的识别。

 

下面是一个宏的示例,所有内容存放在一个库文件中:“$OGG_BASE/dirmac/macrolib.mac”.

 

MACRO#dbconnect

BEGIN

useridgguser, password AACAAAAAAAAAAAHAAIFBOIYAMCGIMARE, encryptkey default

END;

 

MACRO#bpsettings

BEGIN

-- Thefollowing are "best practice" runtime options which may be

-- usedfor workload accounting and load balancing purposes.

--STATOPTIONS RESETREPORTSTATS ensures that process

--statistics counters will be reset whenever a new report file is

--created.

STATOPTIONSRESETREPORTSTATS

 

--Generate a report every day at 1 minute after midnight.

-- This reportwill contain the number of operations, by operation

-- type,performed on each table.

REPORT AT00:01

 

-- Closethe current report file and create a new one daily at 1

-- minuteafter midnight. Eleven report files are maintained on disk

-- in thedirrpt directory/folder for each GoldenGate group. The

--current report file names are <group name>.rpt. The older reports

-- are<group name>0.rpt through <group name>9.rpt, with the

-- olderreport files having larger numbers.

REPORTROLLOVERAT 00:01

 

--REPORTCOUNT denotes that every 60 seconds the Replicat report file

-- in thedirrpt directory/folder will have a line added to it that reports the

-- totalnumber of records processed since startup, along with the rated

-- numberof records processed per second since startup, and the change

-- inrate, or "delta" since the last report.

-- In aproduction environment, this setting would typically be 1 hour.

REPORTCOUNTEVERY 60 SECONDS, RATE

 

-- End of"best practices" section

END;

 

MACRO#funcsmap

PARAMS (#src_table,#target_table)

BEGIN

   -- Map the source table provided in thevariable #src_table to the target

   -- table listed in the variable#target_table. There are extra columns in the

   -- target we need to populate, so get thedata from either the environment

   -- variable, or the user token data sentover from Extract

   MAP #src_table, TARGET #target_table,

    colmap (usedefaults,

            orders_trans_ts = @GETENV("GGHEADER", "COMMITTIMESTAMP"),

            trans_rec_loc8tr = @STRCAT (@GETENV("RECORD", "FILESEQNO"),

                                        @GETENV("RECORD", "FILERBA")),

            extract_lag_ms = @TOKEN("TKN-EXTLAG-MSEC"),

            replicat_lag_ms = @GETENV("LAG", "MSEC"),

            src_db_name = @TOKEN ("TKN-SRC-DBNAME"),

            src_db_version = @TOKEN("TKN-SRC-DBVERSION"),

            src_txn_csn = @TOKEN("TKN-TXN-CSN")

     );

END;

 

所有的宏使用关键“Macro”开始定义,后面是宏的名称,以“#name”定义,宏的内容由“begin...end;”关键字包含,上面的三个宏定义分别是#dbconnect, #bpsettings, and #funcsmap.

#dbconnect宏定义了一个DB连接的语句,使用默认的key进行密码加密;

#bpsettings宏定义了OGG中配置时的一些最佳建议,比如按天或按小时报告复制情况;

#funcsmap宏定义了一个map映射,要注意的是它要求带两个参数来使用;

 

下面是宏在一个replicat端的使用:

 

nolist

include./dirmac/macrolib.mac

list

 

replicatrfuncb

#dbconnect()

#bpsettings()

sourcedefs./dirdef/mydefs.defs

discardfile./dirrpt/rfuncb.dsc, purge

#funcsmap(amer.orders, euro.funcs_test)

 

当rfuncb投递进程启动时,include语句加载库文件:./dirmac/macrolib.mac。参数nolist的意思是告诉投递进程不要把后面的执行写入到日志文件中,而list参数则是要求后续的执行语句都要写日志。

如果本参数文件中没有list语句,则整个投递进程将不会有任何日志输出到文件中。

当投递进程读取这个参数文件时,它会将引用的宏替换为库文件中的语句定义,因此“#dbconnect ()”,“#bpsettings ()”和 “#funcsmap(amer.orders, euro.funcs_test)”都会被替换为相应的语句。

“#funcsmap”宏使用了两个参数,分别代表源表和目标表。PARAMS关键字是告诉使用人员,这个宏需要有参数才能运行。针对参数的使用,有以下几点注意事项:

参数名不区分大小写;

参数名中不能使用引号;

参数在宏定义中不是必须的;

一个宏最多可以有99个参数,所有参数的长度不能超过9999字节;

所有参数都必须在PARAMS语句中定义,当调用宏时,所有参数都必须赋值。

 

小结

OGG宏在灵活运用和配置OGG参数时是个不错的技巧,特别是针对多个配置文件和复杂环境时,可以在一个文件中定义常用的一组操作,然后在所有环境中使用,降低运维的复杂性。

 

0 0