Skip to main content

Querying Data From Flat Files in Oracle

Couple of days before i had an interesting discussion with my client related to Oracle external tables,

Client: Is there any way to read the data from flat file(s) which are stored outside the database?

Raja: Could you please explain in more detail?

Client: Sure. We have data analytics team, what they will do is, they will export the file(s) (.csv/.txt) from their system and push it to our file system (Oracle directory). 

They have a job to push their files to our file system, which will run in predefined interval. Once it's uploaded we should read the file(s) data and display it in our system.

Raja: Yes! it's very much possible, if we go with Oracle external table.

Client: What is that?

Raja: It can be used for query, join and sort operations. External tables allow oracle to query data that is stored outside the database in flat files. Once it's running we can create views against it.

Client: Oh! Sounds interesting. That's what i needed. Is there any limitations on external tables?

Raja: Yes. No DML can be performed on external tables.

Client: That's Ok. It's not needed at all, since we are not going to perform any DML operation. So you please go ahead.

-----------------------------------------------------------

Here I am giving an example to read the data that is stored outside the database in flat files.

Step 1: File(s) should be saved to the file system available to the Oracle server.
Fig 1: Sample load file data (.CSV)
Fig 2: Sample load file data (.TXT)

Step 2: Create a directory, that should pointing to the location of the file.

Step 3: Understand the load file structure, which oracle external table going to read.

Step 4: Create the external table as per the load file structure.

CREATE TABLE fxgn_emp_ext_data
  (
    empno      NUMBER,
    ename      VARCHAR2(240),
    JOB        VARCHAR2(240),
    department VARCHAR2(240),
    LOCATION   VARCHAR2(240),
    manager    VARCHAR2(240),
    hiredate   DATE,
    salary     NUMBER,
    commission NUMBER
  )
  ORGANIZATION EXTERNAL
  (
    TYPE oracle_loader 
    DEFAULT DIRECTORY APEXUAT -- put your directory name here
    ACCESS PARAMETERS (
                        fields terminated BY ',' optionally enclosed BY '"'
                        missing field VALUES are NULL 
                      ( empno,
                        ename,
                        JOB,
                        department,
                        LOCATION,
                        manager,
                        hiredate,
                        salary,
                        commission) 
                      ) 
    LOCATION ('employee_details.csv') 
-- put your file(s) name here, which should be saved in appropriate directory
  )
  REJECT LIMIT UNLIMITED;

Step 6: Query the external table the following result will be displayed.

Fig 3: Output
Note: Once external table is created, it can be queried like a regular table.

ORA-29913: If the load file have not been saved in the appropriate directory the following result will be displayed.
Fig 4: ORA-29913
That's it. This is mind blowing.

Happy APEXing!!!...

References:

Comments

  1. We just upgraded our Oracle database to 19c and discovered that external tables have a new requirement that broke our import process! There has to be at least one line-feed character at the end of the column header, or there need to be data rows. Our files are sometimes just the column headers with no rows. Unfortunately our vendor doesn't put a line-feed at the end in those cases! So we've had to rewrite the code! :-(

    ReplyDelete
  2. There are many blogs I have read. But when I read Your Blogs I have found such useful information, fresh content with such amazing editing everything is superb in your blog. Thank you so much for sharing this useful and informative information with us.

    online medicine delivery in ajmer

    ReplyDelete

Post a Comment

Popular posts from this blog

Printing Page Numbers in RTF Template [Oracle BI Publisher]

Here I am giving an example to print the page numbers dynamically in the RTF (Rich Text Format) template. Step 1:  Go to page footer and copy and paste the below script. Page |  <?fo:page-number?>  of  <?fo:page-number-citation:xdofo:lastpage-joinseq?> <fo:page-number> :   This is the object, which is used to represent the current page-number. <?fo:page-number-citation:xdofo:lastpage-joinseq?> :  This is the syntax, which is used to represent the total number of pages. Step 2:  Load the XML and preview the result. Output: That's it. References: fo:page-number Printing Page Number Code in Oracle XMLP RTF Template

Friendly URL: Redirect to Different Page after Login in Oracle APEX 20.1

Oracle has updated apex.oracle.com to APEX 20.1 which includes among other features the new Friendly URL option. Here i am giving an example to redirect to different page after login in Oracle APEX 20.1 [Friendly URL Enabled] Step 1: Define home page for each user in emp master table as below Step 2: To enable Friendly URL Syntax, follow below steps, 1) On the Workspace home page, click the App Builder icon. 2) Select an application (The Application home page appears). 3) From Application home page, you can access the Definition page in TWO ways: Click the Edit Application Properties button. From Shared Components:              1) Click Shared Components .              2) Under Application Logic, click Application Definition Attributes . The Definition page appears. 4) Under Properties, configure the Friendly URL s attribute: Click Apply Changes to save your ch...

Open modal/inline dialog in Oracle APEX using JS

Analysis: From Oracle Apex 4.2,  Inline dialog can be opened as modal dialog/popup using JS. Open inline dialog region as modal dialog: Step 1: Create static region. Step 2:  Set Template => Inline Dialog. Step 3:  Assign the static id to the region. Fig 1: Set template and Static ID Step 4:  Create button or change one of the report column type to Link. (Action: Redirect URL). Step 5:  Put the below JS to open the region as modal dialog.                javascript:openModal('unitpricedetails');            Fig 2:  Open simple modal dialog Open inline dialog region as modal dialog and pass values to the items: Common Steps:  Step 1 to Step 4. Step 5:  To pass value to that region, create P1_UNIT_ID in the inline  dialog.       javascript:$s('P1_UNIT_ID','#UNIT_ID#');javascript:openModal('unitpricedetai...