JSON und Oracle12c 
Die Oracle-Datenbank als JSON Document Store 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
Carsten Czarski 
Business Unit Database 
Oracle Deutschland B.V. & Co KG
Oracle12c für Entwickler 
Die Datenbank für alle wichtigen Plattformen 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
JSON 
Ruby 
Oracle ADF Oracle APEX 
Oracle RDS
JSON: JavaScript Object Notation 
• Sehr einfaches Datenaustauschformat 
• Einfachere Konzepte als XML 
• Basiert auf JavaScript-Code 
• Javascript auch auf Server wichtig: 
node.js 
• Breite Unterstützung durch Web-APIs 
– Google, Twitter, Facebook & Co 
• Setzt sich mehr und mehr durch 
{ 
"PONumber" : 1600, 
"Reference" : "ABULL-20140421“, 
"Requestor" : "Alexis Bull“, 
"User" : "ABULL“, 
"CostCenter" : "A50“, 
"ShippingInstructions" : { 
"name" : "Alexis Bull“, 
"Address" : { ... }, 
"Phone" : [ ... ] 
}, 
"Special Instructions" : null, 
"AllowPartialShipment" : true, 
"LineItems" : [{ 
"ItemNumber" : 1, 
"Part" : { 
"Description" : "One Magic Christmas“, 
"UnitPrice" : 19.95, 
"UPCCode" : 13131092899 
}, 
"Quantity" : 9 
}, 
{…} 
] 
} 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Flexible Entwicklung mit JSON 
• Agilität und "Continous Delivery" 
– Änderungen an Datenstrukturen immer häufiger und flexibler 
– Lange Zyklen für Schema-Änderungen werden immer weniger akzeptiert 
Security ...? 
IT Betrieb ...? 
Replikation ...? 
• Ansatz: Ablage als JSON und Parsing zur Query Zeit 
– Einfache Ablage der JSON-Dokumente in einem 'Document Store' 
– Parsing zur Abfragezeit – fehlende Attribute "fehlen dann einfach" 
– NoSQL-Datenbanken, Hadoop Cluster 
Funktionsbibliothek ...? 
Relationale Applikationen ...? 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Relationale Tabellen bleiben wichtig. 
Auf das Zusammenspiel kommt es an. 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
5
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
JSON and SQL Duality 
JSON 
Datenzugriff per 
REST oder 
Native API 
JSON wird in der 
Datenbank gespeichert 
Analysen / Queries per SQL 
Oracle Database 12c 
SQL
Native JSON Unterstützung in der Oracle Datenbank 
 JSON Dokumente speichern und verwalten 
• JSON als Text speichern (VARCHAR2, CLOB, BLOB)  Ohne Schema - volle Flexibilität 
• Kein eigener Datentyp: Nutzung der Standard-Datentypen für Zeichenketten bzw. Binärdaten 
• Indizierung mit einem 'JSON aware' Index 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
 API Zugriff auf JSON 
• REST Services 
• Java API (weitere Programmiersprachen in Planung) 
 SQL Abfragen direkt auf JSON-Dokumente 
 Reporting, Analyse und relationaler Zugriff direkt auf JSON-Dokumente 
 'JSON and SQL duality' 
7 
JSON ermöglicht schemalose Datenhaltung 
in der Oracle-Datenbank ... 
... aber komplett integriert mit der 
relationalen Welt!
SQL Queries auf JSON-Dokumente: JSON Table 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
8
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
SQL Beispiel: JSON_TABLE 
9 
select reference, requestor, costcenter 
from po_json, json_table( 
json, 
'$.PurchaseOrder' 
columns ( 
reference varchar2(30) path '$.Reference', 
requestor varchar2(25) path '$.Requestor', 
costcenter varchar2(4) path '$.CostCenter' 
) 
) 
/
JSON_TABLE für relationale Sichten auf JSON 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
10 
REFERENCE REQUESTOR COST 
--------------------------- ------------------ ---- 
FORD-20021009123336872PDT Gerry B. Ford R20 
JONES-20011127121050471PST Richard J Jones R20 
MARTIN-20011127121050401PST Thomas D. Martin S30 
CLARK-20021009123337764PDT Edward K. Clark A10 
JONES-20011127121042590PST Richard J Jones R20 
MARTIN-20011127121040547PST Thomas D. Martin S30 
JONES-20021009123336602PDT Richard J Jones R20 
WARD-20011127121050511PST Stephen B. Ward S30 
BLAKE-20011127121055689PST David E. Blake S30 
MARTIN-20011127121048889PST Thomas D. Martin S30 
: : :
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
JSON Indizierung 
• Function Based Indizes für … 
– Bekannte Abfragemuster 
– Attribute kommen im JSON nur einmal vor – keine Arrays 
– Basiert auf Function Based Indexes 
• Volltextindizierung von JSON-Dokumenten auf Basis von Oracle TEXT 
– Unbekannte Abfragemuster – inkl. Volltextrecherche 
– Beliebige JSON-Attribute 
– Basiert auf Oracle TEXT
JSON Unterstützung in Oracle12c: Interfaces und APIs 
 Neue 'Document-Store' API für Entwickler 
– REST Webservices 
– Document / Collection APIs für JAVA 
– Unterstützung für andere Sprachen / Frameworks in Arbeit 
• SQL Zugriff uneingeschränkt möglich 
– Treiber wie OCI, JDBC, .NET können sofort mit JSON umgehen 
– SQL/JSON Operatoren erlauben direktes Arbeiten mit SQL 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
REST API für JSON 
• REST Webservice API zur Arbeit mit JSON-Dokumenten: Kein SQL nötig 
• Zwei Deployment-Varianten: 
– Oracle Rest Data Services (aka 'APEX Listener') 
– Oracle XML DB Protokollserver (XML DB) 
• Unterstützt die REST Standardoperationen 
– PUT : Create / Update 
– GET : Retrieve 
– DELETE : Delete 
– POST : JSON Queries, Utility-Funktionen 
• Konfiguration des REST Service wird komplett in der Datenbank gespeichert 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
Roadmap 
• JSON mit SQL-Funktionen generieren 
• JSON Support für PL/SQL 
– Für APEX-Entwickler wird mit APEX 5.0 das Paket APEX_JSON kommen.
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
Weitere Informationen 
• Oracle Dokumentation 
– XML DB Developers Guide 
– Kapitel 39: JSON in Oracle Database 
• Blog des Autors: SQL und PL/SQL in Oracle 
– http://sql-plsql-de.blogspot.com 
– Juli 2014: Oracle 12.1.0.2 ist da: Ein Einblick in die JSON-Unterstützung 
15
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
Carsten.Czarski@oracle.com 
http://tinyurl.com/apexcommunity 
http://sql-plsql-de.blogspot.com 
http://plsqlexecoscomm.sourceforge.net 
http://plsqlmailclient.sourceforge.net 
Twitter: @cczarski @oraclebudb
JSON in der Oracle-Datenbank 
Vorgehensweise und Funktionen im Detail 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
17
• Für JSON wird kein eigener Datentyp verwendet 
• Alle Typen, die Text aufnehmen können, sind nutzbar 
VARCHAR2, CLOB, BLOB 
• JSON- und Nicht-JSON Inhalte können in einer Tabelle gemischt werden 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
Schritt 1: Tabelle anlegen 
18 
create table po_json ( 
filename varchar2(200), 
json clob 
);
Schritt 2: JSON Dokumente laden 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
19
Schritt 3: Was ist JSON, was ist nicht JSON 
• SQL Operatoren IS JSON und IS NOT JSON 
• Check Constraint stellt sicher, dass nur noch JSON gespeichert wird 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
20 
select count(*) 
from po_json where json is json; 
select count(*) 
from po_json where json is not json; 
alter table po_json add 
constraint ck_isjson check (json is json);
• SQL Funktionen JSON_EXISTS, JSON_VALUE, JSON_QUERY und JSON_TABLE 
• Arbeitsweise immer gleich 
– JSON-Tabellenspalte oder Literal 
– JSON Pfadausdruck (JSON Path Expression) 
– Returning Klausel 
– Error Handling 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
Schritt 4: SQL Abfragen ... 
21
JSON_QUERY extrahiert JSON "Fragmente" 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
22 
select json_query( 
json, 
'$.PurchaseOrder.ShippingInstructions' 
PRETTY 
) from po_json; 
JSON_QUERY 
-------------------------------------------------------------------------------- 
[ 
{ 
"name" : ["Gerry B. Ford"], 
"address" : ["100 Oracle ParkwayrnRedwood ShoresrnCArn94065rnUSA"], 
"telephone" : ["650 506 7100"] 
} 
] 
1 Zeile wurde ausgewählt.
Im Fehlerfall ... wird NULL zurückgegeben 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
23 
select json_query( 
json, 
'$.PurchaseOrder.ShippingInstrs' 
PRETTY 
) from po_json; 
JSON_QUERY 
-------------------------------------------------------------------------------- 
? 
1 Zeile wurde ausgewählt.
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
... oder eine Fehlermeldung. 
24 
select json_query( 
json, 
'$.PurchaseOrder.ShippingInstrs' 
PRETTY ERROR ON ERROR 
) from po_json; 
* 
FEHLER in Zeile 1: 
ORA-40462: Auswertung von JSON_QUERY hat keinen Wert ergeben
JSON_VALUE extrahiert "skalare Werte" 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
25 
select json_value( 
json, 
'$.PurchaseOrder.Reference[0]' 
RETURNING VARCHAR2 
) from po_json where rownum < 10; 
JSON_VALUE 
------------------------------------------------------------------ 
FORD-20021009123336872PDT 
JONES-20011127121050471PST 
MARTIN-20011127121050401PST 
CLARK-20021009123337764PDT 
:
JSON_EXISTS stellt fest, ob ein JSON-Pfad existiert 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
26 
select filename from po_json 
where json_exists( 
json, 
'$.PurchaseOrder.Reference' 
); 
FILENAME 
----------------------------------------------------------------- 
FORD-20021009123336872PDT.js 
JONES-20011127121050471PST.js 
MARTIN-20011127121050401PST.js 
CLARK-20021009123337764PDT.js 
:
JSON_TABLE für relationale Sichten auf JSON 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
27 
select reference, requestor, costcenter 
from po_json, json_table( 
json, 
'$.PurchaseOrder' 
columns ( 
reference varchar2(30) path '$.Reference[0]', 
requestor varchar2(25) path '$.Requestor[0]', 
costcenter varchar2(4) path '$.CostCenter[0]' 
) 
) 
/
JSON_TABLE für relationale Sichten auf JSON 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
28 
REFERENCE REQUESTOR COST 
--------------------------- ------------------ ---- 
FORD-20021009123336872PDT Gerry B. Ford R20 
JONES-20011127121050471PST Richard J Jones R20 
MARTIN-20011127121050401PST Thomas D. Martin S30 
CLARK-20021009123337764PDT Edward K. Clark A10 
JONES-20011127121042590PST Richard J Jones R20 
MARTIN-20011127121040547PST Thomas D. Martin S30 
JONES-20021009123336602PDT Richard J Jones R20 
WARD-20011127121050511PST Stephen B. Ward S30 
BLAKE-20011127121055689PST David E. Blake S30 
MARTIN-20011127121048889PST Thomas D. Martin S30 
: : :
JSON_TABLE für geschachtelte Strukturen 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
29 
select reference, requestor, num, descr, quantity 
From po_json, json_table( 
json, 
'$.PurchaseOrder' 
columns ( 
reference varchar2(30) path '$.Reference[0]', 
requestor varchar2(25) path '$.Requestor[0]', 
CostCenter varchar2(4) path '$.CostCenter[0]', 
nested path '$.LineItems[*].LineItem[*]' columns ( 
num number path '$."u0024".ItemNumber', 
descr varchar2(40) path '$.Description[0]', 
quantity number path '$.Part[0]."u0024"."Quantity"' 
) 
) 
)
JSON_TABLE für relationale Sichten auf JSON 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
30 
REFERENCE REQUESTOR NUM DESCR QUANTITY 
--------------------------- ------------------ ---- ---------------------------------------- -------- 
FORD-20021009123336872PDT Gerry B. Ford 1 Ordet 4 
FORD-20021009123336872PDT Gerry B. Ford 2 The Naked Kiss 3 
FORD-20021009123336872PDT Gerry B. Ford 3 Charade 2 
FORD-20021009123336872PDT Gerry B. Ford 4 Robocop 2 
FORD-20021009123336872PDT Gerry B. Ford 5 Cries and Whispers 4 
FORD-20021009123336872PDT Gerry B. Ford 6 Autumn Sonata 4 
FORD-20021009123336872PDT Gerry B. Ford 7 Getrud 3 
FORD-20021009123336872PDT Gerry B. Ford 8 And the Ship Sails on 1 
FORD-20021009123336872PDT Gerry B. Ford 9 Flesh for Frankenstein 4 
FORD-20021009123336872PDT Gerry B. Ford 10 The Hidden Fortress 2 
FORD-20021009123336872PDT Gerry B. Ford 11 The Importance of Being Earnest 3 
FORD-20021009123336872PDT Gerry B. Ford 12 The Vanishing 4 
FORD-20021009123336872PDT Gerry B. Ford 13 Cries and Whispers 4 
FORD-20021009123336872PDT Gerry B. Ford 14 Beauty and the Beast 2 
FORD-20021009123336872PDT Gerry B. Ford 15 And God Created Woman 1 
: : : : : 
FORD-20021009123336872PDT Gerry B. Ford 16 The Magic Flute 1 
20 Zeilen ausgewählt.
Schritt 5: Volltextindizierung mit Oracle TEXT 
• Erstellung eines Oracle TEXT Index für JSON 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
31 
CREATE INDEX po_search_idx ON po_json (json) 
INDEXTYPE IS CTXSYS.CONTEXT 
PARAMETERS ( 
'section group CTXSYS.JSON_SECTION_GROUP 
SYNC (ON COMMIT)' 
) 
/ 
Index wurde erstellt.
Schritt 5: Volltextindizierung mit Oracle TEXT 
• Erstellung eines Oracle TEXT Index für JSON 
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
32 
select filename from po_json 
WHERE json_textcontains( 
json, 
'$.PurchaseOrder.LineItems.LineItem.Description', 
'Magic' 
); 
TURNER-20021009123338565PDT.js 
CLARK-2001112712105223PST.js 
:
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
Weitere Informationen 
• Oracle Dokumentation 
– XML DB Developers Guide 
– Kapitel 39: JSON in Oracle Database 
• Blog des Autors: SQL und PL/SQL in Oracle 
– http://sql-plsql-de.blogspot.com 
– Juli 2014: Oracle 12.1.0.2 ist da: Ein Einblick in die JSON-Unterstützung 
33
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 
Carsten.Czarski@oracle.com 
http://tinyurl.com/apexcommunity 
http://sql-plsql-de.blogspot.com 
http://plsqlexecoscomm.sourceforge.net 
http://plsqlmailclient.sourceforge.net 
Twitter: @cczarski @oraclebudb

JSON in der Oracle12c Database

  • 1.
    JSON und Oracle12c Die Oracle-Datenbank als JSON Document Store Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Carsten Czarski Business Unit Database Oracle Deutschland B.V. & Co KG
  • 2.
    Oracle12c für Entwickler Die Datenbank für alle wichtigen Plattformen Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | JSON Ruby Oracle ADF Oracle APEX Oracle RDS
  • 3.
    JSON: JavaScript ObjectNotation • Sehr einfaches Datenaustauschformat • Einfachere Konzepte als XML • Basiert auf JavaScript-Code • Javascript auch auf Server wichtig: node.js • Breite Unterstützung durch Web-APIs – Google, Twitter, Facebook & Co • Setzt sich mehr und mehr durch { "PONumber" : 1600, "Reference" : "ABULL-20140421“, "Requestor" : "Alexis Bull“, "User" : "ABULL“, "CostCenter" : "A50“, "ShippingInstructions" : { "name" : "Alexis Bull“, "Address" : { ... }, "Phone" : [ ... ] }, "Special Instructions" : null, "AllowPartialShipment" : true, "LineItems" : [{ "ItemNumber" : 1, "Part" : { "Description" : "One Magic Christmas“, "UnitPrice" : 19.95, "UPCCode" : 13131092899 }, "Quantity" : 9 }, {…} ] } Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
  • 4.
    Flexible Entwicklung mitJSON • Agilität und "Continous Delivery" – Änderungen an Datenstrukturen immer häufiger und flexibler – Lange Zyklen für Schema-Änderungen werden immer weniger akzeptiert Security ...? IT Betrieb ...? Replikation ...? • Ansatz: Ablage als JSON und Parsing zur Query Zeit – Einfache Ablage der JSON-Dokumente in einem 'Document Store' – Parsing zur Abfragezeit – fehlende Attribute "fehlen dann einfach" – NoSQL-Datenbanken, Hadoop Cluster Funktionsbibliothek ...? Relationale Applikationen ...? Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
  • 5.
    Relationale Tabellen bleibenwichtig. Auf das Zusammenspiel kommt es an. Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 5
  • 6.
    Copyright © 2014Oracle and/or its affiliates. All rights reserved. | JSON and SQL Duality JSON Datenzugriff per REST oder Native API JSON wird in der Datenbank gespeichert Analysen / Queries per SQL Oracle Database 12c SQL
  • 7.
    Native JSON Unterstützungin der Oracle Datenbank  JSON Dokumente speichern und verwalten • JSON als Text speichern (VARCHAR2, CLOB, BLOB)  Ohne Schema - volle Flexibilität • Kein eigener Datentyp: Nutzung der Standard-Datentypen für Zeichenketten bzw. Binärdaten • Indizierung mit einem 'JSON aware' Index Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |  API Zugriff auf JSON • REST Services • Java API (weitere Programmiersprachen in Planung)  SQL Abfragen direkt auf JSON-Dokumente  Reporting, Analyse und relationaler Zugriff direkt auf JSON-Dokumente  'JSON and SQL duality' 7 JSON ermöglicht schemalose Datenhaltung in der Oracle-Datenbank ... ... aber komplett integriert mit der relationalen Welt!
  • 8.
    SQL Queries aufJSON-Dokumente: JSON Table Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 8
  • 9.
    Copyright © 2014Oracle and/or its affiliates. All rights reserved. | SQL Beispiel: JSON_TABLE 9 select reference, requestor, costcenter from po_json, json_table( json, '$.PurchaseOrder' columns ( reference varchar2(30) path '$.Reference', requestor varchar2(25) path '$.Requestor', costcenter varchar2(4) path '$.CostCenter' ) ) /
  • 10.
    JSON_TABLE für relationaleSichten auf JSON Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 10 REFERENCE REQUESTOR COST --------------------------- ------------------ ---- FORD-20021009123336872PDT Gerry B. Ford R20 JONES-20011127121050471PST Richard J Jones R20 MARTIN-20011127121050401PST Thomas D. Martin S30 CLARK-20021009123337764PDT Edward K. Clark A10 JONES-20011127121042590PST Richard J Jones R20 MARTIN-20011127121040547PST Thomas D. Martin S30 JONES-20021009123336602PDT Richard J Jones R20 WARD-20011127121050511PST Stephen B. Ward S30 BLAKE-20011127121055689PST David E. Blake S30 MARTIN-20011127121048889PST Thomas D. Martin S30 : : :
  • 11.
    Copyright © 2014Oracle and/or its affiliates. All rights reserved. | JSON Indizierung • Function Based Indizes für … – Bekannte Abfragemuster – Attribute kommen im JSON nur einmal vor – keine Arrays – Basiert auf Function Based Indexes • Volltextindizierung von JSON-Dokumenten auf Basis von Oracle TEXT – Unbekannte Abfragemuster – inkl. Volltextrecherche – Beliebige JSON-Attribute – Basiert auf Oracle TEXT
  • 12.
    JSON Unterstützung inOracle12c: Interfaces und APIs  Neue 'Document-Store' API für Entwickler – REST Webservices – Document / Collection APIs für JAVA – Unterstützung für andere Sprachen / Frameworks in Arbeit • SQL Zugriff uneingeschränkt möglich – Treiber wie OCI, JDBC, .NET können sofort mit JSON umgehen – SQL/JSON Operatoren erlauben direktes Arbeiten mit SQL Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
  • 13.
    REST API fürJSON • REST Webservice API zur Arbeit mit JSON-Dokumenten: Kein SQL nötig • Zwei Deployment-Varianten: – Oracle Rest Data Services (aka 'APEX Listener') – Oracle XML DB Protokollserver (XML DB) • Unterstützt die REST Standardoperationen – PUT : Create / Update – GET : Retrieve – DELETE : Delete – POST : JSON Queries, Utility-Funktionen • Konfiguration des REST Service wird komplett in der Datenbank gespeichert Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
  • 14.
    Copyright © 2014Oracle and/or its affiliates. All rights reserved. | Roadmap • JSON mit SQL-Funktionen generieren • JSON Support für PL/SQL – Für APEX-Entwickler wird mit APEX 5.0 das Paket APEX_JSON kommen.
  • 15.
    Copyright © 2014Oracle and/or its affiliates. All rights reserved. | Weitere Informationen • Oracle Dokumentation – XML DB Developers Guide – Kapitel 39: JSON in Oracle Database • Blog des Autors: SQL und PL/SQL in Oracle – http://sql-plsql-de.blogspot.com – Juli 2014: Oracle 12.1.0.2 ist da: Ein Einblick in die JSON-Unterstützung 15
  • 16.
    Copyright © 2014Oracle and/or its affiliates. All rights reserved. | Carsten.Czarski@oracle.com http://tinyurl.com/apexcommunity http://sql-plsql-de.blogspot.com http://plsqlexecoscomm.sourceforge.net http://plsqlmailclient.sourceforge.net Twitter: @cczarski @oraclebudb
  • 17.
    JSON in derOracle-Datenbank Vorgehensweise und Funktionen im Detail Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 17
  • 18.
    • Für JSONwird kein eigener Datentyp verwendet • Alle Typen, die Text aufnehmen können, sind nutzbar VARCHAR2, CLOB, BLOB • JSON- und Nicht-JSON Inhalte können in einer Tabelle gemischt werden Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Schritt 1: Tabelle anlegen 18 create table po_json ( filename varchar2(200), json clob );
  • 19.
    Schritt 2: JSONDokumente laden Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 19
  • 20.
    Schritt 3: Wasist JSON, was ist nicht JSON • SQL Operatoren IS JSON und IS NOT JSON • Check Constraint stellt sicher, dass nur noch JSON gespeichert wird Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 20 select count(*) from po_json where json is json; select count(*) from po_json where json is not json; alter table po_json add constraint ck_isjson check (json is json);
  • 21.
    • SQL FunktionenJSON_EXISTS, JSON_VALUE, JSON_QUERY und JSON_TABLE • Arbeitsweise immer gleich – JSON-Tabellenspalte oder Literal – JSON Pfadausdruck (JSON Path Expression) – Returning Klausel – Error Handling Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Schritt 4: SQL Abfragen ... 21
  • 22.
    JSON_QUERY extrahiert JSON"Fragmente" Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 22 select json_query( json, '$.PurchaseOrder.ShippingInstructions' PRETTY ) from po_json; JSON_QUERY -------------------------------------------------------------------------------- [ { "name" : ["Gerry B. Ford"], "address" : ["100 Oracle ParkwayrnRedwood ShoresrnCArn94065rnUSA"], "telephone" : ["650 506 7100"] } ] 1 Zeile wurde ausgewählt.
  • 23.
    Im Fehlerfall ...wird NULL zurückgegeben Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 23 select json_query( json, '$.PurchaseOrder.ShippingInstrs' PRETTY ) from po_json; JSON_QUERY -------------------------------------------------------------------------------- ? 1 Zeile wurde ausgewählt.
  • 24.
    Copyright © 2014Oracle and/or its affiliates. All rights reserved. | ... oder eine Fehlermeldung. 24 select json_query( json, '$.PurchaseOrder.ShippingInstrs' PRETTY ERROR ON ERROR ) from po_json; * FEHLER in Zeile 1: ORA-40462: Auswertung von JSON_QUERY hat keinen Wert ergeben
  • 25.
    JSON_VALUE extrahiert "skalareWerte" Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 25 select json_value( json, '$.PurchaseOrder.Reference[0]' RETURNING VARCHAR2 ) from po_json where rownum < 10; JSON_VALUE ------------------------------------------------------------------ FORD-20021009123336872PDT JONES-20011127121050471PST MARTIN-20011127121050401PST CLARK-20021009123337764PDT :
  • 26.
    JSON_EXISTS stellt fest,ob ein JSON-Pfad existiert Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 26 select filename from po_json where json_exists( json, '$.PurchaseOrder.Reference' ); FILENAME ----------------------------------------------------------------- FORD-20021009123336872PDT.js JONES-20011127121050471PST.js MARTIN-20011127121050401PST.js CLARK-20021009123337764PDT.js :
  • 27.
    JSON_TABLE für relationaleSichten auf JSON Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 27 select reference, requestor, costcenter from po_json, json_table( json, '$.PurchaseOrder' columns ( reference varchar2(30) path '$.Reference[0]', requestor varchar2(25) path '$.Requestor[0]', costcenter varchar2(4) path '$.CostCenter[0]' ) ) /
  • 28.
    JSON_TABLE für relationaleSichten auf JSON Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 28 REFERENCE REQUESTOR COST --------------------------- ------------------ ---- FORD-20021009123336872PDT Gerry B. Ford R20 JONES-20011127121050471PST Richard J Jones R20 MARTIN-20011127121050401PST Thomas D. Martin S30 CLARK-20021009123337764PDT Edward K. Clark A10 JONES-20011127121042590PST Richard J Jones R20 MARTIN-20011127121040547PST Thomas D. Martin S30 JONES-20021009123336602PDT Richard J Jones R20 WARD-20011127121050511PST Stephen B. Ward S30 BLAKE-20011127121055689PST David E. Blake S30 MARTIN-20011127121048889PST Thomas D. Martin S30 : : :
  • 29.
    JSON_TABLE für geschachtelteStrukturen Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 29 select reference, requestor, num, descr, quantity From po_json, json_table( json, '$.PurchaseOrder' columns ( reference varchar2(30) path '$.Reference[0]', requestor varchar2(25) path '$.Requestor[0]', CostCenter varchar2(4) path '$.CostCenter[0]', nested path '$.LineItems[*].LineItem[*]' columns ( num number path '$."u0024".ItemNumber', descr varchar2(40) path '$.Description[0]', quantity number path '$.Part[0]."u0024"."Quantity"' ) ) )
  • 30.
    JSON_TABLE für relationaleSichten auf JSON Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 30 REFERENCE REQUESTOR NUM DESCR QUANTITY --------------------------- ------------------ ---- ---------------------------------------- -------- FORD-20021009123336872PDT Gerry B. Ford 1 Ordet 4 FORD-20021009123336872PDT Gerry B. Ford 2 The Naked Kiss 3 FORD-20021009123336872PDT Gerry B. Ford 3 Charade 2 FORD-20021009123336872PDT Gerry B. Ford 4 Robocop 2 FORD-20021009123336872PDT Gerry B. Ford 5 Cries and Whispers 4 FORD-20021009123336872PDT Gerry B. Ford 6 Autumn Sonata 4 FORD-20021009123336872PDT Gerry B. Ford 7 Getrud 3 FORD-20021009123336872PDT Gerry B. Ford 8 And the Ship Sails on 1 FORD-20021009123336872PDT Gerry B. Ford 9 Flesh for Frankenstein 4 FORD-20021009123336872PDT Gerry B. Ford 10 The Hidden Fortress 2 FORD-20021009123336872PDT Gerry B. Ford 11 The Importance of Being Earnest 3 FORD-20021009123336872PDT Gerry B. Ford 12 The Vanishing 4 FORD-20021009123336872PDT Gerry B. Ford 13 Cries and Whispers 4 FORD-20021009123336872PDT Gerry B. Ford 14 Beauty and the Beast 2 FORD-20021009123336872PDT Gerry B. Ford 15 And God Created Woman 1 : : : : : FORD-20021009123336872PDT Gerry B. Ford 16 The Magic Flute 1 20 Zeilen ausgewählt.
  • 31.
    Schritt 5: Volltextindizierungmit Oracle TEXT • Erstellung eines Oracle TEXT Index für JSON Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 31 CREATE INDEX po_search_idx ON po_json (json) INDEXTYPE IS CTXSYS.CONTEXT PARAMETERS ( 'section group CTXSYS.JSON_SECTION_GROUP SYNC (ON COMMIT)' ) / Index wurde erstellt.
  • 32.
    Schritt 5: Volltextindizierungmit Oracle TEXT • Erstellung eines Oracle TEXT Index für JSON Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 32 select filename from po_json WHERE json_textcontains( json, '$.PurchaseOrder.LineItems.LineItem.Description', 'Magic' ); TURNER-20021009123338565PDT.js CLARK-2001112712105223PST.js :
  • 33.
    Copyright © 2014Oracle and/or its affiliates. All rights reserved. | Weitere Informationen • Oracle Dokumentation – XML DB Developers Guide – Kapitel 39: JSON in Oracle Database • Blog des Autors: SQL und PL/SQL in Oracle – http://sql-plsql-de.blogspot.com – Juli 2014: Oracle 12.1.0.2 ist da: Ein Einblick in die JSON-Unterstützung 33
  • 34.
    Copyright © 2014Oracle and/or its affiliates. All rights reserved. | Carsten.Czarski@oracle.com http://tinyurl.com/apexcommunity http://sql-plsql-de.blogspot.com http://plsqlexecoscomm.sourceforge.net http://plsqlmailclient.sourceforge.net Twitter: @cczarski @oraclebudb

Hinweis der Redaktion

  • #19 create table po_json (filename varchar2(200), json clob) /