A RESTful MicroService for JSON Processing in the Database · 1. Load your JavaScript code in your...
Transcript of A RESTful MicroService for JSON Processing in the Database · 1. Load your JavaScript code in your...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
A RESTful MicroService for JSON Processing in the Database
Kuassi Mensah Director, Product Management Database Server Technologies July 11, 2017
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Speaker Bio
Confidential – Oracle Internal/Restricted/Highly Restricted 4
Director of Product Management at Oracle
• Java frameworks for the Oracle database (OJVM, JDBC, UCP, App Cont, TG, etc)
• Hadoop, Spark and Flink integration with the Oracle database
• JavaScript with Oracle database (Nashorn with JDK, JRE and OJVM)
MS CS from the Programming Institute of University of Paris VI (Jussieu)
Frequent speaker @ JavaOne, Oracle Open World, Data Summit, Node Summit, Collaborate/IOUG, RMOUG, BIWA , UKOUG, DOAG, OUGN, BGOUG, OUGF, OTN LAD, OTN APAC.
Author: Oracle Database Programming using Java and Web Services
Social network: @kmensah, http://db360.blogspot.com/, https://www.linkedin.com/in/kmensah
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Program Agenda
The Evolution of Web Applications
Processing JSON in the Database
JavaScript MicroService on the Database tier
A RESTful JavaScript MicroService
1
2
3
5
4
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
The Evolution of Web Applications
Confidential – Oracle Internal/Restricted/Highly Restricted 6
• Service-based architecture Death of the monolithic application. The era of microservices
• Programming models and languages Functional programming, Reactive programming Style trumps languages
• Data models, formats and persistence RDBMS, XML, JSON, Text, Spatial, Graph, etc Polyglot persistence
• Cloud Services Storage, Infrastructure, Software, Platform, Containers, etc
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Micro Services
Confidential – Oracle Internal/Restricted/Highly Restricted 7
• Distributed Service-oriented architecture
• A Web Application may have of up to thousands of microservices
• Polyglot
• Isolation between services
• Loose coupling, communication over simple wire protocols
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Monolithic Shopping Cart Application
8
Source: http://blog.arungupta.me/monolithic-microservices-refactoring-javaee-applications/
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 9
MicroService-based Shopping Cart Application Source: http://blog.arungupta.me/monolithic-microservices-refactoring-javaee-applications/
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Key Benefits of Micro Services
Confidential – Oracle Internal/Restricted/Highly Restricted 10
• Organizational and cultural shift
• Each microservice is developed, deployed and tested independently
‒"you build, you run it“ (Amazon)
• DevOps, Continuous Integration and delivery
•Team agility, flexibility, scalability, fault tolerance
• Maintenance: detect failure fast, fail gracefully and recover fast
• But Increased complexity Network Congestion and latency
•
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Technology of MicroServices
Confidential – Oracle Internal/Restricted/Highly Restricted 11
• Containers: Docker, LXC, etc
• Service Registry & Discovery: Zookeeper, Eureka, OSGi, Kubernetes
• Eventing
• Distributed Consensus
• Elastic Infrastructure
• Messaging
• Polyglot: language, persistence, etc.
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Polyglot Persistence with Oracle Database
The Oracle database furnishes an integrated access to all data models
• Relational
• XML
• JSON
• Text
• Graph
• Spatial
• Multimedia
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Program Agenda
The Evolution of Web Applications
Processing JSON in the Database
JavaScript MicroService on the Database tier
A RESTful JavaScript MicroService
1
2
3
13
4
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Processing JSON in the Database
14
Mission statement
• Design a service for processing millions of JSON documents stored in a database
• The service may be implemented in any language
• The service may be deployed in any tier (mid-tier, database)
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | Confidential – Oracle Internal/Restricted/Highly Restricted 15
Implementation Choices
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Program Agenda
The Evolution of Web Applications
Processing JSON in the Database
JavaScript MicroService on the Database tier
A RESTful JavaScript MicroService
1
2
3
16
4
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JavaScript & Java: Top Two Popular Languages
http://redmonk.com/sogrady/2017/06/08/language-rankings-6-17/
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JavaScript: a Language, a Platform, an Ecosystem
• Ubiquitous
• Tons of libraries, frameworks, and tools Node.js, Angular.js, Backbone.js, React.js, …, new frameworks by the day
• Versatile JavaScript supports all programming models, styles and applications types
• JavaScript extended thru other Languages http://bit.ly/1VpgaOl
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JavaScript on the JVM: Nashorn
• Nashorn engine: replacement for Rhino https://blogs.oracle.com/nashorn/ https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn
• Preserves investment in Java thru interoperability
– Calling Javascript from Java
– Calling Java from JavaScript
• Built into Java 8
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Running JavaScript with Nashorn using javax.script
20
// 1) Create an instance of the script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// 2) Create an instance of a Nashorn engine
ScriptEngine engine = factory.getEngineByName("javascript");
// 3) initialize your Java resource
URL url =
Thread.currentThread().getContextClassLoader().getResource(abFilePath);
// 4) evaluate the JavaScript code
engine.eval(new InputStreamReader(url.openStream()));
// 5) invoke the evaluated JavaScript function using invokeFunction
Invocable invobj = (Invocable) engine;
Object selectResult =
invobj.invokeFunction("selectQuery", inputId);
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
NASHORN
JDBC
NASHORN
Mid-tier (Java 8) DB 12.2 (Java 8)
JavaScript/Nashorn with Oracle Database
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JavaScript In the Database
22
• Why? - Faster, no network latency - Avoid data shipping i.e., processing thousands/millions of JSON documents in RDBMS - Reuse JavaScript code and skills for database modules
• Many DBMS(es) furnish JavaScript - MongoDb - Cosmos DB (built on DocumentDB) - ForerunnerDB
• Oracle database 12cR2 OJVM supports Java 8 and Nashorn
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Java VM in Oracle RDBMS (a.k.a. OJVM) Same Isolation and Scalability as Microservice Containers
JDK8 VM (+ Nashorn) Class Objmems
Newspace
Oldspace
Sessionspace
Internspace
Database Process / Session (PGA)
Shared Memory (SGA)
Newspace
Oldspace
Sessionspace
Newspace
Oldspace
Sessionspace
User Java & JavaScript Classes
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
How to Run JavaScript In the Database
24
1. Load your JavaScript code in your Schema as Java Resource loadjava –v –u yourschema hello.js
2. Invoke JavaScript : 3 approaches 1) JavaScript Procedures from SQL or PL/SQL by using dbms_javascript.run SQL: call dbms_javascript.run('hello.js'); PL/SQL: dbms_javascript.run('hello.js'); 2) JavaScript Procedures from Java in the database import oracle.aurora.rdbms.DbmsJavaScript;
…
DbmsJavaScript.run("hello.js");
3) JavaScript Functions: using the standard JSR223 scripting APIs in javax.script
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
A JavaScript MicroService for JSON Processing
25
var selectQuery = function(id)
{
var Driver = Packages.oracle.jdbc.OracleDriver;
var oracleDriver = new Driver();
var url = "jdbc:default:connection:"; // server-side JDBC
var query ="SELECT first_name, last_name from employees";
// establish a JDBC connection
var connection = oracleDriver.defaultConnection();
// prepared statement
var preparedStatement = connection.prepareStatement(query);
// execute Query
var resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
// Placeholder for parsing & processing JSON
// Display results
print(resultSet.getString(1) + "== " +
resultSet.getString(2) + " " );
}
// cleanup objects
resultSet.close();preparedStatement.close();connection.close();
return output;
}
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Invoking JavaScript in the Database with javax.script
Confidential – Oracle Internal/Restricted/Highly Restricted 26
create or replace and compile java source named "InvokeScript" as
mport javax.script.*;
import java.net.*;
import java.io.*;
public class InvokeScript {
public static String eval(String inputId) throws Exception {
String output = new String();
try {
// create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// create a JavaScript engine
ScriptEngine engine = factory.getEngineByName("javascript");
//read the script as a java resource
engine.eval(new
InputStreamReader(InvokeScript.class.getResourceAsStream("/select.js")));
Invocable invocable = (Invocable) engine;
Object selectResult = invocable.invokeFunction("selectQuery", inputId);
output = selectResult.toString();
} catch(Exception e) {
output =e.getMessage();
}
return output;
}
}
/
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Invoking JavaScript Functions
27
-- Wrap InvokeScript.eval() as a SQL Funtion
CREATE OR REPLACE FUNCTION scriptEval(inputId in varchar2)
return varchar2 as language java name
‘InvokeScript.eval(java.lang.String) return
java.lang.String';
/
-- A SQL Procedure for calling scriptEval
CREATE OR REPLACE PROCEDURE sqldemo(id IN varchar2) IS
output varchar2(10000);
BEGIN
SELECT scriptEval(id) INTO output from dual;
dbms_output.put_line(output);
END;
/
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Invoke MicroService via SQL
28
sqlplus ORDSTEST/ORDSTEST
SQL> set serveroutput on
SQL> call dbms_java.set_output(80000);
Call completed.
SQL> call sqldemo('100');
JavaScript MicroService
{ "EmpId" : "100", "FirstName" : "Kuassi", "LastName" : "Mensah", "Job" : "Manager", "Email" : "[email protected]", "Address" : { "City" : "Redwood", "Country" : "US" } }
JSON
JSON
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Demo Invoking the JavaScript MicroService via SQL
Confidential – Oracle Internal/Restricted/Highly Restricted 29
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Program Agenda
The Evolution of Web Applications
Processing JSON in the Database
JavaScript MicroService on the Database tier
RESTful JavaScript MicroService
1
2
3
30
4
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
RESTful JavaScript MicroService
Oracle Confidential – Internal/Restricted/Highly Restricted 31
REST framework
JSON
JSON JavaScript MicroService
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Oracle REST Data Services (ORDS)
32
• Runs on APEX Gateway, WLS, Tomcat, GlassFish, Jersey “Install Oracle REST Data Services 3.0.0 EA2 in under 5 minutes http://blog.cdivilly.com/2015/03/11/install-ords-3.0.0
• Need a USERS tablespace with at least 40MB space
• Follow the steps in “Installation Example” - accept defaults - skip APEX Gateway (pick option “2”) - skip “specify passwords for Application Express RESTful Services database users” (pick option “2”) - start in standalone mode (pick option “1”)
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
ORDS Configuration
33
• Configure the REST service for invoking JavaScript Microservices the URL is made of workspace + p_base_path + p_pattern http://server:port/ords/<workspace>/load/routes/nashorn/jsonqry/<n> begin ords.create_service( p_module_name => 'load.routes' , p_base_path => '/load/routes/', p_pattern => 'nashorn/selectbyid/:id', p_source_type => 'plsql/block', p_source => 'begin selectbyid(:id); end;‘ ); commit; end; /
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
RESTful MiroService for JSON Processing
{ "EmpId" : "100", "FirstName" : "Kuassi", "LastName" : "Mensah", "Job" : "Manager", "Email" : "[email protected]", "Address" : { "City" : "Redwood", "Country" : "US" } }
JSON
http://localhost:8090/ords/ordstest/load/routes/nashorn/selectbyid/100
Oracle REST Data Services
Db call
URI Request or HTTP(s) post mapped to SQL request
JDBC Connection
Pool
Oracle Database
URI http(s)
JSON
JavaScript MicroService
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Demo Invoking the JavaScript MicroService via SQL
Confidential – Oracle Internal/Restricted/Highly Restricted 35
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Resources
Oracle Confidential – Internal/Restricted/Highly Restricted 36
• GitHub Oracle Db Examples Nashorn https://github.com/oracle/oracle-db-examples/tree/master/javascript/nashorn
• JavaScript in Oracle Database 12c Release 2 http://db360.blogspot.de/2016/11/javascript-in-oracle-database-12c.html
• REST enable Java or JavaScript in the Database http://db360.blogspot.de/2016/11/rest-enable-java-or-javascript-in.html
• Tim Hall’s Oracle REST Data Service (ORDS) articles https://oracle-base.com/articles/misc/articles-misc#ords
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Questions?
Confidential – Oracle Internal 37
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. | 38