Wednesday, August 21, 2013

Tutorial: Loading images or any blob type files from server to Oracle Database using PLSQL - Part 1

Recently I was working with External tables which is becoming very handy in all the data transfer activities that my work wants to me to perform. They are really cool alternatives to SQL*Loader Utility. I also came across a requirement to load images from your local / server machine to Oracle Database and this is where I wanted to take a look at external tables whether they are equipped to handle images / documents or any other MIME types for that case, the answer was YES and it made my coding world easy to understand and deploy.

There are many ways which we can load image / document type file formats into Oracle Database fields (BLOB Type):- Java methods, DBMS_LOB package or External Tables. This blog post will detail on the DBMS_LOB and External Table to handle loading images / documents stored on a local / server machine as a complete file.

External Table:

1) Create a file (file.txt) for the external table to read with columns ID, file_name, file_name

1,034517.jpg,034517.jpg
2,037608.jpg,037608.jpg
3,045023.jpg,045023.jpg
4,049412.jpg,049412.jpg
5,059125.jpg,059125.jpg
6,066945.jpg,066945.jpg

2) Create a database directory where the file will be stored on the machine

CREATE OR REPLACE DIRECTORY xxfile_dir AS '/usr/tmp/JK'  

3) Save the file 'file.txt' and the files that are referenced inside the file in the directory mentioned in step 2

4) Now connect to database and create an external table with the columns ID, file_name, BLOB column for your file

CREATE TABLE XXLOAD_FILE (
  ID                NUMBER(10),
  FILE_NAME         CHAR(15),
  BLOB_CONTENT      BLOB
)
ORGANIZATION EXTERNAL
(
  TYPE ORACLE_LOADER
  DEFAULT DIRECTORY XXFILE_DIR
  ACCESS PARAMETERS
  (
    RECORDS DELIMITED BY NEWLINE
    BADFILE XXFILE_DIR:'lob_tab_%a_%p.bad'
    LOGFILE XXFILE_DIR:'lob_tab_%a_%p.log'
    FIELDS TERMINATED BY ','
    MISSING FIELD VALUES ARE NULL
    (
      ID                CHAR(10),
      FILE_NAME         CHAR(15),
      BLOB_FILENAME     CHAR(100)
    )
    COLUMN TRANSFORMS (BLOB_CONTENT FROM LOBFILE (BLOB_FILENAME) FROM (XXFILE_DIR) BLOB)
  )
  LOCATION ('file.txt')
)
PARALLEL 2
REJECT LIMIT UNLIMITED;

4) Now you are all done, either call the table using a select statement or in your procedures and load into your document / image tables. You can see that the field BLOB_FILENAME does not match the actual definition as this field is used to actually fetch the file and save it into BLOB_CONTENT field using the COLUMN TRANSFORMS function

select * from XXLOAD_FILE
or
create table XXFILE as
select ID, substr(FILE_NAME,1,instr(FILE_NAME,'.',1)-1) person_no, FILE_NAME, BLOB_CONTENT FILE from XXLOAD_FILE


As this blog post is getting really huge, I would be splitting the tutorial using DBMS_LOB in the next post.

1 comment:

Please add your valuable comments and also questions that would make me write a post for you.

Search This Blog

Labels

oracle oracle applications 11i 12.1 PLSQL SQL database index r12 12 API HRMS PL application r12.1.3 table whitepaper AR DOCUMENT_CATEGORY Database Options Oracle Application Setup Packages Sequences Stand-alone System Profile Options Tables Tuning User-defined types Views autoinvoice compatibility concurrent developer document documents of record download impact import increase issue lookup manager master object names oracle workflow performance receivables script technology windows workflow APP-PAY-06841 ATG BI BLOB DBMS_LOB DFF DIRECTORY Dynamic FND FNDLOAD HR_CHANGE_START_DATE_API IN IN OUT Jdeveloper 11g KFF MIME Materialized views OA Framework OAF ORACLE_LOADER OUT Private synonyms Problem Problem Statement SQL*LOADER SSHR Starters Tutorial WebService XP ad_dd administrator all all workflows amateur architecture assi assignment attachments breadth first search builder category change client column columns compression consideration create create_shared_type custom flexfield cyclic graph data flow data structure data structures depth first search employee external tables field file flex flex field flexfield fnd_irep_classes fnd_irep_function_flavors fnd_lookup_types_pkg fnd_lookup_values_pkg functionality graph image infrastructure insert_row integrated SOA gateway invisible indexes irep jdev jdeveloper key key flexfield keywords latest start date list list of APIs load logging mandatory metalink node noob operating system operational BI package package maximum length parameters patch per_shared_types_api person procedures query queue quick start record registering registration reserved results solution stack standalone start date subcategory technical traverse traversing a graph type update_start_date upload values vertex view vista wf wf_local_roles windows 7

Total Pageviews