This document summarizes a presentation about using directory objects and external tables in Oracle. It discusses how to use directory objects to define aliases for operating system directories and control access to output paths. It provides examples of using external tables to load, extract, and merge data from files. The examples demonstrate how to retrieve files via FTP, load/update database tables, and send email notifications after the jobs complete. Attendees would learn how to incorporate these features into PL/SQL packages to schedule ETL jobs.
1. Atlantic Oracle Training Conference -
December 20051
Kick Up Your Data Load And Data Extract Jobs A Notch!
Mary Wagner
Database Administrator / Developer
Delaware Department of
Transportation
2. Atlantic Oracle Training
Conference - December 20052
My Background
Currently employed as a Database Administrator / Developer
Certified in Oracle 8i and 9i
Experience using 7, 8i, 9i, 10g release 1, RAC
10 years of IT Experience including…
Database administration, design, development
Java and PL/SQL development
Internet / Intranet web site development
LAN Administration
3. Atlantic Oracle Training
Conference - December 20053
Session Goal
To provide an introduction to external tables and directory
objects, along with some simple, practical examples, so that
you can begin to work with and investigate these new Oracle
features.
4. Atlantic Oracle Training
Conference - December 20054
Session Objectives
Directory Object Overview
External Table Overview
Setting up for the Examples
Example 1 - Using Directory Objects and External Tables in
a Data Load Job
Example 2 – Using Directory Objects and External Tables in
a Data Extract Job
5. Atlantic Oracle Training
Conference - December 20055
Directory Object Overview (1)
What is a Directory Object?
Alias for an existing operating system directory
Exists as object within the Oracle database
Can be referred to in PL/SQL code
6. Atlantic Oracle Training
Conference - December 20056
Directory Object Overview (2)
Why Use Them?
Create new output paths without restarting the database
Control access to output paths through database
permissioning
Dynamically create directory output paths within PL/SQL
procedures (i.e. for dates, etc.)
More…….
7. Atlantic Oracle Training
Conference - December 20057
Directory Object Overview (3)
Requirements:
“Create Any Directory” privilege
Existence of related operating system directory
OS Permissioning of operating system directory for Oracle
database processes (i.e. oracle user on unix/linux)
Granting of privileges to database user who will run PL/SQL
referring to the directory object
8. Atlantic Oracle Training
Conference - December 20058
Directory Object Overview (4)
Creation and Permissioning:
mydba > @C:aotc_fall_2005scriptscreate_myuser.sql
mydba > create or replace directory
DIR_AOTC_FALL_2005_MY_OUTPUT1 as
'C:aotc_fall_2005my_output1';
mydba > grant read, write on directory
DIR_AOTC_FALL_2005_MY_OUTPUT1 to MYUSER;
9. Atlantic Oracle Training
Conference - December 20059
Directory Object Overview (4-b)
Creation and Permissioning:
mydba > create or replace directory
DIR_AOTC_FALL_2005_MY_OUTPUT2 as
'C:aotc_fall_2005my_output2';
mydba > grant read, write on directory
DIR_AOTC_FALL_2005_MY_OUTPUT2 to MYUSER;
10. Atlantic Oracle Training
Conference - December 200510
Directory Object Overview (5)
Use Example:
myuser > select * from dba_directories;
myuser > @C:aotc_fall_2005scriptscreate_pkg_test.sql
myuser > exec pkg_test.write_to_file_test;
myuser > exec
pkg_test.write_to_file_test_dynamic('DIR_AOTC_FALL_2005_MY_
OUTPUT2', 'test_file2.txt');
11. Atlantic Oracle Training
Conference - December 200511
External Table Overview (1)
What is an External Table?
A type of Oracle database table
Points to an operating system file
DDL refers to directory object
Can INSERT INTO….SELECT …. FROM …..MERGE
Can perform multi table UPDATES
Can JOIN with regular database tables
9i – Read only, table can only be queried
10g – Can also extract to, using ORACLE_DATAPUMP type
12. Atlantic Oracle Training
Conference - December 200512
External Table Overview (2)
Why Use Them?
Can use any SQL function on them to selectively load/insert
data into a database table
Can update a database table from them
Can incorporate data loads or updates from them using
PL/SQL procedures
Can unload/extract data to them to reload into another
oracle database (10g only)
13. Atlantic Oracle Training
Conference - December 200513
External Table Overview (3)
Requirements:
Existence of related operating system file
OS Permissioning of operating system directory and file for
Oracle database processes (i.e. oracle user on unix/linux)
Granting of privileges to database user who will run PL/SQL
referring to the external table
“CREATE TABLE” privilege (10g release 2)
14. Atlantic Oracle Training
Conference - December 200514
External Table Overview (3-b)
When not to use External Tables:
If LOBS are involved
If the source file has to be accessed on a remote server
If more than one user needs to access the external table at
the same time to load data
16. Atlantic Oracle Training
Conference - December 200516
External Table Overview (5)
Use Example:
myuser > desc ext_test_file_table;
myuser > select * from user_objects;
myuser > select * from ext_test_file_table;
myuser > select * from ext_test_file_table;
myuser > select substr(firstname, 1, 1) || lastname as username from
ext_test_file_table;
myuser > select firstname || ' ' || lastname as fullname from
ext_test_file_table;
17. Atlantic Oracle Training
Conference - December 200517
External Table Overview (5-b)
Changing file name and directory location :
myuser > ALTER TABLE ext_test_file_table LOCATION
( 'test_file2.txt'
);
myuser > ALTER TABLE ext_test_file_table DEFAULT
DIRECTORY DIR_AOTC_FALL_2005_MY_OUTPUT2;
myuser > select * from ext_test_file_table;
18. Atlantic Oracle Training
Conference - December 200518
Setting Up for the Examples.
Grant additional rights to the example user
mydba > @create_myuser.sql (already created
earlier)
Create the example table
myuser > @create_table.sql
Create the FTP PL/SQL package
myuser > @create_pkg_ftp.sql
Create the EMAIL PL/SQL package
myuser > @create_pkg_email.sql
Create the EXAMPLE PL/SQL package
myuser > @create_pkg_examples.sql
19. Atlantic Oracle Training
Conference - December 200519
Example 1 –> Step 1.) Create a New Directory.
mydba> grant create any directory to myuser;
myuser> select * from dba_directories;
myuser> create or replace directory
DIR_AOTC_FALL_2005_EXAMPLE1 as
'C:aotc_fall_2005example1';
myuser> select * from dba_directories;
20. Atlantic Oracle Training
Conference - December 200520
Example 1 –> Step 2.) Create External Table.
myuser> CREATE TABLE EXT_EMPLOYEE_FILE
(EMPLOYEEID NUMBER(3),
LASTNAME VARCHAR2(32),
FIRSTNAME VARCHAR2(32),
PHONE VARCHAR2(13),
EMAILADDRESS VARCHAR2(32),
) ORGANIZATION EXTERNAL
( TYPE ORACLE_LOADER
………..
22. Atlantic Oracle Training
Conference - December 200522
Example 1 –> Step 4.) Load Data
PKG_EXAMPLES.EMPLOYEE_DATA_LOAD_AND_FTP
myuser> INSERT INTO EMPLOYEES SELECT EMPLOYEEID,
LASTNAME, FIRSTNAME,
FIRSTNAME || ' ' || LASTNAME, PHONE,
LOWER(EMAILADDRESS) FROM
EXT_EMPLOYEE_FILE;
myuser> COMMIT;
23. Atlantic Oracle Training
Conference - December 200523
Example 1 –> Step 5.) Send Email Notification
PKG_EXAMPLES.EMPLOYEE_DATA_LOAD_AND_FTP
PKG_EMAIL.send
(p_smtp_host => 'smtp.mycompany.com',
p_sender_email => 'pkg_examples.employee_data_load_and_ftp@mydb',
p_from => 'pkg_examples.employee_data_load_and_ftp@mydb',
p_to => pkg_email.array('jsmith@mycompany.com'),
p_cc => pkg_email.array('jdoe@mycompany.com',
'sjackson@mycompany.com'),
p_bcc => pkg_email.array('dba@mycompany.com'),
p_subject => 'EMPLOYEE DATA LOAD AND FTP JOB SUCCEEDED',
p_body => 'The employee load and ftp job completed successfully at -- ' ||
TO_CHAR(SYSDATE, 'MM/DD/YYYY HH24:MI:SS'));
24. Atlantic Oracle Training
Conference - December 200524
Example 1 –> Step 6.) Run or Schedule PL/SQL
Procedures
exec PKG_EXAMPLES.EMPLOYEE_DATA_LOAD_AND_FTP
Because everything is now in PL/SQL, you can :
Schedule it to run through DBMS_JOB
Schedule it to run through DBMS_SCHEDULER
Schedule it to run through Oracle Management Server
Or run it manually………..
25. Atlantic Oracle Training
Conference - December 200525
Example 2 –> Step 1.) Alter External Table
PKG_EXAMPLES.EMPLOYEE_DATA_MERGE_AND_FTP
myuser > ALTER TABLE EXT_EMPLOYEE_FILE LOCATION
( 'employee_file_new_and_existing.txt'
);
myuser > SELECT * FROM EXT_EMPLOYEE_FILE;
27. Atlantic Oracle Training
Conference - December 200527
Example 2 –> Step 3.) Merge Data
PKG_EXAMPLES.EMPLOYEE_DATA_MERGE_AND_FTP
MERGE INTO employees e
USING EXT_EMPLOYEE_FILE e_ext
ON (E.EMPLOYEEID=E_EXT.EMPLOYEEID)
WHEN MATCHED THEN UPDATE
set e.firstname=e_ext.firstname,
e.lastname=e_ext.lastname,
e.fullname=e_ext.firstname || ' ' || e_ext.lastname,
e.phone=e_ext.phone,
e.emailaddress=lower(e_ext.emailaddress)
WHEN NOT MATCHED THEN…….
28. Atlantic Oracle Training
Conference - December 200528
Example 2 –> Step 4.) Send Email Notification
PKG_EXAMPLES.EMPLOYEE_DATA_MERGE_AND_FTP
PKG_EMAIL.send
(p_smtp_host => 'smtp.mycompany.com',
p_sender_email => 'pkg_examples.employee_data_load_and_ftp@mydb',
p_from => 'pkg_examples.employee_data_load_and_ftp@mydb',
p_to => pkg_email.array('jsmith@mycompany.com'),
p_cc => pkg_email.array('jdoe@mycompany.com',
'sjackson@mycompany.com'),
p_bcc => pkg_email.array('dba@mycompany.com'),
p_subject => 'EMPLOYEE DATA MERGE AND FTP JOB SUCCEEDED',
p_body => 'The employee merge and ftp job completed successfully at --
' || TO_CHAR(SYSDATE, 'MM/DD/YYYY HH24:MI:SS'));
29. Atlantic Oracle Training
Conference - December 200529
Example 2 –> Step 5.) Run or Schedule PL/SQL
Procedures
exec PKG_EXAMPLES.EMPLOYEE_DATA_MERGE_AND_FTP
Because everything is now in PL/SQL, you can :
Schedule it to run through DBMS_JOB
Schedule it to run through DBMS_SCHEDULER
Schedule it to run through Oracle Management Server
Or run it manually………..
30. Atlantic Oracle Training
Conference - December 200530
Example 3 (10g) –> Step 1.) Create a New Directory.
PKG_EXAMPLES.EMPLOYEE_DATA_EXTRACT_AND_FTP
myuser > create or replace directory
DIR_AOTC_FALL_2005_EXAMPLE3 as
'C:aotc_fall_2005example3';
31. Atlantic Oracle Training
Conference - December 200531
Example 3 (10g) –> Step 2.) Create External Table -
Extract Data
PKG_EXAMPLES.EMPLOYEE_DATA_EXTRACT_AND_FTP
myuser> CREATE TABLE EMPLOYEE_EXTRACT
ORGANIZATION EXTERNAL
( TYPE ORACLE_DATAPUMP
DEFAULT DIRECTORY DIR_AOTC_FALL_2005_EXAMPLE3
LOCATION( 'EMPLOYEE_EXTRACT.DAT’ )
)
AS
SELECT * FROM EMPLOYEES
/
33. Atlantic Oracle Training
Conference - December 200533
Example 3 (10g) –> Step 4.) Send Email Notification
PKG_EXAMPLES.EMPLOYEE_DATA_EXTRACT_AND_FTP
PKG_EMAIL.send
(p_smtp_host => 'smtp.mycompany.com',
p_sender_email => 'pkg_examples.employee_data_load_and_ftp@mydb',
p_from => 'pkg_examples.employee_data_load_and_ftp@mydb',
p_to => pkg_email.array('jsmith@mycompany.com'),
p_cc => pkg_email.array('jdoe@mycompany.com',
'sjackson@mycompany.com'),
p_bcc => pkg_email.array('dba@mycompany.com'),
p_subject => 'EMPLOYEE DATA EXTRACT AND FTP JOB
SUCCEEDED',
p_body => 'The employee extract and ftp job completed successfully at --
' || TO_CHAR(SYSDATE, 'MM/DD/YYYY HH24:MI:SS'));
34. Atlantic Oracle Training
Conference - December 200534
Example 3 (10g)–> Step 5.) Run or Schedule PL/SQL
Procedure
exec PKG_EXAMPLES.EMPLOYEE_DATA_EXTRACT_AND_FTP
Because everything is now in PL/SQL, you can :
Schedule it to run through DBMS_JOB
Schedule it to run through DBMS_SCHEDULER
Schedule it to run through Oracle Management Server
Or run it manually………..
35. Atlantic Oracle Training
Conference - December 200535
Miscellaneous Notes
Be Careful With Directory Permissions – Be frugal
when granting “create any directory” permissions to users.
If they create it, they can write to it through the database.
Case Sensitivity – Be aware of OS directory and file
name case sensitivity when creating directory objects and
external tables in Oracle.
Do The Research – Your environment is not going to be
exactly the same as any one else's. Read the Oracle
DOCS and Chapter 15 of Tom Kyte’s new book (and the
rest also!).
36. Atlantic Oracle Training
Conference - December 200536
Acknowledgements / More Information
Oracle Docs / Manuals:
Oracle Database SQL Reference Manual, 9i, 10g, Oracle
Corporation
Oracle Database Concepts Manual, 9i, 10g, Oracle
Corporation
Oracle Utilities Guide 9i, 10g, Oracle Corporation
Other Books:
Expert Oracle Database Architecture, Tom Kyte
Web Sites:
http://asktom.oracle.com
http://www.oracle-base.com
http://otn.oracle.com
37. Atlantic Oracle Training
Conference - December 200537
Scripts / Contact Info
Use the scripts at your own risk and test them fully in your own test
environment before using them in a production database.
Please email me if you find any errors in the scripts or have any
questions.
Email me at:
maryx.wagner@state.de.us