Run SQL Directly Against Files in Cloud Object Store with APEX

Search for a command to run...

No comments yet. Be the first to comment.
Oracle APEX ideas and insights that you can read in four minutes or less.
In this short, I will show you how to configure secure access between Oracle APEX and Oracle Cloud Infrastructure (OCI) Object Storage. This access allows your APEX applications and PL/SQL code to securely GET, DELETE and POST files from/to OCI Objec...
Introduction One of the marquee features of APEX 26.1 was AI Interactive Reports. When I started testing this new feature, I was interested to see what was going on behind the scenes. In this post, I

Introduction One of the marquee features of APEX 26.1 is APEX AI Agents. APEX AI Agents allow you to use an LLM and your own PL/SQL and JS tools to perform actions on your data instead of just chattin

Introduction In my previous post, I looked at logging APEX AI Agent requests and responses with Request and Response handlers. Logging is the first step because it shows you what is actually moving th

Introduction Logging is one of the first things you need when building serious AI Agents. Without it, debugging quickly becomes guesswork. You can see the final answer, but not always why the model ch

Context management patterns from building agents in Oracle APEX

Did you know that using APEX PL/SQL APIs, you can run SQL queries against flat files stored in Cloud Object Storage? In this short, I will take you through an example of how you can do this with Oracle Cloud Infrastructure (OCI) Object Store. The principles are the same for other object stores, e.g., AWS S3, Microsoft SharePoint, etc.
Pre-Requisites
APEX_OCI_BLOG_FILEShttps://objectstorage.us-ashburn-1.oraclecloud.com/n/XXXX/b/APEX_OCI_BLOG_FILES/o/results.csvAPEX_OCI_BLOG_CREDENTIAL, which has read access to the OCI bucketWe will work with two APEX PL/SQL APIs, which make querying files in cloud object stores possible. These are apex_data_parser and apex_web_service.
The first thing we need to do is understand the file's structure.
SELECT *
FROM apex_data_parser.get_columns
(apex_data_parser.discover
(p_content => apex_web_service.make_rest_request_b
(p_url => 'https://objectstorage.us-ashburn-1.oraclecloud.com/n/XXXX/b/APEX_OCI_BLOG_FILES/o/results.csv',
p_http_method => 'GET',
p_credential_static_id => 'APEX_OCI_BLOG_CREDENTIAL'),
p_max_rows => 1000,
p_file_name =>'test.csv' ))
APEX_OCI_BLOG_CREDENTIAL is used to authenticate into OCI Object Storage
Web Credentials can be used with
apex_web_serviceoutside of the context of an APEX Session, as long as the schema is mapped to an APEX Workspace. If the schema is mapped to multiple workspaces, you must first callAPEX_UTIL.SET_WORKSPACEorAPEX_UTIL.SET_SECURITY_GROUP_ID. See documentation for details.
Now that we know the file's structure, we can run queries against it. In the SQL below, I am fetching the win, loss, and draw record for Wales 🏴. BTW, at the time of writing this post, it is 221 wins, 315 Losses, and 151 draws 😔
WITH scores AS
(SELECT line_number
, TO_DATE(col001,'YYYY"-"MM"-"DD') date_played
, col002 home_team
, col003 away_team
, TO_NUMBER(col004) home_score
, TO_NUMBER(col005) away_score
, col006 tournament
, col007 city
, col008 country
FROM apex_data_parser.parse
(p_content => apex_web_service.make_rest_request_b
(p_url => 'https://objectstorage.us-ashburn-1.oraclecloud.com/n/XXXX/b/APEX_OCI_BLOG_FILES/o/results.csv',
p_http_method => 'GET',
p_credential_static_id => 'APEX_OCI_BLOG_CREDENTIAL'),
p_skip_rows => 1,
p_detect_data_types => 'N',
p_file_name => 'test.csv'))
SELECT SUM(CASE
WHEN home_team = 'Wales' AND home_score > away_score THEN 1
WHEN away_team = 'Wales' AND away_score > home_score THEN 1
ELSE 0
END) number_of_wins
, SUM(CASE
WHEN home_team = 'Wales' AND home_score < away_score THEN 1
WHEN away_team = 'Wales' AND away_score < home_score THEN 1
ELSE 0
END) number_of_losses
, SUM(CASE
WHEN home_team = 'Wales' AND home_score = away_score THEN 1
WHEN away_team = 'Wales' AND away_score = home_score THEN 1
ELSE 0
END) number_of_draws
FROM scores
WHERE (home_team = 'Wales' OR away_team = 'Wales')
I showed you how to query a CSV file stored in Oracle Object Storage using APEX PL/SQL APIs. You can use the same APIs to query Excel, JSON, and XML files.
You can run these same SQL statements from APEX Components such as Interactive Reports, Classic Reports, etc. You can incorporate this technique into PL/SQL packages for more complex logic.