| Oracle® Database Advanced Replication Management API Referen
ce 10g Release 1 (10.1) Part Number B10733-01 |
|
|
View PDF font> |
This chapter illustrates how to build a deployment template using the replication managment API.
This chapter contains these topics:
Before you build m aterialized view environments, you must set up your master site, create a master group, and set up your intended materialized view si tes. Also, if conflicts are possible at the master site due to activity at the materialized view sites you are creating, then configu re conflict resolution for the master tables of the materialized views before you create the materialized view group.
Oracle offers deployment templates to allow the database administrator to package a mat erialized view environment for easy, custom, and secure distribution and installation. A deployment template can be simple (for examp le, it can contain a single materialized view with a fixed data set), or complex (for example, it can contain hundreds of materialize d views with a dynamic data set based on one or more variables). The goal is to define the environment once and deploy the deployment template as often as necessary. Oracle deployment templates feature:
To prepare a materialized view environment for deployment, the DBA creates a deployment template at the master site. This template stores all of the information needed to deploy a materialized view environment, including the DDL to create the objects at the remote site and the target refresh group. This template also maintains links to user security information and template parameters for custom materialized view creation.
You cannot use deployment templates to instantiate th e following types of objects:
Nor can you use d eployment templates to instantiate any objects based on these types of objects.
| See Also:
Oracle Database Advanced Replication for more conceptual information about deployment templates |
If you want one of your master sites to support a materialized views that can be fast refreshed, then you need to create materialized view logs for each master table that is replicated to a materialized view.
The example in this chapter uses the hr sample schema. Enter the following to create materialized view logs for the t
ables in the hr schema:
CONNECT hr/hr@orc3.world CREATE MATERIALIZED VIEW LOG ON hr.countries; CREATE MATERIALIZED VIEW LOG ON hr.departments; CREATE MATERIALIZED VIEW LOG ON hr.employees; CREATE MATERIALIZED VIEW LOG ON hr.jobs; CREATE MATERIALIZED VIEW LOG ON hr.job_history; CREATE MATERIALIZED VIEW LOG ON hr.locations; CREATE MATERIALIZED VIEW LOG ON hr.regions;
The |
This section contains a complete script example of h ow to construct a deployment template using the replication management API.
| See Also:
Oracle Database Advanced Replication for conceptual and archi tectural information about deployment templates |
Text description of the illustration rardta.gif
Be sure to read the comments contained within the scripts, as they contain important and useful infor mation about building templates with the replication management API.
|
Note: You must use the Replication Management tool if you want to create materialized views with a subset of the columns their master tables. See Oracle Database Advanced Replication and the Replication Management t ool online help for more information about column subsetting. |
/************************* BEGINNING OF SCRIPT ****************************** < /pre>
This script creates a private deployment template that contains four template objects, two template parameters, a set of user parameter values, and an authorized user. Complete the following steps to build a template:
Before assembling the components of your deployment template, use the CREATE_RERESH_TEMPLATE procedure to define the name of your deployment template, along with several other template characteristics (Public/Private status
, target refresh group, and owner).
*/ SET ECHO ON SPOOL create_dt.out CONNECT repadmin/repadmin@orc3.wo rld DECLARE a NUMBER; BEGIN a := DBMS_REPCAT_RGT.CREATE_REFRESH_TEMPLATE ( owner => 'hr', refresh_group_name => 'hr_refg', refresh_template_name => 'hr_refg_dt' , template_comment => 'Human Resources Deployment Template', public_te mplate => 'N'); END; / /*a>
Create countries_mv materialized view.
*/ DECLARE tempstring VARCHAR2(3000); a NUMBER; BEGIN tempstring := 'CREATE MATERIALIZED VIEW hr.countries_mv REFRESH FAST WITH PRIMARY KEY FO R UPDATE AS SELECT country_id, country_name, region_id FROM hr.countries@:dblin k'; a := DBMS_REPCAT_RGT.CREATE_TEMPLATE_OBJECT ( refresh_template_name => 'hr_refg_dt', object_name => 'countries_mv', object_type => 'MATERIALIZED VIEW', ddl_text => tempstring, master_rollback_seg => 'rbs'); END; / /*
Whenever you create a materialized view, always specify the schema name o
f the table owner in the query for the materialized view. In the example previously, hr is specified as the owner of the
countries table.
Create departments_mv materialized view.
*/ DECLARE tempstring VARCHAR2(3000); a NUMBER; BEGIN tempstring := 'CREATE MATERIALIZED VIEW hr.departm ents_mv REFRESH FAST WITH PRIMARY KEY FOR UPDATE AS SELECT department_id, depar tment_name, manager_id, location_id FROM hr.departments@:dblink'; a := DBMS_REPCAT_RGT.CREATE_TEMPLATE_OBJECT ( refresh_template_name => 'hr_refg_dt', object_name => 'departments_mv', object_type => 'MATERIALIZED VI EW', ddl_text => tempstring, master_rollback_seg => 'rbs'); END; / /*
Create employees_mv materialized view.
*/ DECLARE tempstring VARCHAR2(3000); a NUMBER; BEGIN < a name="7606"> tempstring := 'CREATE MATERIALIZED VIEW hr.employees_mv REFRESH FAST WITH PRIMARY KEY FOR UPDATE AS SELECT employee_id, first_name, last_name, email, phone_number, h ire_date, job_id, salary, commission_pct, manager_id, department_id FROM hr.emplo yees@:dblink WHERE department_id = :dept'; a := DBMS_REPCAT_RGT.CREATE_TEMPLATE_OB JECT ( refresh_template_name => 'hr_refg_dt', object_name => 'e mployees_mv', object_type => 'MATERIALIZED VIEW', ddl_text => temps tring, master_rollback_seg => 'rbs'); END; / < /a> /*
Create jobs_mv materialized vi
ew.
*/ DECLARE tempstring VARCH AR2(3000); a NUMBER; BEGIN tempstring := 'CREATE MATERIALIZED VIEW hr. jobs_mv REFRESH FAST WITH PRIMARY KEY FOR UPDATE AS SELECT job_id, job_title, mi n_salary, max_salary FROM hr.jobs@:dblink'; a := DBMS_R EPCAT_RGT.CREATE_TEMPLATE_OBJECT ( refresh_template_name => 'hr_refg_dt', object_name => 'jobs_mv', object_type => 'MATERIALIZED VIEW', ddl_text => tempstring, master_rollback_seg => 'rbs'); END; / /*
Create job_his
tory_mv materialized view.
*/ DECLARE tempstring VARCHAR2(3000); a NUMBER; BEGIN tempstri ng := 'CREATE MATERIALIZED VIEW hr.job_history_mv REFRESH FAST WITH PRIMARY KEY FOR UPDATE AS SELECT employee_id, start_date, end_date, job_id, department_id FROM hr.job_history@:dblink '; a := DBMS_REPCAT_RGT.CREATE_TEMPLATE_OBJECT ( refresh_template_name => 'hr_refg_dt', object_name => 'job_history_mv', object_type => 'MATERIALIZED VIEW', ddl_text => tempstring, master_rollback_seg => 'rbs'); END; / /*
Create locations_mv materialized view.
*/ DECLARE tempstring VARCHAR2(3000); a NUMBER; BEGIN tempstring := 'CREATE MATERIALIZED VIEW hr.locations_mv REFRESH FAST WITH PRIMARY KEY FOR UPDATE AS SELECT location_id, street_address, post al_code, city, state_province, country_id FROM hr.locations@:dblink'; a := DBMS_REPCAT_RGT.CREATE_TEMPLATE_OBJECT ( refresh_t emplate_name => 'hr_refg_dt', object_name => 'locations_mv', obje ct_type => 'MATERIALIZED VIEW', ddl_text => tempstring, master_ro llback_seg => 'rbs'); END; / /* < /a>
Create regions_mv materialized view.
*/ DECLARE tempstring VARCHAR2(3000); a NU MBER; BEGIN tempstring := 'CREATE MATERIALIZED VIEW hr.regions_mv REFRESH FAST WITH PRIMARY KEY FOR UPDATE AS SELECT region_id, region_name FR OM hr.regions@:dblink'; a := DBMS_REPCAT_RGT.CREATE_TEMPLATE_OBJECT ( refresh_template_name => 'hr_refg_dt', object_name => 'regions_mv', object_type => 'MATERIALIZED VIEW', ddl_text => tempstring, master_rollback_seg => 'rbs'); END; / /*
Rather than using the CREATE_* functions and procedures as in the other
steps, use the ALTER_TEMPLATE_PARM procedure to define a template parameter value and prompt string. You use the A
LTER_* procedure because the actual parameter was created in Step 1
and 1. Recall that you defined the :dblink and :dept template parameters in the ddl_text parameter. Oracle detects these parameters in the DDL and automatically creates
the template parameter. Use the ALTER_TEMPLATE_PARM procedure to define the remainder of the template parameter informa
tion (that is, default parameter value and prompt string).
Complete the following tasks to de fine parameter defaults.
Define the default value for the dept parameter.
Define
the default value for the dblink parameter.
*/ BEGIN DBMS_REPCAT_RGT.ALTER_TEMPLATE_PARM ( r efresh_template_name => 'hr_refg_dt', parameter_name => 'dblink', new_def ault_parm_value => 'orc3.world', new_prompt_string => 'Enter your master site:', a> new_user_override => 'Y'); END; / /*
To automate the instantiation of custom data sets at individual remote materialized view sites,
you can define user parameter values that will be used automatically when the specified user instantiates the target template. The
Complete the following tasks to define user parameter values.
Define dept
user parameter value for user hr.
*/ DE CLARE a NUMBER; BEGIN a := DBMS_REPCAT _RGT.CREATE_USER_PARM_VALUE ( refresh_template_name => 'hr_refg_dt', parameter_name => 'dept', user_name => 'hr', parm_value => ; '20'); END; / /*
Define dblink user parameter value for user hr.
*/ DECLARE a NUMBER; BEGIN a := DBMS_REPCAT_RGT.CREATE_USER_PARM_VALUE ( refresh_temp late_name => 'hr_refg_dt', parameter_name => 'dblink', user_name => 'hr', parm_value => 'orc3.world'); END; / /*
Because this is a private template (public_templa
te => 'n' in the DBMS_REPCAT_RGT.CREATE_REFRESH_TEMPLATE function defined in Step ), you need to authorize users to instantiate the dt_personnel deployment template.
Use the CREATE_USER_AUTHORIZATION function in the DBMS_REPCAT_RGT package to create authorized users.
*/ DECLARE a NUMBER; BEGIN a := DBMS_REPCAT_RGT.CREATE_USER_AUTHORIZATION ( user_name => 'hr', refresh_template_name => 'hr_refg_dt'); END; / COMMIT; SET ECHO OFF SPOOL OFF /************************* END OF SCRIPT *********** ***********************/
After building your deployment template, you need to package the template for instantiation. This example illustrate
s how to use both the online and offline instantiation procedures. Notice that the instantiation procedures are very similar: you sim
ply use either the INSTANTIATE_ONLINE function or INSTANTIATE_OFFLINE function according to your needs. Thi
s section describes two tasks: create the instantiation script and save the instantiation script to a file.
Tex t description of the illustration rardt2.gif
When
you execute either the INSTANTIATE_OFFLINE or the INSTANTIATE_ONLINE function, Oracle populates the
USER_REPCAT_TEMP_OUTPUT data dictionary view with the script to create the remote materialized view environment. Both online a
nd offline scripts contain the SQL statements to create the objects specified in the deployment template. The difference is that an o
ffline instantiation script also contains the data to populate the objects. The online instantiation script does not contain the data
. Rather, during online instantiation, the materialized view site connects to the master site to download the data.
Complete the steps in either the "Packaging a Deployment Template for Offline Instant iation" or "Packaging a Deployment Template for Online Instantiation" according to your needs.
< a name="18073">The INSTANTIATE_OFFLINE function creates a script that creates the materialized view environmen
t according to the contents of a specified deployment template. In addition to containing the DDL (CREATE statements) to
create the materialized view environment, this script also contains the DML (INSERT statements) to populate the materia
lized view environment with the appropriate data set.
|
Note: If you are packaging your template at the same master site that contains the target master objects for your deployment template, then you must create a loopback database link. |
--Use the INSTANTIATE_OFFLINE function to package the --template for offline instantiation by a remote materialized view --site. Executing this procedur e both creates a script that --creates that materialized view environment and populates the -- environment with the proper data set. This script is stored --in the temporary USER_REPCAT_TEMP_OUTPUT view. CONNECT repadmin/repadmin@orc3.world SET SERVEROUTPUT ON < a name="7913">DECLARE dt_num NUMBER; BEGIN dt_num := DBMS_REPCAT_RGT.INSTANTIATE_OFFLINE( refresh_template_name => 'hr_refg_dt', user_name => 'hr', site_name => 'mv4.world', next_date => SYSDATE, interval => 'SYSDATE + (1/144)'); a> DBMS_OUTPUT.PUT_LINE('Template ID = ' || dt_num); END; / COMMIT; /
Make a note of the number that is returned for the USER_REPCAT_TEMP_OUTPUT data dictionary view
to retrieve the generated script. Be sure that you complete the steps in "Saving an Instantiation Script to
File" after you complete this section. This script is unique to an individual materialized view sit
e and cannot be used for other materialized view sites.
The INSTANTIATE_ONLINE function creates a script that creates the mate
rialized view environment according to the contents of a specified deployment template. When this script is executed at the remote ma
terialized view site, Oracle creates the materialized view site according to the DDL (CREATE statements) in the script a
nd populates the environment with the appropriate data set from the master site. This requires that the remote materialized view site
has a "live" connection to the master site.
| See Also:
Oracle Database Advanced Replication for additional materialized view site requirements |
--Use the INSTANTIATE_ONLINE function to "package" the < /a>--template for online instantiation by a remote materialized view --site. Executing this procedure creates a s cript which can --then be used to create a materialized view environment. This script --is s tored in the temporary USER_REPCAT_TEMP_OUTPUT view. CONNECT repadmin/repadmin@orc3.world SET SERVEROUTPUT ON DECLARE dt_num NUMBER; BEGIN dt_num := DBMS_REPCAT_RGT.INSTANTIATE_ONLINE( refresh_template_name => 'hr_refg_dt', user_name => 'hr', site_name => 'mv4.world', next_date => SYSDATE, interval => 'SYSDATE + (1/144)'); DBMS_OUTPUT.PUT_LINE('Template ID = ' || dt_num); EN D; / COMMIT; /
Make a note of the number that is returned for the dt_num variable. You must use this number when you select from the <
code>USER_REPCAT_TEMP_OUTPUT data dictionary view to retrieve the generated script. Be sure that you complete the steps in "Saving an Instantiation Script to File" after you complete this task.
The best way to save the contents of the USER_REPCAT_TEMP_OUTPUT data
dictionary view is to use the UTL_FILE package to save the contents of the TEXT column in the USER_RE
PCAT_TEMP_OUTPUT view to a file.
These contents are saved to a directory that correspo
nds to a directory object. To create a directory object, the CREATE ANY DIRECTORY privilege is
required. If the replication administrator does not have this privilege, then connect as an administrative user who can grant privil
eges. For example:
GRANT CREATE ANY DIRECTORY TO repadmin;
|
Note: The following action must be performed immediately after you have called either the |
PL/SQL Packages and Types Reference for more informat
ion about the UTL_FILE package
Enter the following to sa ve the deployment template script to a file.
DECLARE
fh UTL_FILE.FILE_
TYPE;
CURSOR ddlcursor(myid NUMBER) IS
SELECT TEXT FROM USER_REPCAT_TEMP_OUTPUT WHERE O
UTPUT_ID = myid ORDER BY LINE;
BEGIN
fh := UTL_FILE.FOPEN ('file_locat
ion', 'file_name', 'w');
UTL_FILE.PUT_LINE (fh, 'SET ECHO OFF;');
FOR myrec IN ddlcursor(template_id) LOOP
UTL_FILE.PUT_LINE(fh, myrec.text);
END LOOP;
UTL_FILE.PUT_LINE (fh, 'SET ECHO ON;');
UTL_FILE.FFLUSH
(fh);
UTL_FILE.FCLOSE(fh);
END;
/
Notice that file_location, file_name, and templa
te_id are placeholders. Substitute the correct values for your environment:
file_location placeholder with the name of a directory object that represen
ts the directory where you want to save the template script.template_id placeholder with the number returned by the INSTANTIATE_OFFLINE or
INSTANTIATE_ONLINE function when you packaged the template previously.Fo r example, suppose you have the following values:
| Placeholder | Value |
|---|---|
|
|
|
| <
/a>
|
|
|
|
|
See Also:
|
/************************* BEGINNING OF SCRIPT ******************************
Before executing the instantiation script at the remote material ized view site, you must create the schema that contains the replicated objects.
The followin
g illustrates creating the hr schema. This schema may already exist in your database. In this case, the schema may need
additional privileges, such as CREATE MATERIALIZED VIEW, ALTER ANY <
code>MATERIALIZED VIEW, and CREATE DATABASE LINK.
*/ SET ECHO ON SPOOL instant_mv.out CONNECT SYSTEM/MANAGER@mv4.world CREATE TABLESPACE demo_mv DATAFILE 'demo_mv.dbf' SIZE 10M AUTOEXTEND ON EXTENT MANAGEMENT LOCAL AUTOALLOCATE ; CREATE TEMPORARY TABLESPACE temp_mv TEMPFILE 'temp_mv.dbf' SIZE 5M AU TOEXTEND ON; CREATE USER hr IDENTIFIED BY hr; ALTER USER hr DEFAULT TABLESPACE demo_mv QUOTA UNLIMITED ON demo_mv; ALTER USER hr TEMPORARY TABLESPACE temp_mv; GRANT CREATE SESSIO N, CREATE TABLE, CREATE PROCEDURE, CREATE SEQUENCE, CREATE TRIGGER, CREATE VIEW, CREATE SYNONYM, ALTER SE SSION, CREATE MATERIALIZED VIEW, ALTER ANY MATERIALIZED VIEW, CREAT E DATABASE LINK TO hr; /*
Before instantiating the deployment template, you must make sure that the necessary database lin
ks exist for the replicated schema. The owner of the materialized views needs a database link pointing to the proxy_refresher
code> that was created when the master site was set up.
*/ CREATE PUBLIC DATABASE LINK orc3.world USING 'orc3.world'; CONNECT hr/hr@mv4.world CREATE DATABASE LINK orc3.world CONNECT TO proxy_refresher IDENTIFIED BY proxy_refresher; /*
| See Also:
Step 7 for more information about creating proxy master site users |
*/ CONNECT mviewadmin/mviewadmin@mv4.world @d:\sf.sql SET ECHO OFF SPOOL OFF /*
Depending on the size of the ma terialized view environment created and the amount of data loaded, the instantiation procedure may take a substantial amount of time.
************************** END OF SCRIPT **********************************/
If you have just instantiated a deployment template using the offline instantiation method, then you should perform a refresh of the refresh group as soon as possible by issuing the fo llowing execute statement:
CONNECT hr/hr@mv4.world EX ECUTE DBMS_REFRESH.REFRESH ('hr_refg');