OraMatt: YABAOracle

Yet Another Blog About Oracle

, , , , ,

External Table Preprocessor to list files in a directory


/* Create the directory object and grant permissions */
CREATE DIRECTORY USER_DOCS_DIR as '/home/oracle/user_docs_dir/';

/* DO NOT GRANT THIS ON PRODUCTION */
GRANT READ,WRITE,EXECUTE on USER_DOCS_DIR to public;

/* Create the preprocessor script */
! echo "#!/bin/bash \
/bin/ls /home/oracle/user_docs_dir/ -l " >/home/oracle/user_docs_dir/exec_ls.sh

DROP TABLE ls_table;
CREATE TABLE ls_table
(
           "file_permissions"     VARCHAR2(50),
           "file_links"    VARCHAR2(50),
           "file_owner"    VARCHAR2(50),
           "file_owner_group" VARCHAR2(50),
           "file_size"       VARCHAR2(50),
           "file_month"    VARCHAR2(50),
           "file_day"      VARCHAR2(50),
           "file_timestamp"       VARCHAR2(50), 
           "file_name"       VARCHAR2(50)
)
   ORGANIZATION EXTERNAL (
       TYPE ORACLE_LOADER
       DEFAULT DIRECTORY USER_DOCS_DIR
       ACCESS PARAMETERS 
       (   
        records delimited by newline
        PREPROCESSOR USER_DOCS_DIR: 'exec_ls.sh' 
        LOGFILE USER_DOCS_DIR: 'ls_table.log'
        skip 1
        fields terminated by whitespace
        MISSING FIELD VALUES ARE NULL
        ) 
         LOCATION(USER_DOCS_DIR:'exec_ls.sh')
       )
;

/* Test the external table */
select * from ls_table ;




One response to “External Table Preprocessor to list files in a directory”

  1. […] the LS_TABLE external table that you can find here you’ll be able to loop through all the files in a directory and load them into a […]

Leave a comment

Navigation

About

I’m Matt and I do Oracle things.