|
| 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 | +
|
0 commit comments