Mais conteúdo relacionado Semelhante a Node.js und die Oracle-Datenbank (20) Node.js und die Oracle-Datenbank1. 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
2. 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
3. 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.
4. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Javascript auf dem Server
5. 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
6. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Klassischer (synchroner) Programmablauf
7. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Asynchroner Programmablauf mit node.js
8. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Node Packaged Modules (npm)
9. 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
10. 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)
11. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Download von GitHub
12. 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.
>>>
13. 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
14. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Voraussetzungen – Linux-System
• Download und Auspacken der ZIP-Archive
• $ORACLE_HOMEOracle Instant Client (Basic + SDK)
• $NODE_HOMENode.js
• $NODE_ORACLEDB_HOMENode-oracledb
$ ls
instantclient_12_1 node-v0.10.36-linux-x64 node-oracledb-master
$
15. 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
16. 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
17. 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}
18. 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
19. 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"
20. 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
21. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Build von "node-oracledb"
$ cd $NODE_ORACLEDB_HOME
$ npm install
> oracledb@0.2.4 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'
22. 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
23. 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
24. 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 .
25. 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
26. 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
27. 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?");
28. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Verzeichnisstruktur
29. 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 ] ]
$
30. 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')
});
31. 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')
});
33. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Verzeichnisstruktur nach "npm install express"
34. 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);
});
}
35. 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));
}
)
})
}
36. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Ein wichtiger Tipp zwischendurch ...
37. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
IMAP Integration: Mail Listener
38. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
Verzeichnisstruktur nach "npm install mail-listener2"
39. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
MailListener2: IMAP-Server mit Tabelle verbinden
var mailListener = new MailListener({
username: "vorname.nachname@mycompany.com",
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
});
40. 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+""");
}
});
41. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
HTML5-Websocket: Asynchrone Client-Benachrichtigung
42. 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
43. 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
44. Copyright © 2014 Oracle and/or its affiliates. All rights reserved.
HTML5 Websockets mit Node.js
DEMO
Oracle Confidential – 4
47. 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
48. 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
Nächster Oracle Developer Monthly
20. Februar 2015 – 09:00 Uhr