Skip to content

Commit cfa4f64

Browse files
Add files via upload
1 parent 1c2c71b commit cfa4f64

3 files changed

Lines changed: 252 additions & 0 deletions

File tree

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
-- required credential creation
2+
begin
3+
utl_json_cloud.create_or_replace_credential(p_credential_name => 'JSON_DEMO_CREDENTIAL',
4+
p_username => 'username',
5+
p_password => 'authentication_token');
6+
end;
7+
/
8+
9+
10+
-- "external json collection" i.e. pair of external table pointing to some json data and json collection view on top of this
11+
begin
12+
utl_json_cloud.create_external_json_collection(p_collection_name => 'iot_events_external',
13+
p_credential_name => 'JSON_DEMO_CREDENTIAL',
14+
p_file_list => 'bucket_url/dir*/*.json');
15+
end;
16+
/
17+
18+
select *
19+
from iot_events_external;
20+
21+
select *
22+
from iot_events_external_ext;
23+
24+
select *
25+
from iot_events;
26+
27+
drop table if exists iot_events_part;
28+
29+
30+
-- json collection partitioned table
31+
create json collection table iot_events_part
32+
partition by range (json_value(DATA, '$.eventDate' RETURNING DATE ERROR ON ERROR))
33+
interval (numtodsinterval(1,'DAY'))
34+
(
35+
partition p_before_2025 values less than (DATE '2025-01-01')
36+
);
37+
38+
-- loading data from "external json collection"
39+
insert /*+ append parallel(16) */ into iot_events_part
40+
select *
41+
from iot_events_external;
42+
43+
select * from iot_events_part iot
44+
WHERE json_value(iot.DATA, '$.eventDate' RETURNING DATE ERROR ON ERROR) >= DATE '2025-01-30'
45+
AND json_value(iot.DATA, '$.eventDate' RETURNING DATE ERROR ON ERROR) < DATE '2025-01-31';
46+
47+
select partition_name, high_value
48+
from user_tab_partitions where table_name = 'IOT_EVENTS_PART';
49+
50+
declare
51+
part_URL VARCHAR2(4000);
52+
begin
53+
part_URL := 'bucket_url/year=2025/month=01';
54+
dbms_cloud.export_data(
55+
credential_name => 'JSON_DEMO_CREDENTIAL',
56+
file_uri_list => part_URL || '/day=01/2025-01-01_events.json',
57+
query => q'[
58+
select
59+
DATA
60+
FROM iot_events_part c
61+
WHERE json_value(iot.DATA, '$.eventDate' RETURNING DATE ERROR ON ERROR) >= DATE '2025-01-01'
62+
AND json_value(iot.DATA, '$.eventDate' RETURNING DATE ERROR ON ERROR) < DATE '2025-01-02'
63+
]',
64+
format => json_object('type' VALUE 'json')
65+
);
66+
end;
67+
/
68+
69+
-- data export
70+
71+
declare
72+
part_URL VARCHAR2(4000);
73+
begin
74+
part_URL := ' part_URL := 'bucket_url/year=2025/month=01';
75+
dbms_cloud.export_data(
76+
credential_name => 'JSON_DEMO_CREDENTIAL',
77+
file_uri_list => part_URL || '/day=02/2025-01-02_events.json',
78+
query => q'[
79+
select
80+
DATA
81+
FROM iot_events_part iot
82+
WHERE json_value(iot.DATA, '$.eventDate' RETURNING DATE ERROR ON ERROR) >= DATE '2025-01-02'
83+
AND json_value(iot.DATA, '$.eventDate' RETURNING DATE ERROR ON ERROR) < DATE '2025-01-03'
84+
]',
85+
format => json_object('type' VALUE 'json')
86+
);
87+
end;
88+
/
89+
90+
declare
91+
part_URL VARCHAR2(4000);
92+
begin
93+
part_URL := part_URL := 'bucket_url/year=2025/month=01';
94+
dbms_cloud.export_data(
95+
credential_name => 'JSON_DEMO_CREDENTIAL',
96+
file_uri_list => part_URL || '/day=03/2025-01-03_events.json',
97+
query => q'[
98+
select
99+
DATA
100+
FROM iot_events_part iot
101+
WHERE json_value(iot.DATA, '$.eventDate' RETURNING DATE ERROR ON ERROR) >= DATE '2025-01-03'
102+
AND json_value(iot.DATA, '$.eventDate' RETURNING DATE ERROR ON ERROR) < DATE '2025-01-04'
103+
]',
104+
format => json_object('type' VALUE 'json')
105+
);
106+
end;
107+
/
108+
109+
-- cold data "external json collection view" creation
110+
begin
111+
utl_json_cloud.create_external_json_collection(p_collection_name => 'iot_events_cold_data',
112+
p_credential_name => 'JSON_DEMO_CREDENTIAL',
113+
p_file_list => 'bucket_url/year*/month*/day*/*.json');
114+
end;
115+
/
116+
117+
select *
118+
from iot_events_cold_data;
119+
120+
-- old data removal
121+
alter table iot_events_part
122+
drop partition for (DATE '2025-01-01')
123+
update indexes;
124+
125+
alter table iot_events_part
126+
drop partition for (DATE '2025-01-02')
127+
update indexes;
128+
129+
alter table iot_events_part
130+
drop partition for (DATE '2025-01-03')
131+
update indexes;
132+
133+
-- create a view on top of "external" and "internal" json collections
134+
create or replace json collection view iot_events
135+
as
136+
select *
137+
from iot_events_part
138+
union all
139+
select *
140+
from iot_events_cold_data;
141+
142+
select *
143+
from iot_events;
144+
145+
146+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
create or replace package body utl_json_cloud
2+
is
3+
function is_autonomous return boolean
4+
is
5+
num_of_rows number(1);
6+
begin
7+
select count(*)
8+
into num_of_rows
9+
from dba_pdbs
10+
where cloud_identity like '%AUTONOMOUS%';
11+
12+
if num_of_rows > 0 then
13+
return true;
14+
end if;
15+
return false;
16+
end;
17+
18+
procedure create_credential(p_credential_name varchar2,
19+
p_username varchar2,
20+
p_password varchar2)
21+
is
22+
begin
23+
if is_autonomous then
24+
dbms_cloud.create_credential(credential_name => p_credential_name,
25+
username => p_username,
26+
password => p_password);
27+
else
28+
dbms_credential.create_credential(credential_name => p_credential_name,
29+
username => p_username,
30+
password => p_password);
31+
end if;
32+
end;
33+
34+
procedure drop_credential(p_credential_name varchar2)
35+
is
36+
begin
37+
if is_autonomous then
38+
dbms_cloud.drop_credential(credential_name => p_credential_name);
39+
else
40+
dbms_credential.drop_credential(credential_name => p_credential_name);
41+
end if;
42+
end;
43+
44+
procedure create_or_replace_credential(p_credential_name varchar2,
45+
p_username varchar2,
46+
p_password varchar2)
47+
is
48+
begin
49+
begin
50+
drop_credential(p_credential_name);
51+
exception
52+
when e_non_existing_credential then
53+
null;
54+
end;
55+
create_credential(p_credential_name,p_username,p_password);
56+
end;
57+
58+
procedure create_external_json_collection(p_collection_name varchar2,
59+
p_credential_name varchar2,
60+
p_file_list clob)
61+
is
62+
begin
63+
dbms_cloud.create_external_table(table_name => p_collection_name||'_ext',
64+
credential_name => p_credential_name,
65+
file_uri_list => p_file_list,
66+
format => json_object('type' value 'jsondoc'));
67+
execute immediate 'create or replace json collection view '||p_collection_name||' as select DATA from '||p_collection_name||'_ext';
68+
end;
69+
70+
procedure drop_external_json_collection(p_collection_name varchar2)
71+
is
72+
begin
73+
execute immediate 'drop view '||p_collection_name;
74+
execute immediate 'drop table '||p_collection_name||'_ext';
75+
end;
76+
77+
end;
78+
/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
create or replace package utl_json_cloud
2+
is
3+
e_non_existing_credential exception;
4+
pragma exception_init(e_non_existing_credential,-20004);
5+
6+
function is_autonomous return boolean;
7+
8+
procedure create_credential(p_credential_name varchar2,
9+
p_username varchar2,
10+
p_password varchar2);
11+
12+
procedure drop_credential(p_credential_name varchar2);
13+
14+
procedure create_or_replace_credential(p_credential_name varchar2,
15+
p_username varchar2,
16+
p_password varchar2);
17+
18+
procedure create_external_json_collection(p_collection_name varchar2,
19+
p_credential_name varchar2,
20+
p_file_list clob);
21+
22+
procedure drop_external_json_collection(p_collection_name varchar2);
23+
24+
end;
25+
/
26+
27+
28+

0 commit comments

Comments
 (0)