Working with Oracle NoSQL

Oracle NoSQL Download and Startup

NoSQL is a Key , Value data base , In Simple words traditional RDBMS Database had tables , Columns , Rows etc .. example

Employee Table

Empid -----> Name ------> Age ------> Department -----> Location

1........... James ...... 30 .......... IT .............. London

2........... Mike ........ 31 .......... BT ............. Paris

However the world of NoSQL is little different

(EmpId, Employee Data)
(1, [James, 30, IT, London, West Street, Cross Road, Dead End])
(2, [Mike, 31, BT, Paris, High Street, In the Bar, Over the Moon ])

 

You can download from here

http://www.oracle.com/technetwork/database/database-technologies/nosqldb/downloads/index.html

Extract the zip file to create kv-3.1.7 directory , the database start up command is

java -jar /home/ubuntu/kv-3.1.7/lib/kvstore.jar kvlite
Output Window
Created new kvlite store with args:
-root ./kvroot -store kvstore -host ubuntu -port 5000 -admin 5001

Oracle NOSQL Console AND JDeveloper

You can check if NoSQL Database engine is working at this Web URL http://localhost:5001/ n1

It might be a good idea to have an IDE , With All NoSQL Jar Files Ready for the Java Projects , This avoids the challenges of compiling and running Java core and you can focus on something better.

n2

NOSQL setting up database connection

To create DB Connection

    NoSQLDemo(String[] argv)
    {
        // Constructor Code to get Connection
        String storeName = "kvstore";
        String hostName = "localhost";
        String hostPort = "5000";
        final int nArgs = argv.length;
        int argc = 0;
        while (argc <
        nArgs) {
            final String thisArg = argv[argc++];
            if (thisArg.equals("-store")) {
                if (argc <
                nArgs) {
                    storeName = argv[argc++];
                    } else {
                    usage("-store requires an argument");
                }
                } else if (thisArg.equals("-host")) {
                if (argc <
                nArgs) {
                    hostName = argv[argc++];
                    } else {
                    usage("-host requires an argument");
                }
                } else if (thisArg.equals("-port")) {
                if (argc <
                nArgs) {
                    hostPort = argv[argc++];
                    } else {
                    usage("-port requires an argument");
                }
                } else {
                usage("Unknown argument: " + thisArg);
            }
        }
        store = KVStoreFactory.getStore
        (new KVStoreConfig(storeName, hostName + ":" + hostPort));
    }

To Close NoSQL DB Connection

    void close(){
        store.close();
    }

CRUD Operation on key value pair in nosql

import oracle.kv.KVStore;
import oracle.kv.KVStoreConfig;
import oracle.kv.KVStoreFactory;
import oracle.kv.Key;
import oracle.kv.Value;
import oracle.kv.ValueVersion;
import oracle.kv.Direction;
import oracle.kv.KeyValueVersion;

import java.util.ArrayList;
import java.util.Iterator;

public class GetSetKV
{
    private final KVStore store;
    public static void main(String[] args)
    {
        GetSetKV getSetKV = new GetSetKV(args);
        getSetKV.exampleGetSet();
        //getSetKV.exampleJustGet();
        //getSetKV.exampleDelete();
    }
    void exampleGetSet()
    {

        final String keyString = "Empid_001";
        final String valueString = "Peter Parker"; 

        // Insert of KV happens only if the Specified Key is Not Present
        store.putIfAbsent(Key.createKey(keyString), Value.createValue(valueString.getBytes()));
        final ValueVersion valueVersion = store.get(Key.createKey(keyString));
        System.out.println(keyString + " " + new String(valueVersion.getValue().getValue()));
        store.close();
    }
    void exampleJustGet()
    {

        final String keyString = "Empid_001";  

        // After Insert Operation you can get the Key Value pair back
        final ValueVersion valueVersion = store.get(Key.createKey(keyString));
        System.out.println(keyString + " " + new String(valueVersion.getValue().getValue()));
        store.close();
    }
    void exampleDelete()
    {

        final String keyString = "Empid_003";
        System.out.println("Deleting for Key "+keyString);
        store.delete(Key.createKey(keyString));
        store.close();
    }

    GetSetKV(String[] argv)
    {
        // Establish NoSQL DB Connection
        String storeName = "kvstore";
        String hostName = "localhost";
        String hostPort = "5000";
        final int nArgs = argv.length;
        int argc = 0;
        while (argc <
        nArgs) {
            final String thisArg = argv[argc++];
            if (thisArg.equals("-store")) {
                if (argc <
                nArgs) {
                    storeName = argv[argc++];
                    } else {
                    usage("-store requires an argument");
                }
                } else if (thisArg.equals("-host")) {
                if (argc <
                nArgs) {
                    hostName = argv[argc++];
                    } else {
                    usage("-host requires an argument");
                }
                } else if (thisArg.equals("-port")) {
                if (argc <
                nArgs) {
                    hostPort = argv[argc++];
                    } else {
                    usage("-port requires an argument");
                }
                } else {
                usage("Unknown argument: " + thisArg);
            }
        }
        store = KVStoreFactory.getStore
        (new KVStoreConfig(storeName, hostName + ":" + hostPort));
    }

    private void usage(String message) {
        System.out.println("\n" + message + "\n");
        System.out.println("usage: " + getClass().getName());
        System.out.println("\t-store  (default: kvstore) " +
        "-host  (default: localhost) " +
        "-port  (default: 5000)");
        System.exit(1);
    }
}

Concept of Major and Minor Keys in NoSQL

if every employee had distinct name in a organization it would have been great like

Emp Id 001 -- James
Emp Id 002 -- John
Emp Id 003 -- Mike

But in real world names repeat

Emp Id 001 -- James Parker Jr.
Emp Id 002 -- John Stein
Emp Id 003 -- Mike Paul
Emp Id 004 -- James Smith
Emp Id 005 -- Mike Johnson
Emp Id 006 -- James Parker Sr.

Then the challenge would be addressed by using a concept of major and minor keys , to keep each record fairly distinguishable , remember we dont have a concept of Primary Key as used in RDBMS here , so we need figure out ways to keep a key distinct

521463

In the above Picture Key is divided into 1 major key and 2 minor keys , userid is major key , subscriptions and address are 2 minor keys

each of these combination has corresponding values

userid / subscriptions [key] --> Expiration Data [Value]
userid / address [key] --> Phone Number [Value 1], Email Id [ Value 2]

Oracle

Content

Oracle

Content

Oracle

Content

Oracle

Content