Date post: | 02-Aug-2015 |
Category: |
Software |
Upload: | carsten-czarski |
View: | 448 times |
Download: | 0 times |
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Oracle Developer Node.js und die Oracle-Datenbank
Carsten Czarski Business Unit Database Oracle Deutschland B.V. & Co KG
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Inhalt
• Node.js: Was ist das
• Der Treiber für die Oracle-Datenbank: node-oracledb
• Node.js und die Oracle-Datenbank am Beispiel
– 1: Zugriff auf eine Tabelle und Ausgabe auf der Konsole
– 2: Ausgabe von Tabellendaten als JSON über HTTP
– 3: Mail-Listener: Automatisches Laden von Mails in Tabellen
– 4: WebSockets meets Oracle Database
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Node.js
Node.js is an open source, cross-platform runtime environment for server-side and networking applications. Node.js applications are written in JavaScript, and can be run
within the Node.js runtime.
Node.js provides an event-driven architecture and a non-blocking I/O API that optimizes an application's throughput and scalability. These technologies are commonly used for
real-time web applications.
Node.js uses the Google V8 JavaScript engine to execute code, and a large percentage of the basic modules are written in JavaScript. Node.js contains a built-in library to allow
applications to act as a Web server without software such as Apache HTTP Server or IIS.
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Javascript auf dem Server
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Node.js ...
• ... basiert auf der Google V8 Engine (JavaScript) – Open Source seit 2008
– Implementiert in C++
– Kompiliert Javascript mit einem Just In Time Compiler
• ... ist asynchron und eventgetrieben – I/O-Gebundene API Calls sind nicht-blockierend
– Entwickler übergibt eine Callback-Funktion
– Interne Verwendung einer Event Queue
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Klassischer (synchroner) Programmablauf
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Asynchroner Programmablauf mit node.js
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Node Packaged Modules (npm)
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Populäre node.js Pakete
• Express: HTTP Server
• MailListener: Liest Mails aus einem IMAP/POP Postfach aus
• Websockets: Server für HTML5 Websocket-Technologie
• Webshot: HTML-Seiten abrufen und als Bild speichern
• Node-Oracledb: Oracle-Datenbanktreiber für node.js
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
node-oracledb
• node.js Treiber für die Oracle-Datenbank
– "Offizieller" Treiber für die Oracle-Datenbank
– Derzeit verfügbar: Version 0.2 (Early Adopter)
– Produktionsversion wird 1.0 sein
• Features
– SQL and PL/SQL Execution, Binding using JavaScript objects or arrays, Query results as JavaScript objects or array, Conversion between JavaScript and Oracle types
– Transaction Management, Connection Pooling, Statement Caching, Client Result Caching, End-to-end tracing
– High Availability Features: Fast Application Notification (FAN), Runtime Load Balancing (RLB), Transparent Application Failover (TAF)
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Download von GitHub
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Voraussetzung: Python muss installiert sein
• Version zwischen 2.5 und 3.0 $ python2.7 Python 2.7.9 (default, Jan 29 2015, 17:33:10) [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Installation node.js und node-oracledb
ZIP-Archive auspacken
Node.js testen
Build node-oracledb
Umgebung vorbereiten
Erstes Programm
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Voraussetzungen – Linux-System
• Download und Auspacken der ZIP-Archive
• $ORACLE_HOME Oracle Instant Client (Basic + SDK)
• $NODE_HOME Node.js
• $NODE_ORACLEDB_HOME Node-oracledb
$ ls instantclient_12_1 node-v0.10.36-linux-x64 node-oracledb-master $
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Installation node.js und node-oracledb
ZIP-Archive auspacken
Node.js testen
Build node-oracledb
Umgebung vorbereiten
Erstes Programm
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Node.js installieren und einrichten
• Starten von node.js mit "$NODE_HOME/bin/node"
• Starten des Paketmanagers npm mit "$NODE_HOME/bin/node"
$ cd $NODE_HOME $ ls bin ChangeLog include lib LICENSE node_modules README.md share $ ls ./bin node npm $ $NODE_HOME/bin/node -v v0.10.36 $ $NODE_HOME/bin/npm -v 1.4.28
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Proxy-Server und Umgebungsvariablen
• Falls Node.js Umgebung hinter der Firewall
• Package Manager braucht Internetverbindung $ export PATH=$NODE_HOME/bin:$PATH $ npm config set proxy=http://{proxy-server}:{port} $ npm config set https-proxy=http://{proxy-server}:{port}
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Installation node.js und node-oracledb
ZIP-Archive auspacken
Node.js testen
Build node-oracledb
Umgebung vorbereiten
Erstes Programm
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Oracle Instant Client installieren
• Verzeichnisstruktur nach Auspacken des ZIP-Archivs $ cd $ORACLE_HOME $ find . ./uidrvci ./libocci.so.12.1 ./libociei.so : ./xstreams.jar ./sdk ./sdk/include ./sdk/include/occiCommon.h ./sdk/include/occi.h ./sdk/include/occiData.h :
Instantclient "BASIC"
Instantclient "SDK"
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Umgebung für Build von "node-oracledb" vorbereiten
• Symlink erzeugen
• Umgebungsvariablen setzen
$ ls $ORACLE_HOME/libcntsh.* libclntshcore.so.12.1 libclntsh.so.12.1 $ ln –s libclntsh.so.12.1 libclntsh.so $ ls libclntshcore.so.12.1 libclntsh.so libclntsh.so.12.1
$ export OCI_LIB_DIR = $ORACLE_HOME # Einmalig für Build $ export OCI_INC_DIR = $ORACLE_HOME/sdk/include # Einmalig für Build $ export LD_LIBRARY_PATH = $ORACLE_HOME:$LD_LIBRARY_PATH # Immer nötig
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Build von "node-oracledb"
$ cd $NODE_ORACLEDB_HOME $ npm install > [email protected] install /home/oracle/node-oracledb-master > node-gyp rebuild make: Entering directory `/home/oracle/node-oracledb-master/build' CXX(target) Release/obj.target/oracledb/src/njs/src/njsOracle.o CXX(target) Release/obj.target/oracledb/src/njs/src/njsPool.o CXX(target) Release/obj.target/oracledb/src/njs/src/njsConnection.o CXX(target) Release/obj.target/oracledb/src/njs/src/njsMessages.o CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiEnv.o CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiEnvImpl.o CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiException.o CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiExceptionImpl.o CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiConnImpl.o CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiDateTimeArrayImpl.o CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiPoolImpl.o CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiStmtImpl.o CXX(target) Release/obj.target/oracledb/src/dpi/src/dpiUtils.o SOLINK_MODULE(target) Release/obj.target/oracledb.node SOLINK_MODULE(target) Release/obj.target/oracledb.node: Finished COPY Release/oracledb.node make: Leaving directory `/home/oracle/node-oracledb-master/build'
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Installation node.js und node-oracledb
ZIP-Archive auspacken
Node.js testen
Build node-oracledb
Umgebung vorbereiten
Erstes Programm
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Arbeitsumgebung erzeugen
• $ mkdir /home/oracle/nodework
• $ cd /home/oracle/nodework
• $ mkdir node_modules
• $ cd node_modules
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
"node-oracledb" nach "node_modules" verschieben
• $ mkdir /home/oracle/nodework
• $ cd /home/oracle/nodework
• $ mkdir node_modules
• $ cd node_modules
• $ mv $NODE_ORACLEDB_HOME .
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
"node-oracledb" nach "node_modules" verschieben
• $ mkdir /home/oracle/nodework
• $ cd /home/oracle/nodework
• $ mkdir node_modules
• $ cd node_modules
• $ mv $NODE_ORACLEDB_HOME .
• $ mv node-oracledb-master oracledb
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Installation node.js und node-oracledb
ZIP-Archive auspacken
Node.js testen
Build node-oracledb
Umgebung vorbereiten
Erstes Programm
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Das erste "node-oracledb" Programm: emp.js
var oracledb = require('oracledb'); oracledb.getConnection( { user : "scott", password : "tiger", connectString : "sccloud033:1521/orcl" }, function(err, connection) { if (err) {console.error(err.message); return;} connection.execute( "SELECT * from EMP where EMPNO=7839", [], function(err, result) { if (err) {console.log('%s', err.message); return;} console.log(result.rows); } ); }); console.log("Finished – really?");
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Verzeichnisstruktur
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Test: Das erste Node.js Programm
$ node emp.js Finished. Really???? [ [ 7839, 'KING', 'PRESIDENT', null, Tue Nov 17 1981 00:00:00 GMT+0100 (CET), 5000, null, 10 ] ] $
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Zweites Programm: "JSON-HTTP-Server" var oracledb = require('oracledb'); var http = require('http'); oracledb.createPool( { user : "scott", password : "tiger", connectString : "sccloud033:1521/orcl", poolMin : 1, poolMax : 20 }, function(err, pool){ if (err) { ... } http.createServer(function (req, res) { pool.getConnection(function(err, connection){ if (err) { ... } connection.execute("select * from emp ", [], function(err, results) { if (err) { ... } connection.release(function (err) { if (err) { ... } }); res.writeHead(200, {'Content-Type': 'application/json'});! res.end(JSON.stringify(results.rows)); }) }) }).listen(1234, 'sccloud033.de.oracle.com') });
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Zweites Programm: "JSON-HTTP-Server" var oracledb = require('oracledb'); var http = require('http'); oracledb.createPool( { user : "scott", password : "tiger", connectString : "sccloud033:1521/orcl", poolMin : 1, poolMax : 20 }, function(err, pool){ if (err) { ... } http.createServer(function (req, res) { pool.getConnection(function(err, connection){ if (err) { ... } connection.execute("select * from emp ", [], function(err, results) { if (err) { ... } connection.release(function (err) { if (err) { ... } }); res.writeHead(200, {'Content-Type': 'application/json'});! res.end(JSON.stringify(results.rows)); }) }) }).listen(1234, 'sccloud033.de.oracle.com') });
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Verzeichnisstruktur nach "npm install express"
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Express: Einfacher Webserver
var express = require('express') function startServer () { var app = express(); app.get ("/emp/*", processEmp); app.get ("/dept/*", processDept); var server = app.listen(9000, function () { var host = server.address().address var port = server.address().port console.log('Table EMP REST Service listening at http://%s:%s', host, port); }); }
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Express: Einfacher Webserver
function processEmp(req, res) { pool.getConnection(function(err, connection){ connection.execute( "select * from emp where (empno=:1 or :1 is null)", [req.params[0]], function(err, results) { connection.release(function (err) {}), res.writeHead(200, {'Content-Type': 'application/json'}); res.end(JSON.stringify(results.rows)); } ) }) }
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Ein wichtiger Tipp zwischendurch ...
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
IMAP Integration: Mail Listener
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Verzeichnisstruktur nach "npm install mail-listener2"
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
MailListener2: IMAP-Server mit Tabelle verbinden
var mailListener = new MailListener({ username: "[email protected]", password: "***********", host: "mailserver.mycompany.com", port: 993, tls: true, tlsOpTions: { rejectUnauthorized: false }, mailbox: "INBOX", searchFilter: ["UNSEEN"], markSeen: true, fetchUnreadOnStart: false, mailParserOptions: {streamAttachments: true}, attachments: false });
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
MailListener2: IMAP-Server mit Tabelle verbinden
mailListener.on("mail", function(mail, seqno, attributes) { if (mail.subject.match(/^MAIL_NODETEST.*/)) { pool.getConnection(function(err, connection){ connection.execute( "insert into mails_nodejs (sender, subject, datetime) values (:1, :2, :3)", [mail.from[0].address, mail.subject, mail.date], function(err, results) { if (err) { console.log(err); } connection.commit(function (err) {…}); connection.release(function (err) {…}); console.log("storing message: \"" + mail.subject + "\""); } ) }) } else { console.log("Ignoring Message: \""+mail.subject+"\""); } });
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
HTML5-Websocket: Asynchrone Client-Benachrichtigung
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
HTML5 Websockets
• Bidirektionale Verbindung zwischen Browser und Webserver
– Browser sendet HTTP-Anfrage mit Upgrade Request für Websocket
– Verbindung bleibt nach Server-Antwort offen
– Server kann weitere Daten ohne erneute Client-Anfrage senden
• Nutzen
– Gut geeignet für jede Form der asynchronen Benachrichtigung
– Ständiges Polling des Clients zum Server fällt weg
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
HTML5 Websockets im Internet
Internet erfordert viele offene Websockets
Leichtgewichtige Server-Technologie
nötig Node.js
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
HTML5 Websockets mit Node.js DEMO
Oracle Confidential – Internal/Restricted/Highl
44
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Weitere Informationen
• Node.js Info und Download http://nodejs.org
• Node Package Manager http://npmjs.org
• Oracle-Treiber node-oracledb auf Github https://github.com/oracle/node-oracledb
• Node.js Developer Center im OTN http://www.oracle.com/technetwork/database/database-technologies/node_js/oracle-node-js-2399407.html
• Diskussionsforum node.js und Oracle https://community.oracle.com/community/database/developer-tools/node_js/content
Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
[email protected] http://tinyurl.com/apexcommunity http://sql-plsql-de.blogspot.com http://plsqlexecoscomm.sourceforge.net http://plsqlmailclient.sourceforge.net Twitter: @cczarski @oraclebudb
Nächster Oracle Developer Monthly
20. Februar 2015 – 09:00 Uhr