Otn Developer Day: Oracle Big Data: Introduction To Oracle Nosql Database
Otn Developer Day: Oracle Big Data: Introduction To Oracle Nosql Database
ii
Oracle NoSQL Database
Oracle NoSQL Database Hands on Workshop
In this lab, you will open and run the MovieDemo application using JDeveloper Studio.
echo $KVROOT
echo $KVHOME
echo $KVDEMOHOME
rm -rf $KVROOT
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
Oracle NoSQL Database Hands on Workshop
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:
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 field’s 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.
Let’s 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
2. Look for a response similar to what is listed below. Minimize window (leave running).
3. Open a new tab in the terminal window and start a admin session:
kv->
This is what you should get after you successfully registering the Customer schema:
This is what you should get after you successfully registering the schema:
6
Oracle NoSQL Database
6. We need to register few more schemas as mentioned earlier:
7. Run ‘show schemas’ command to make sure all six schemas are registered.
8. In JDeveloper Studio, open oracle.demo.oow.bd.dao.CustomerDAO and notice few things (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) deserializes Value object back into CustomerTO POJO.
7
Oracle NoSQL Database
9. Couple more things: In the same CustomerDAO class:
10. Let’s upload the customer profile data into NoSQL Database now. Open java class
oracle.demo.oow.bd.loader.CustomerProfileLoaderFile
11. 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
12. To confirm, login from the Movieplex web page again. You should see a welcome page as shown in the
image below.
13. Notice that there are no movies available yet. Lab exercise 3 will load the movies.
9
Oracle NoSQL Database
Oracle NoSQL Database Hands on Workshop
Movie information data is represented as JSON structure and details of quarter million movie tiles can be found
from file $KVDEMOHOME/dataparser/metadata/imdb/movie-info.out (If the file does not exist then
unzip $KVDEMOHOME/dataparser/metadata/imdb/movie-info.zip file)
{"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:
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.
10
Oracle NoSQL Database
Instructions:
11
Oracle NoSQL Database
8. Close the window and observe what happens.
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.
12. Click the stop button and close the window. Notice the change in status (green status bar).
12
Oracle NoSQL Database
13
Oracle NoSQL Database
Oracle NoSQL Database Hands on Workshop
Instructions:
1. Create a new Java class. Select File > New from the drop down menu.
14
Oracle NoSQL Database
3. Type new class-name (MovieTest) and the package name (oracle.demo.oow.bd.test) and hit OK button.
4. Let’s 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;
5. Now let’s 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 --
**/
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"));
}
} catch (Exception e) {
System.out.println("ERROR: Please make sure Oracle NoSQL Database is
running");
}
16
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. Let’s 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)
17
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?
18
Oracle NoSQL Database