Upload
carsten-czarski
View
289
Download
0
Embed Size (px)
DESCRIPTION
Überblick über die JSON-Unterstützung in der Oracle-Datenbank 12.1.0.2. Enthält Informationen zu JSON-Speicherung, zum Zugriff mit SQL und zur Indizierung.
Citation preview
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
JSON und Oracle12cDie Oracle-Datenbank als JSON Document Store
Carsten CzarskiBusiness Unit Database
Oracle Deutschland B.V. & Co KG
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Oracle12c für EntwicklerDie Datenbank für alle wichtigen Plattformen
JSONRuby
Oracle ADF Oracle APEXOracle RDS
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
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
• 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
Security ...?IT Betrieb ...?
Replikation ...?Funktionsbibliothek ...?
Relationale Applikationen ...?
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Relationale Tabellen bleiben wichtig. Auf das Zusammenspiel kommt es an.
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
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
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
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 derrelationalen Welt!
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
SQL Queries auf JSON-Dokumente: JSON Table
8
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
SQL Beispiel: JSON_TABLE
9
select reference, requestor, costcenterfrom po_json, json_table(json,'$.PurchaseOrder'columns (reference varchar2(30) path '$.Reference',requestor varchar2(25) path '$.Requestor',costcenter varchar2(4) path '$.CostCenter'
))/
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
JSON_TABLE für relationale Sichten auf JSON
10
REFERENCE REQUESTOR COST--------------------------- ------------------ ----FORD-20021009123336872PDT Gerry B. Ford R20JONES-20011127121050471PST Richard J Jones R20MARTIN-20011127121050401PST Thomas D. Martin S30CLARK-20021009123337764PDT Edward K. Clark A10JONES-20011127121042590PST Richard J Jones R20MARTIN-20011127121040547PST Thomas D. Martin S30JONES-20021009123336602PDT Richard J Jones R20WARD-20011127121050511PST Stephen B. Ward S30BLAKE-20011127121055689PST David E. Blake S30MARTIN-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
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
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. |
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. |
http://tinyurl.com/apexcommunityhttp://sql-plsql-de.blogspot.comhttp://plsqlexecoscomm.sourceforge.nethttp://plsqlmailclient.sourceforge.net
Twitter: @cczarski @oraclebudb
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
JSON in der Oracle-DatenbankVorgehensweise und Funktionen im Detail
17
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
• Für JSON wird kein eigener Datentyp verwendet
• Alle Typen, die Text aufnehmen können, sind nutzbarVARCHAR2, CLOB, BLOB
• JSON- und Nicht-JSON Inhalte können in einer Tabelle gemischt werden
Schritt 1: Tabelle anlegen
18
create table po_json (filename varchar2(200), json clob
);
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Schritt 2: JSON Dokumente laden
19
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
• SQL Operatoren IS JSON und IS NOT JSON
• Check Constraint stellt sicher, dass nur noch JSON gespeichert wird
Schritt 3: Was ist JSON, was ist nicht JSON
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);
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
• 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
Schritt 4: SQL Abfragen ...
21
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
JSON_QUERY extrahiert JSON "Fragmente"
22
select json_query(json, '$.PurchaseOrder.ShippingInstructions' PRETTY
) from po_json;
JSON_QUERY--------------------------------------------------------------------------------
[{
"name" : ["Gerry B. Ford"],"address" : ["100 Oracle Parkway\r\nRedwood Shores\r\nCA\r\n94065\r\nUSA"],"telephone" : ["650 506 7100"]
}]
1 Zeile wurde ausgewählt.
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Im Fehlerfall ... wird NULL zurückgegeben
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
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
JSON_VALUE extrahiert "skalare Werte"
25
select json_value(json, '$.PurchaseOrder.Reference[0]' RETURNING VARCHAR2
) from po_json where rownum < 10;
JSON_VALUE------------------------------------------------------------------FORD-20021009123336872PDTJONES-20011127121050471PSTMARTIN-20011127121050401PSTCLARK-20021009123337764PDT:
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
JSON_EXISTS stellt fest, ob ein JSON-Pfad existiert
26
select filename from po_json where json_exists(json, '$.PurchaseOrder.Reference'
);
FILENAME-----------------------------------------------------------------FORD-20021009123336872PDT.jsJONES-20011127121050471PST.jsMARTIN-20011127121050401PST.jsCLARK-20021009123337764PDT.js:
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
JSON_TABLE für relationale Sichten auf JSON
27
select reference, requestor, costcenterfrom 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]'
))/
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
JSON_TABLE für relationale Sichten auf JSON
28
REFERENCE REQUESTOR COST--------------------------- ------------------ ----FORD-20021009123336872PDT Gerry B. Ford R20JONES-20011127121050471PST Richard J Jones R20MARTIN-20011127121050401PST Thomas D. Martin S30CLARK-20021009123337764PDT Edward K. Clark A10JONES-20011127121042590PST Richard J Jones R20MARTIN-20011127121040547PST Thomas D. Martin S30JONES-20021009123336602PDT Richard J Jones R20WARD-20011127121050511PST Stephen B. Ward S30BLAKE-20011127121055689PST David E. Blake S30MARTIN-20011127121048889PST Thomas D. Martin S30: : :
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
JSON_TABLE für geschachtelte Strukturen
29
select reference, requestor, num, descr, quantityFrom 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"'
))
)
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
JSON_TABLE für relationale Sichten auf JSON
30
REFERENCE REQUESTOR NUM DESCR QUANTITY--------------------------- ------------------ ---- ---------------------------------------- --------FORD-20021009123336872PDT Gerry B. Ford 1 Ordet 4FORD-20021009123336872PDT Gerry B. Ford 2 The Naked Kiss 3FORD-20021009123336872PDT Gerry B. Ford 3 Charade 2FORD-20021009123336872PDT Gerry B. Ford 4 Robocop 2FORD-20021009123336872PDT Gerry B. Ford 5 Cries and Whispers 4FORD-20021009123336872PDT Gerry B. Ford 6 Autumn Sonata 4FORD-20021009123336872PDT Gerry B. Ford 7 Getrud 3FORD-20021009123336872PDT Gerry B. Ford 8 And the Ship Sails on 1FORD-20021009123336872PDT Gerry B. Ford 9 Flesh for Frankenstein 4FORD-20021009123336872PDT Gerry B. Ford 10 The Hidden Fortress 2FORD-20021009123336872PDT Gerry B. Ford 11 The Importance of Being Earnest 3FORD-20021009123336872PDT Gerry B. Ford 12 The Vanishing 4FORD-20021009123336872PDT Gerry B. Ford 13 Cries and Whispers 4FORD-20021009123336872PDT Gerry B. Ford 14 Beauty and the Beast 2FORD-20021009123336872PDT Gerry B. Ford 15 And God Created Woman 1: : : : :FORD-20021009123336872PDT Gerry B. Ford 16 The Magic Flute 1
20 Zeilen ausgewählt.
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
• Erstellung eines Oracle TEXT Index für JSON
Schritt 5: Volltextindizierung mit Oracle TEXT
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.
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
• Erstellung eines Oracle TEXT Index für JSON
Schritt 5: Volltextindizierung mit Oracle TEXT
32
select filename from po_json WHERE json_textcontains(json, '$.PurchaseOrder.LineItems.LineItem.Description', 'Magic'
);
TURNER-20021009123338565PDT.jsCLARK-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. |
http://tinyurl.com/apexcommunityhttp://sql-plsql-de.blogspot.comhttp://plsqlexecoscomm.sourceforge.nethttp://plsqlmailclient.sourceforge.net
Twitter: @cczarski @oraclebudb