Skip to content

Commit fb10b4b

Browse files
authored
Add files via upload
1 parent 6442b2d commit fb10b4b

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed

java/ojvm/JSON-Tables.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
REM
2+
REM DDL for creating a table for the JSON Collection (and documents)
3+
REM for use by testSODA.java and testSODA.js
4+
REM
5+
CREATE TABLE MyFirstJSONCollection (
6+
ID varchar2(255) not null,
7+
CREATED_ON timestamp,
8+
LAST_MODIFIED timestamp,
9+
VERSION varchar2)255) not null,
10+
JSON_DOCUMENT BLOB
11+
)
12+
;

java/ojvm/SODA.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Simple Oracle Document Access (SODA) in OJVM
2+
3+
[SODA for Java](https://github.com/oracle/soda-for-java) is Oracle's fluent Java API for accessing JSON collections and documents without any knowledge of SQL.
4+
See [Getting started with SODA for Java](https://github.com/oracle/soda-for-java/blob/master/doc/Getting-started-example.md) for more details on running testSODA.java with a client JVM (JDK or JRE).
5+
The goal of this write up is to furnish the steps for running testSODA.java in OJVM and manipulate the JSON collection directly in the database session without moving data around.
6+
7+
* ** Requirements to run SODA with Java in the database**:
8+
9+
(i) Download the [latest orajsoda.jar](https://github.com/oracle/soda-for-java/releases) currently orajsoda-1.0.4.jar
10+
11+
(ii) Upload orasoda.jar in your database schema
12+
loadjava -r -v -u hr/hr orajsoda-1.0.4.jar
13+
14+
(iii) Load the [latest javax.json-1.0.4.jar](https://mvnrepository.com/artifact/org.glassfish/javax.json/1.0.4)
15+
loadjava -r -v -u hr/hr javax.json-1.0.4.jar
16+
17+
* ** Prep testSODA.java for OJVM
18+
19+
(i) Get (copy/paste) testSODA.java from [Getting started with SODA for Java](https://github.com/oracle/soda-for-java/blob/master/doc/Getting-started-example.md)
20+
21+
(ii) Replace the URL in the connect string with the OJVM server-side connect URL
22+
Replace "jdbc:oracle:thin:@//hostName:port/serviceName";
23+
With "jdbc:default:connection"
24+
The furnished testSODA.java on this page already has the change
25+
26+
(iii) load the updated testSODA.java in your schema
27+
loadjava -r -v -user hr/hr testSODA.java
28+
29+
(iv) Create the table for persisting the JSON collection and documents using the furnished JSON-tables.sql
30+
31+
(v) Create a SQL wrapper for invoking the main method
32+
create or replace procedure testSODA as
33+
language java name 'testSODA.main(java.lang.String[])';
34+
35+
(vi) Invoke the wrapper of the main method and display the output
36+
set serveroutput on
37+
call dbms_java.set_output(2000);
38+
call testSODA();
39+
40+
The furnished testSODA.sql performs the steps (v) and (vi).

java/ojvm/testSODA.java

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import java.sql.Connection;
2+
import java.sql.DriverManager;
3+
import oracle.sql.*;
4+
import oracle.soda.rdbms.OracleRDBMSClient;
5+
6+
import oracle.soda.OracleDatabase;
7+
import oracle.soda.OracleCursor;
8+
import oracle.soda.OracleCollection;
9+
import oracle.soda.OracleDocument;
10+
import oracle.soda.OracleException;
11+
12+
import java.util.Properties;
13+
14+
import oracle.jdbc.OracleConnection;
15+
import oracle.jdbc.pool.OracleDataSource;
16+
17+
public class testSODA {
18+
public static void main(String[] args) {
19+
20+
// SODA works on top of a regular JDBC connection.
21+
22+
Connection conn = null;
23+
24+
try {
25+
/*
26+
* Where is your code running: in the database or outside?
27+
*/
28+
if (System.getProperty("oracle.jserver.version") != null)
29+
{
30+
/*
31+
* You are in the database, already connected, use the default connection
32+
*
33+
*/
34+
OracleDataSource ods = new OracleDataSource();
35+
ods.setURL("jdbc:default:connection");
36+
conn = ods.getConnection();
37+
}
38+
else {
39+
/*
40+
* You are not in the database, you need to connect thru the client driver
41+
*/
42+
// Set up the connection string: replace hostName, port, and serviceName
43+
// with the info for your Oracle RDBMS instance
44+
String url = "jdbc:oracle:thin:@//hostName:port/serviceName";
45+
46+
Properties props = new Properties();
47+
// Replace with your schemaName and password
48+
props.setProperty("user", "schemaName");
49+
props.setProperty("password", "password");
50+
51+
conn = (OracleConnection) DriverManager.getConnection(url, props);
52+
}
53+
// Get an OracleRDBMSClient - starting point of SODA for Java application
54+
OracleRDBMSClient cl = new OracleRDBMSClient();
55+
56+
// Get a database
57+
OracleDatabase db = cl.getDatabase(conn);
58+
59+
//Check whether the named collection already exists or not
60+
OracleCollection col = db.openCollection("MyFirstJSONCollection");
61+
if (col != null) col.admin().drop();
62+
63+
// Create a collection with the name "MyFirstJSONCollection".
64+
// Note: Collection names are case-sensitive.
65+
// A table with the name "MyFirstJSONCollection" will be
66+
// created in the RDBMS to store the collection
67+
col = db.admin().createCollection("MyFirstJSONCollection");
68+
69+
// Create a few JSON documents, representing
70+
// users and the number of friends they have
71+
OracleDocument doc1 =
72+
db.createDocumentFromString(
73+
"{ \"name\" : \"Alex\", \"friends\" : \"50\" }");
74+
75+
OracleDocument doc2 =
76+
db.createDocumentFromString(
77+
"{ \"name\" : \"Mia\", \"friends\" : \"300\" }");
78+
79+
OracleDocument doc3 =
80+
db.createDocumentFromString(
81+
"{ \"name\" : \"Gloria\", \"friends\" : \"399\" }");
82+
83+
// Insert the documents into a collection, one-by-one.
84+
// The result documents contain auto-generated
85+
// keys, among other documents components (version, etc).
86+
// Note: SODA provides the more efficient bulk insert as well
87+
OracleDocument resultDoc1 = col.insertAndGet(doc1);
88+
OracleDocument resultDoc2 = col.insertAndGet(doc2);
89+
OracleDocument resultDoc3 = col.insertAndGet(doc3);
90+
91+
// Retrieve the first document using its auto-generated
92+
// unique ID (aka key)
93+
System.out.println ("* Retrieving the first document by its key *\n");
94+
95+
OracleDocument fetchedDoc = col.find().key(resultDoc1.getKey()).getOne();
96+
97+
System.out.println (fetchedDoc.getContentAsString());
98+
99+
// Retrieve all documents representing users that have
100+
// 300 or more friends. Use the following query-by-example:
101+
// {friends : {$gte : 300}}.
102+
System.out.println ("\n* Retrieving documents representing users with" +
103+
" at least 300 friends *\n");
104+
105+
OracleDocument f = db.createDocumentFromString(
106+
"{ \"friends\" : { \"$gte\" : 300 }}");
107+
108+
OracleCursor c = null;
109+
110+
try {
111+
// Get a cursor over all documents in the collection
112+
// that match our query-by-example
113+
c = col.find().filter(f).getCursor();
114+
115+
while (c.hasNext()) {
116+
// Get the next document
117+
fetchedDoc = c.next();
118+
119+
System.out.println (fetchedDoc.getContentAsString());
120+
}
121+
}
122+
finally {
123+
// Important: you must close the cursor to release resources!
124+
if (c != null) {
125+
c.close();
126+
}
127+
}
128+
129+
// Drop the collection, deleting the table backing
130+
// it and collection metadata
131+
if (args.length > 0 && args[0].equals("drop")) {
132+
col.admin().drop();
133+
System.out.println ("\n* Collection dropped *");
134+
}
135+
}
136+
catch (Exception e) {
137+
e.printStackTrace();
138+
}
139+
finally {
140+
if (conn != null) {
141+
try {
142+
conn.close();
143+
}
144+
catch (Exception e) {
145+
}
146+
}
147+
}
148+
}
149+
}

java/ojvm/testSODA.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
REM
2+
REM Wrapper (a.k.a. Call Spec) for invoking testSODA.main()
3+
REM
4+
create or replace procedure testSODA as
5+
language java name 'testSODA.main(java.lang.String[])';
6+
/
7+
REM
8+
REM Enable the output of testSODA(); then invoke it.
9+
REM
10+
set serveroutput on
11+
call dbms_java.set_output(2000);
12+
call testSODA();
13+
exit;

0 commit comments

Comments
 (0)