Loading JSON Data using External Tables in Oracle Database 23ai/26ai
In Oracle database 23ai/26ai loading JSON data using external tables got much simpler.
oracle 23configurationintermediate
by OracleDba
26 views
In Oracle database 23ai/26ai loading JSON data using external tables got much simpler.
123456
conn sys/SysPassword1@//localhost:1521/freepdb1 as sysdba
create or replace directory tmp_dir as '/tmp';
grant read, write on directory tmp_dir to testuser1;
conn testuser1/testuser1@//localhost:1521/freepdb1123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
cat > /tmp/fruit_array.json <<EOF
[
{"fruit":"apple", "quantity": 20},
{"fruit":"orange", "quantity": 10},
{"fruit":"banana", "quantity": 15}
]
EOF
drop table if exists json_ext;
create table json_ext (
data json
)
organization external
(type oracle_bigdata
access parameters (com.oracle.bigdata.fileformat = jsondoc)
location (tmp_dir:'fruit_array.json'))
parallel
reject limit unlimited;
select json_serialize(data pretty)
from json_ext;
JSON_SERIALIZE(DATAPRETTY)
--------------------------------------------------------------------------------
[
{
"fruit" : "apple",
"quantity" : 20
},
{
"fruit" : "orange",
"quantity" : 10
},
{
"fruit" : "banana",
"quantity" : 15
}
]
1 row selected.
SQL>
drop table if exists json_ext;
create table json_ext (
data json
)
organization external
(type oracle_bigdata
access parameters (
com.oracle.bigdata.json.path = '$[*]'
com.oracle.bigdata.fileformat = jsondoc
)
location (tmp_dir:'fruit_array.json'))
parallel
reject limit unlimited;
select json_serialize(data pretty)
from json_ext;
JSON_SERIALIZE(DATAPRETTY)
--------------------------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 20
}
{
"fruit" : "orange",
"quantity" : 10
}
{
"fruit" : "banana",
"quantity" : 15
}
3 rows selected.
SQL>
cat > /tmp/fruit_array2.json <<EOF
{
"rows": [
{"fruit":"apple", "quantity": 20},
{"fruit":"orange", "quantity": 10},
{"fruit":"banana", "quantity": 15}
]
}
EOF
drop table if exists json_ext;
create table json_ext (
data json
)
organization external
(type oracle_bigdata
access parameters (
com.oracle.bigdata.json.path = '$.rows[*]'
com.oracle.bigdata.fileformat = jsondoc
)
location (tmp_dir:'fruit_array2.json'))
parallel
reject limit unlimited;
select json_serialize(data pretty)
from json_ext;
JSON_SERIALIZE(DATAPRETTY)
--------------------------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 20
}
{
"fruit" : "orange",
"quantity" : 10
}
{
"fruit" : "banana",
"quantity" : 15
}
3 rows selected.
SQL>12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
cat > /tmp/fruit_file1.json <<EOF
{"fruit":"apple", "quantity": 20}
EOF
cat > /tmp/fruit_file2.json <<EOF
{"fruit":"orange", "quantity": 10}
EOF
cat > /tmp/fruit_file3.json <<EOF
{"fruit":"banana", "quantity": 15}
EOF
drop table if exists json_ext;
create table json_ext (
data json
)
organization external
(type oracle_bigdata
access parameters (com.oracle.bigdata.fileformat = jsondoc)
location (tmp_dir:'fruit_file*.json'))
parallel
reject limit unlimited;
SELECT json_serialize(data pretty)
FROM json_ext;
JSON_SERIALIZE(DATAPRETTY)
--------------------------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 20
}
{
"fruit" : "orange",
"quantity" : 10
}
{
"fruit" : "banana",
"quantity" : 15
}
3 rows selected.
SQL>1234567891011121314151617181920212223242526272829303132
cat > /tmp/fruit_multidoc.json <<EOF
{"fruit":"apple", "quantity": 20}
{"fruit":"orange", "quantity": 10}
{"fruit":"banana", "quantity": 15}
EOF
SELECT json_serialize(data pretty)
FROM json_ext external modify (
location (tmp_dir:'fruit_multidoc.json')
);
JSON_SERIALIZE(DATAPRETTY)
--------------------------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 20
}
{
"fruit" : "orange",
"quantity" : 10
}
{
"fruit" : "banana",
"quantity" : 15
}
3 rows selected.
SQL>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
cat > /tmp/fruit_multidoc_multiline.json <<EOF
{
"fruit":"apple",
"quantity": 20,
"extra_info": {"val1": "one",
"val2": "two"}
}
{
"fruit":"orange",
"quantity": 10,
"extra_info": {"val1": "three",
"val2": "four"}
}
{
"fruit":"banana",
"quantity": 15,
"extra_info": {"val1": "five",
"val2": "six"}
}
EOF
SELECT json_serialize(data pretty)
FROM json_ext external modify (
location (tmp_dir:'fruit_multidoc_multiline.json')
);
JSON_SERIALIZE(DATAPRETTY)
--------------------------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 20,
"extra_info" :
{
"val1" : "one",
"val2" : "two"
}
}
{
"fruit" : "orange",
"quantity" : 10,
"extra_info" :
{
"val1" : "three",
"val2" : "four"
}
}
{
"fruit" : "banana",
"quantity" : 15,
"extra_info" :
{
"val1" : "five",
"val2" : "six"
}
}
3 rows selected.
SQL>1234567891011121314151617181920212223242526272829303132333435
-- JSON column
drop table if exists json_data_table;
create table json_data_table (
id number generated always as identity,
json_data json
);
insert into json_data_table (json_data)
select data from json_ext;
commit;
-- Relational table.
drop table if exists relational_data_table;
create table relational_data_table (
id number generated always as identity,
fruit varchar2(10),
quantity number
);
-- Dot Notation
insert into relational_data_table(fruit, quantity)
select je.data.fruit,
je.data.quantity
from json_ext je;
-- JSON_VALUE
insert into relational_data_table(fruit, quantity)
select json_value(data, '$.fruit'),
json_value(data, '$.quantity')
from json_ext;
commit;Please to add comments
No comments yet. Be the first to comment!