Intro To Oracle Nosql DB Hol 1937059
Intro To Oracle Nosql DB Hol 1937059
ii
Oracle NoSQL Database
3.
4.
Open JDeveloper Studio by clicking the cup icon from the toolbar.
5. If the bigdatademo project is not open inside JDeveloper Studio, open bigdatademo.jws from
/home/oracle/movie/moviedemo/nosqldb/bigdatademo directory ($KVDEMOHOME).
6. In the Application Navigator window, expand the UI project and locate the index.jsp file under Web
Content directory.
7. Right mouse click index.jsp and select Run from the context menu. JDeveloper builds and deploys
the application.
8. When you see the web page below, enter the username and password (guest1/welcome1) and click
Sign in.
9. What is the result?
2
Oracle NoSQL Database
The error message above appears because we have not started an instance of Oracle NoSQL Database. In
addition, we have not loaded the profile data that contains the guest accounts. In lab exercise 2, we will start an
instance of Oracle NoSQL Database and load the profile data.
3
Oracle NoSQL Database
Lab Exercise 2 Start Oracle NoSQL Database instance and load the user profile data
In this exercise, you will start an Oracle NoSQL Database instance and then load the user profile information.
You will step through some of the code while loading the profile information. At the end of this lab, you should
be able to log on successfully. KVLite will be used as the Oracle NoSQL Database Instance. A very brief
introduction to KVLite follows:
User profile data that is going to be loaded into Oracle NoSQL Database as JSON objects and the file that contains
the profile information can be found here: $KVDEMOHOME/dataparser/metadata/customer.out
{"id":1,"name":"Frankie","email":"[email protected]","username":"guest1
","password":"n\u000bN\"bR\\-I\u000f"}
{"id":2,"name":"Kelli","email":"[email protected]","username":"guest2",
"password":"n\u000bN\"bR\\-I\u000f"}
...
In Oracle NoSQL Database, customer profile information is going to be represented as following key-value
structure:
Major-Key1
Minor-Key1
Value
CUST
Major-Key2
(username)
guest1
info
{"id":1,"name":"Frankie","email":"Frankie.Nas
[email protected]","username":"guest1","passwo
rd":"n\u000bN\"bR\\-I\u000f"}
CUST
guest2
info
{"id":2,"name":"Kelli","email":"Kelli.Miller@
oraclemail.com","username":"guest2","password
":"n\u000bN\"bR\\-I\u000f"}
4
Oracle NoSQL Database
One can store these JSON objects as plain byte arrays (same as in R1) but for efficient storage utilization and ease
of use, we are going to use Avro JSON binding to serialize and deserialize user profile information to/from the
NoSQL database. There are couples of benefits of using Avro SerDe APIs provided in R2 such as:
Avro uses schema definition to parse the JSON objects and in the process removes the fields names
from the object. Making it very efficient to store and retrieve from the storage layer.
Avro support schema evolution, which means you can add columns as the schema evolves.
Different bindings available in the product make serialization/deserialization easy for the end user.
We are using JSON binding in our example code.
Most importantly in future releases secondary index etc would leverage the schema definition.
Lets now create an Avro schema (represented as JSON only) and we will use ddl add-schema admin command to
register it to the store. All the schemas definition files are available under
$KVDEMOHOME/dataparser/schemas directory and the one that we are going to use for user profile is
customer.avsc. The content of the file looks like this:
{
"type" : "record",
"name" : "Customer",
"namespace" : "oracle.avro",
"fields" : [ {
"name" : "id",
"type" : "int",
"default" : 0
}, {
"name" : "name",
"type" : "string",
"default" : ""
}, {
"name" : "email",
"type" : "string",
"default" : ""
}, {
"name" : "username",
"type" : "string",
"default" : ""
}, {
"name" : "password",
"type" : "string",
"default" : ""
} ]
}
5
Oracle NoSQL Database
Instructions
Bring up a command prompt (terminal window) and go to your KVHOME directory
1.
2.
Look for a response similar to what is listed below. Minimize window (leave running).
java -jar $KVHOME/lib/kvstore-2.*.jar kvlite -root
$KVROOT
Open a new tab in the terminal window and change directory to schemas directory.
cd $KVDEMOHOME/dataparser/schemas
4.
Under the schemas directory you would find six AVRO schema files (*.avsc). You can cat them one by one to
see how the schema is defined.
ls -altr
5.
6.
7.
This is what you should get after you successfully registering the schema:
6
Oracle NoSQL Database
8.
Run show schemas command to make sure all six schemas are registered.
Kv-> show schemas
oracle.avro.Customer
ID: 1 Modified: 2013-01-29
oracle.avro.Movie
ID: 2 Modified: 2013-01-29
oracle.avro.Cast
ID: 3 Modified: 2013-01-29
oracle.avro.Crew
ID: 4 Modified: 2013-01-29
oracle.avro.Genre
ID: 5 Modified: 2013-01-29
oracle.avro.Activity
ID: 6 Modified: 2013-01-29
10. In JDeveloper Studio, expand dataparser project and expand all the packages by clicking Application
Sources. Look for the package oracle.demo.oow.bd.dao, this is where Data Access Classes are defined,
which means classes that interact with the Oracle NoSQL Database. Open
oracle.demo.oow.bd.dao.CustomerDAO. This class has all the access methods for Customer related
operation like creating a customer profile, reading a profile using customerId, authenticating a customer
based on username and password. Lets closely examine how Customer data is written and accessed from
this class. Use CTRL + G to go to any line number:
Line 55: The constructor where we create JsonAvroBinding and parse customer schema. This
binding we later use while serializing and deserializing the customer JSON object.
Line 467: Method toValue(CustomerTO) takes CutomerTO POJO object and serializes into Value
object
Line 489: Method getCustomerTO(Value) de-serializes Value object back into CustomerTO
POJO.
7
Oracle NoSQL Database
12. Lets upload the customer profile data into NoSQL Database now. Open java class
oracle.demo.oow.bd.loader.CustomerProfileLoaderFile
13. To run, right click the class from the left pane and select Run, which would upload all the profile
information (100 of them) into the NoSQL database.
8
Oracle NoSQL Database
14. To confirm, login from the Movieplex web page again. You should see a welcome page as shown in the
image below.
15. Notice that there are no movies available yet. Lab exercise 3 will load the movies.
9
Oracle NoSQL Database
{"id":857,"original_title":"Saving Private
Ryan","release_date":"1998","overview":"On 6 June 1944, members of the 2nd Ranger
Battalion under Cpt. Miller fight ashore to secure a
beachhead","vote_count":394695,"popularity":8.5,"poster_path":"/9UwfRlvq6Eekewf3
QAW5Plx0iEL.jpg","runtime":0,"genres":[{"id":"3","name":"Drama"},{"id":"18","name
":"War"},{"id":"7","name":"Action"},{"id":"1","name":"History"}]}
...
To search movies by movieID, movie JSON is stored in Oracle NoSQL Database, as following key-value structure:
Major-Key1
MV
Major-Key2
(MovieID)
829
MV
553
Value
{"id":829,"original_title":"Chinatown","release_date":"19
74","overview":"JJ 'Jake' Gittes is a private detective
who seems to specialize in matrimonial cases...",
"vote_count":110050,"popularity":8.4,"poster_path":"/6ybT
8RbSbd4AltIDABuv39dgqMU.jpg","runtime":0,"genres":[{"id":
"8","name":"Crime"},{"id":"3","name":"Drama"},{"id":"20",
"name":"Mystery"},{"id":"9","name":"Thriller"}]}
{"id":553,"original_title":"Dogville","release_date":"200
3","overview":"Late one night ..",
"vote_count":63536,"popularity":8,"poster_path":"/ydEUCkb
kdCizibt7BQehQ5cGROO.jpg","runtime":0,"genres":[{"id":"3"
,"name":"Drama"},{"id":"20","name":"Mystery"},{"id":"9","
name":"Thriller"}]}
Movie schema is created to match the movie JSON string, and JSON binding is later used in the code to serialize
and deserialize the JSON object to/from the KV Store. To view the movie schema you can double click movie.avsc
from JDEV. There are few more schemas that we are going to use to store Genre, Cast, Crew, & user Activity
information into the store. Feel free to view the definition by clicking the .avsc files.
To search movies by genreID, an association between genreID and movieID(s) is created in the store. Please note
that only major-minor Key associations are defined with Value set to an empty string.
Major-Key1
GN_MV
GN_MV
GN_MV
GN_MV
GN_MV
GN_MV
GN_MV
Major-Key2
(GenreID)
8
3
20
9
3
20
9
Minor-Key1
(MovieID)
829
829
829
829
553
553
553
Value
""
""
""
10
Oracle NoSQL Database
11
Oracle NoSQL Database
Instructions:
1.
2.
3.
4.
5.
6.
7.
12
Oracle NoSQL Database
8.
9.
Click on the movie that you just selected, which appears in the Recently Browsed section.
10.
When the description appears, click on the arrow that is visible in the middle of the movie image.
11.
12.
Click the stop button and close the window. Notice the change in status (green status bar).
13
Oracle NoSQL Database
14
Oracle NoSQL Database
Instructions:
1.
Create a new Java class. Select File > New from the drop down menu.
2.
15
Oracle NoSQL Database
3.
Type new class-name (MovieTest) and the package name (oracle.demo.oow.bd.test) and hit OK button.
4.
Lets create a method getMovieById(int movieId) that takes movieId as an input argument, as well as main
method that will call getMovieById method by passing a movieId. This is how your class should look like
at the end.
package oracle.demo.oow.bd.test;
import java.util.ArrayList;
import java.util.List;
import oracle.kv.KVStore;
import oracle.kv.KVStoreConfig;
import oracle.kv.KVStoreFactory;
import oracle.kv.Key;
import oracle.kv.Value;
import oracle.kv.ValueVersion;
public class MovieTest {
public MovieTest() {
super();
}
public void getMovieById(int movieId) {
/** TODO - Code to fetch data from KV-Store will go here **/
}
public static void main(String[] args) {
MovieTest movieTest = new MovieTest();
movieTest.getMovieById(829);
}
}
16
Oracle NoSQL Database
Note: When you copy paste the code from document into the JDev, the code might lose the indentation. To indent
the code, right-click in JDev main-pain and select Reformat
5.
Now lets create a connection to a running instance of kvstore, identified by name kvstore, and listening
at port 5000. Add the code right bellow the /** TODO - Code to fetch data from KV-Store will go here -**/
Key key = null;
KVStore kvStore = null;
try {
if (movieId > -1) {
/**
* Create a handle to the kvStore, so we can run get/put operations
*/
kvStore =
KVStoreFactory.getStore(new KVStoreConfig("kvstore",
"localhost:5000"));
/** TODO Add code here to create a KEY **/
}
} catch (Exception e) {
System.out.println("ERROR: Please make sure Oracle NoSQL Database is
running");
}
17
Oracle NoSQL Database
6.
Next, create a Key by setting a prefix MV and movieId as the major components to the Key. Add the
code right bellow the /** TODO Add code here to create a KEY --**/
/**
* Create a key to fetch movie JSON from the kv-store.
* In our case we saved movie information using this key structure:
* /MV/movieId/ .
*/
List<String> majorComponent = new ArrayList<String>();
majorComponent.add("MV");
majorComponent.add(Integer.toString(movieId));
key = Key.createKey(majorComponent);
/** TODO Add code to execute get operation **/
7.
Once we defined the key, next step is to run the get operation by passing the key. Add this code right
bellow /** TODO Add code to execute get operation **/
/**
* Now run a get operation, which will return you ValueVersion
* object.
*/
ValueVersion valueVersion = kvStore.get(key);
/** TODO Add code to display the Value (i.e. movie-information) **/
8.
Lets now display the Value, which should be the JSON representation of the movie details. Paste this
code right bellow /** TODO Add code to display the Value .. **/
/**
* If ValueVersion is not null, get the Value part and convert
* the Byte[] into String object.
*/
if (valueVersion != null) {
Value value = valueVersion.getValue();
String movieJsonTxt = new String(value.getValue());
System.out.println(movieJsonTxt);
} //if (valueVersion != null)
18
Oracle NoSQL Database
9.
Now run your program from the JDev by right clicking in the main pane and selecting the green play
icon. What do you see on the stdout?
19
Oracle NoSQL Database