我需要将Linux文件系统上的JSON文件加载到oracle数据库表。
我需要将JSON文件的每一列加载到适当的列中 在oracle数据库表中。
有没有javascript可以做到这一点?
答案 0 :(得分:0)
使用12.1.0.2.0或更高版本给出像这样的JSON文档
{
"PONumber": 2286,
"Reference": "ABANDA-20140803",
"Requestor": "Amit Banda",
"User": "ABANDA",
"CostCenter": "A80",
"ShippingInstructions": {
"name": "Amit Banda",
"Address": {
"street": "Magdalen Centre, The Isis Science Park",
"city": "Oxford",
"county": "Oxon.",
"postcode": "OX9 9ZB",
"country": "United Kingdom"
},
"Phone": [
{
"type": "Office",
"number": "861-555-4886"
}
]
},
"Special Instructions": "Hand Carry",
"LineItems": [
{
"ItemNumber": 1,
"Part": {
"Description": "Cookie's Fortune",
"UnitPrice": 19.95,
"UPCCode": 44004499323
},
"Quantity": 4.0
}, {
"ItemNumber": 2,
"Part": {
"Description": "A Bright Shining Lie",
"UnitPrice": 19.95,
"UPCCode": 26359122026
},
"Quantity": 4.0
}, {
"ItemNumber": 3,
"Part": {
"Description": "Karaoke: 25 Song Country Library Vol.1 201",
"UnitPrice": 19.95,
"UPCCode": 13023006096
},
"Quantity": 7.0
}, {
"ItemNumber": 4,
"Part": {
"Description": "Red Skelton: Lost Episodes",
"UnitPrice": 19.95,
"UPCCode": 18713811172
},
"Quantity": 4.0
}, {
"ItemNumber": 5,
"Part": {
"Description": "Stealing Home",
"UnitPrice": 19.95,
"UPCCode": 85391181828
},
"Quantity": 9.0
}
]
}
你可以这样做......
G:\JSON\examples\loadDocumentToTable>sqlplus system/oracle@PDB12201 @loadDocumentToTable %CD% purchaseOrder.json
SQL*Plus: Release 12.2.0.1.0 Production on Thu May 25 22:34:14 2017
Copyright (c) 1982, 2016, Oracle. All rights reserved.
Last Successful login time: Thu May 25 2017 22:33:29 -07:00
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
SQL> spool loadDocumentToTables.log
SQL> --
SQL> def USERNAME = JSON2RELATIONAL
SQL> --
SQL> def PASSWORD = &USERNAME
SQL> --
SQL> def JSONDIR = &1
SQL> --
SQL> def JSONFILE = &2
SQL> --
SQL> drop user &USERNAME cascade
2 /
old 1: drop user &USERNAME cascade
new 1: drop user JSON2RELATIONAL cascade
User dropped.
SQL> grant connect, resource, create any directory, unlimited tablespace to &USERNAME identified by &PASSWORD
2 /
old 1: grant connect, resource, create any directory, unlimited tablespace to &USERNAME identified by &PASSWORD
new 1: grant connect, resource, create any directory, unlimited tablespace to JSON2RELATIONAL identified by JSON2RELAT
IONAL
Grant succeeded.
SQL> connect &USERNAME/&PASSWORD@&_connect_identifier
Connected.
SQL> --
SQL> create or replace directory JSONDIR as '&JSONDIR'
2 /
old 1: create or replace directory JSONDIR as '&JSONDIR'
new 1: create or replace directory JSONDIR as 'G:\JSON\examples\loadDocumentToTable'
Directory created.
SQL> create table PURCHASEORDER_MASTER(
2 PO_NUMBER NUMBER(10)
3 ,REQUESTOR VARCHAR2(128 CHAR)
4 ,USERID VARCHAR2(10 CHAR)
5 ,COSTCENTER VARCHAR2(16)
6 ,SHIP_TO_NAME VARCHAR2(20 CHAR)
7 ,SHIP_TO_STREET VARCHAR2(38 CHAR)
8 ,SHIP_TO_CITY VARCHAR2(32 CHAR)
9 ,SHIP_TO_COUNTY VARCHAR2(32 CHAR)
10 ,SHIP_TO_POSTCODE VARCHAR2(32 CHAR)
11 ,SHIP_TO_STATE VARCHAR2(2 CHAR)
12 ,SHIP_TO_PROVINCE VARCHAR2(2 CHAR)
13 ,SHIP_TO_ZIP VARCHAR2(8 CHAR)
14 ,SHIP_TO_COUNTRY VARCHAR2(32 CHAR)
15 ,SHIP_TO_PHONE VARCHAR2(24 CHAR)
16 ,INSTRUCTIONS VARCHAR2(2048 CHAR)
17 )
18 /
Table created.
SQL> create table PURCHASEORDER_LINEITEM(
2 PO_NUMBER NUMBER(10)
3 ,ITEMNO NUMBER(38)
4 ,DESCRIPTION VARCHAR2(256 CHAR)
5 ,UPCCODE VARCHAR2(14 CHAR)
6 ,QUANTITY NUMBER(12,4)
7 ,UNITPRICE NUMBER(14,2)
8 )
9 /
Table created.
SQL> insert all
2 WHEN (SEQ# = 1) -- Only for the first row output by JSONTABLE
3 THEN INTO PURCHASEORDER_MASTER(
4 PO_NUMBER,REQUESTOR,USERID,COSTCENTER,SHIP_TO_NAME,SHIP_TO_STREET,SHIP_TO_CITY,SHIP_TO_COUNTY,SHIP_TO
_POSTCODE,SHIP_TO_STATE,SHIP_TO_PROVINCE ,SHIP_TO_ZIP,SHIP_TO_COUNTRY,SHIP_TO_PHONE,INSTRUCTIONS
5 )
6 VALUES(
7 PO_NUMBER,REQUESTOR,USERID,COSTCENTER,SHIP_TO_NAME,SHIP_TO_STREET,SHIP_TO_CITY,SHIP_TO_COUNTY,SHIP_TO
_POSTCODE,SHIP_TO_STATE,SHIP_TO_PROVINCE ,SHIP_TO_ZIP,SHIP_TO_COUNTRY,SHIP_TO_PHONE,INSTRUCTIONS
8 )
9 WHEN (1=1) -- For all rows output by JSONTABLE
10 THEN INTO PURCHASEORDER_LINEITEM(
11 PO_NUMBER,ITEMNO,DESCRIPTION,UPCCODE,QUANTITY,UNITPRICE
12 )
13 VALUES(
14 PO_NUMBER,ITEMNO,DESCRIPTION,UPCCODE,QUANTITY,UNITPRICE
15 )
16 SELECT *
17 FROM JSON_TABLE(
18 BFILENAME('JSONDIR','PurchaseOrder.json'),
19 '$'
20 COLUMNS(
21 PO_NUMBER NUMBER(10) path '$.PONumber',
22 REFERENCE VARCHAR2(30 CHAR) path '$.Reference',
23 REQUESTOR VARCHAR2(128 CHAR) path '$.Requestor',
24 USERID VARCHAR2(10 CHAR) path '$.User',
25 COSTCENTER VARCHAR2(16) path '$.CostCenter',
26 SHIP_TO_NAME VARCHAR2(20 CHAR) path '$.ShippingInstructions.name',
27 SHIP_TO_STREET VARCHAR2(32 CHAR) path '$.ShippingInstructions.Address.street',
28 SHIP_TO_CITY VARCHAR2(32 CHAR) path '$.ShippingInstructions.Address.city',
29 SHIP_TO_COUNTY VARCHAR2(32 CHAR) path '$.ShippingInstructions.Address.county',
30 SHIP_TO_POSTCODE VARCHAR2(10 CHAR) path '$.ShippingInstructions.Address.postcode',
31 SHIP_TO_STATE VARCHAR2(2 CHAR) path '$.ShippingInstructions.Address.state',
32 SHIP_TO_PROVINCE VARCHAR2(2 CHAR) path '$.ShippingInstructions.Address.province',
33 SHIP_TO_ZIP VARCHAR2(8 CHAR) path '$.ShippingInstructions.Address.zipCode',
34 SHIP_TO_COUNTRY VARCHAR2(32 CHAR) path '$.ShippingInstructions.Address.country',
35 SHIP_TO_PHONE VARCHAR2(24 CHAR) path '$.ShippingInstructions.Phones[0].number',
36 INSTRUCTIONS VARCHAR2(2048 CHAR) path '$.SpecialInstructions',
37 NESTED PATH '$.LineItems[*]'
38 columns (
39 SEQ# for ordinality,
40 ITEMNO NUMBER(38) path '$.ItemNumber',
41 DESCRIPTION VARCHAR2(256 CHAR) path '$.Part.Description',
42 UPCCODE VARCHAR2(14 CHAR) path '$.Part.UPCCode',
43 QUANTITY NUMBER(12,4) path '$.Quantity',
44 UNITPRICE NUMBER(14,2) path '$.Part.UnitPrice'
45 )
46 )
47 )
48 /
6 rows created.
SQL> set lines 256 trimspool off pages 50
SQL> COLUMN DESCRIPTION FORMAT A32
SQL> COLUMN INSTRUCTIONS FORMAT A32
SQL> --
SQL> select *
2 FROM PURCHASEORDER_MASTER
3 /
PO_NUMBER REQUESTOR
USERID COSTCENTER SHIP_TO_NAME SHIP_TO_STREET
---------- -------------------------------------------------------------------------------------------------------------
------------------- ---------- ---------------- -------------------- --------------------------------------
SHIP_TO_CITY SHIP_TO_COUNTY SHIP_TO_POSTCODE SH SH SHIP_TO_ SHIP_T
O_COUNTRY SHIP_TO_PHONE INSTRUCTIONS
-------------------------------- -------------------------------- -------------------------------- -- -- -------- ------
-------------------------- ------------------------ --------------------------------
2286 Amit Banda
ABANDA A80 Amit Banda Magdalen Centre, The Isis Science Park
Oxford Oxon. OX9 9ZB United Kingdom
SQL> select *
2 FROM PURCHASEORDER_LINEITEM
3 /
PO_NUMBER ITEMNO DESCRIPTION UPCCODE QUANTITY UNITPRICE
---------- ---------- -------------------------------- -------------- ---------- ----------
2286 1 Cookie's Fortune 44004499323 4 19.95
2286 2 A Bright Shining Lie 26359122026 4 19.95
2286 3 Karaoke: 25 Song Country Library 13023006096 7 19.95
Vol.1 201
2286 4 Red Skelton: Lost Episodes 18713811172 4 19.95
2286 5 Stealing Home 85391181828 9 19.95
SQL> exit
Disconnected from Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
G:\JSON\examples\loadDocumentToTable>