From 2a34daf58129f0ad3b4df8d17120524a74212451 Mon Sep 17 00:00:00 2001
From: Anthony Tuininga If you are running this tutorial in your own environment, install the following required software: If you are running this tutorial in your own environment, install the required software: Python. Version 3.6 is preferred. cx_Oracle version 7.2 and the Oracle Client libraries. SQL*Plus such as from the Oracle Instant Client SQL*Plus Package. The Advanced Queuing section requires Oracle client 12.2 or later. The SODA section requires Oracle client 18.5, or later, and Oracle Database 18 or later. To create the schema run:Contents
+
+ Contents
Preface
-
-
+
-sqlplus sys/yoursyspassword@localhost/orclpdb as sysdba @sql/SetupSamples
+sqlplus sys/yoursyspassword@localhost/orclpdb1 as sysdba @sql/SetupSamples
Connection Information
@@ -130,11 +139,11 @@ Connection Information
The username is "pythonhol" with
- the password "welcome". The connect string is "localhost/orclpdb".
+ the password "welcome". The connect string is "localhost/orclpdb1".
See sql/SampleEnv.sql
.
It is easist to have a local pluggable database with the service - 'orclpdb' configured. If your database is not local, or has a + 'orclpdb1' configured. If your database is not local, or has a different service, you will need to modify the connection information in db_config.py and db_config.sql.
The following sections may need adjusting, depending on how you @@ -174,16 +183,16 @@
user = "pythonhol" pw = "welcome" -dsn = "localhost/orclpdb" +dsn = "localhost/orclpdb1"
db_config.sql
def user = "pythonhol" def pw = "welcome" -def connect_string = "localhost/orclpdb" +def connect_string = "localhost/orclpdb1"-
By default they connect to the 'orclpdb' database service on the same machine as Python. You can modify the values in both files to match the connection information for your environment.
+By default they connect to the 'orclpdb1' database service on the same machine as Python. You can modify the values in both files to match the connection information for your environment.
@@ -207,7 +216,7 @@localhost
, and the database service name
- orclpdb
.
+ orclpdb1
.
Open a command terminal and change to the tutorial
directory:
In a terminal window, start SQL*Plus using the lab credentials and connection string, such as:
-sqlplus pythonhol/welcome@localhost/orclpdb +sqlplus pythonhol/welcome@localhost/orclpdb1
Use the SQL*Plus DESCRIBE command to look at the SDO definition:
@@ -1661,6 +1670,9 @@Oracle Database "LOB" long objects can be streamed using a LOB + locator, or worked with directly as strings or bytes.
+This inserts some test string data and then fetches one
- record into clob
, which is a cx_Oracle LOB Object.
- Methods on LOB include size()
and
+ record into clob
, which is a cx_Oracle character
+ LOB Object. Methods on LOB include size()
and
read()
.
To see the output, run the file:
@@ -1947,7 +1959,7 @@Review aq.py
:
Simple Oracle Document Access is a set of NoSQL-style APIs. + Documents can be inserted, queried, and retrieved from Oracle + Database. By default, documents are JSON strings. SODA APIs + exist in many languages.
+ +Review soda.py
:
+import cx_Oracle +import db_config + +con = cx_Oracle.connect(db_config.user, db_config.pw, db_config.dsn) + +soda = con.getSodaDatabase() + +collection = soda.createCollection("friends") + +content = {'name': 'Jared', 'age': 35, 'address': {'city': 'Melbourne'}} + +doc = collection.insertOneAndGet(content) +key = doc.key + +doc = collection.find().key(key).getOne() +content = doc.getContent() +print('Retrieved SODA document dictionary is:') +print(content) ++ +
soda.createCollection()
will create a new
+ collection, or open an existing collection, if the name is
+ already in use.
insertOneAndGet()
inserts the content of a
+ document into the database and returns a SODA Document Object.
+ This allows access to meta data such as the document key. By
+ default, document keys are automatically generated.
The find()
method is used to begin an operation
+ that will act upon documents in the collection.
content
is a dictionary. You can also get a JSON string
+ by calling doc.getContentAsString()
.
Run the file:
+ +python soda.py+ +
The output shows the content of the new document.
+ +Extend soda.py
to insert some more documents and
+ perform a find filter operation:
+myDocs = [ + {'name': 'Gerald', 'age': 21, 'address': {'city': 'London'}}, + {'name': 'David', 'age': 28, 'address': {'city': 'Melbourne'}}, + {'name': 'Shawn', 'age': 20, 'address': {'city': 'San Francisco'}} +] +collection.insertMany(myDocs) + +filterSpec = { "address.city": "Melbourne" } +myDocuments = collection.find().filter(filterSpec).getDocuments() + +print('Melbourne people:') +for doc in myDocuments: + print(doc.getContent()["name"]) ++ +
Run the script again:
+ +python soda.py+ +
The find operation filters the collection and returns
+ documents where the city is Melbourne. Note the
+ insertMany()
method is currently in preview.
SODA supports query by example (QBE) with an extensive set of
+ operators. Extend soda.py
with a QBE to find
+ documents where the age is less than 25:
+filterSpec = {'age': {'$lt': 25}} +myDocuments = collection.find().filter(filterSpec).getDocuments() + +print('Young people:') +for doc in myDocuments: + print(doc.getContent()["name"]) ++ +
Running the script displays the names.
+ +