Quantcast
Channel: Oracle
Viewing all articles
Browse latest Browse all 1814

Wiki Page: Using Oracle NoSQL Database with Groovy

$
0
0
Written by Deepak Vohra Groovy is an object-oriented, dynamic language for the Java Virtual Machine, compiles to Java bytecode and integrates with all Java libraries. Groovy rankings have recently surged from 37 to 16 since an year ago (Refer http://www.tiobe.com/tiobe-index/ ). Groovy can be used as a scripting language for the Java platform. While Groovy is very similar to Java it does have some salient differences with Java. A Groovy class with a main method can be run as an uncompiled script. In this article we shall use Oracle NoSQL Database with Groovy. We shall connect to Oracle NoSQL Database from a Groovy script, and run CRUD (Create, Read, Update and Delete) operations in Oracle NoSQL Database. The article has the following sections. Setting the Environment Download the following software. Groovy 2.2 (or later version) from http://groovy.codehaus.org/ . Oracle NoSQL Database 3.05 (or later version) from http://www.oracle.com/technetwork/products/nosqldb/downloads/default-495311.html . Oracle NoSQL Database, Client Driver 3.05 (or later version) from http://www.oracle.com/technetwork/products/nosqldb/downloads/default-495311.html. Set GROOVY_HOME environment variable. Add GROOVY_HOME/bin to PATH environment variable. Add C:\OracleNoSQLDatabase\kv-client-3.0.5\kv-3.0.5\lib\kvclient.jar to classpath. Create the lightweight version of Oracle NoSQL Database called kvlite with the following command. java -jar C:/OracleNoSQLDatabase/kv-ce-3.0.5/kv-3.0.5/lib/kvstore.jar kvlite The output from the command indicates that the kvlite store gets created. Creating a Connection In this section we shall connect with Oracle NoSQL Database using Groovy. Create a Groovy script Connect_oraclenosql.groovy . Declare connection parameters for the store name, host, and port. static String storeName = "kvstore" static String hostName = "localhost" static String port = "5000" Define a method connect (storeName, connectionString) in which create an instance of KVStoreConfig , which represents the configuration parameters to create a handle for a KV store, using class constructor KVStoreConfig( String storeName, String ... helperHostPort) . def connect (storeName, connectionString) { KVStoreConfig kVStoreConfig = new KVStoreConfig (storeName, connectionString) } Using the KVStoreFactory static method getStore(KVStoreConfig config) obtain an instance of KVStore , which is a handle to the KV store. KVStore store = KVStoreFactory.getStore(kVStoreConfig) Output a message to indicate if a connection gets established and output an exception message if an exception gets generated. Create an instance of the Connect_oraclenosql class and invoke the method connect from the main method supplying the store name and connection String as arguments. Connect_oraclenosql connect = new Connect_oraclenosql() String connectionString = hostName + ":" + port connect.connect(storeName,connectionString) The Connect_oraclenosql class is listed below. import oracle.kv.KVStore import oracle.kv.KVStoreConfig import oracle.kv.KVStoreFactory class Connect_oraclenosql { KVStore store static String storeName = "kvstore" static String hostName = "localhost" static String port = "5000" public static void main (args) { try { Connect_oraclenosql connect = new Connect_oraclenosql() String connectionString = hostName + ":" + port connect.connect(storeName,connectionString) } catch (Exception ex) { println (ex.toString ()) } } public Connect_oraclenosql() { } def connect (storeName, connectionString) { KVStoreConfig kVStoreConfig = new KVStoreConfig (storeName, connectionString) try { store = KVStoreFactory.getStore(kVStoreConfig) println ("Connection to Oracle NoSQL database established ") } catch (Exception ex) { errorMessage = "ERROR: No connection to the store. " + ex.toString () println ("Error: " + errorMessage) } } } Run the Connect_oraclenosql.groovy class with the following command. >groovy Connect_oraclenosql.groovy The output indicates that a connection gets established. Adding Data In this section we shall add data to Oracle NoSQL database for which create a Groovy script Create_oraclenosql.groovy . As in the preceding section declare variables for connection parameters store name, host and port. static String storeName = "kvstore" static String hostName = "localhost" static String port = "5000" Add a method create (storeName, connectionString) in which obtain a handle to the KVStore instance as discussed in the previous section. def create (storeName, connectionString) { KVStoreConfig kVStoreConfig = new KVStoreConfig (storeName, connectionString) try { store = KVStoreFactory.getStore(kVStoreConfig) } catch (Exception ex) { println ("Error: " + ex.toString ()) } Add another method called put (String keysString, String valueString) in which we shall set key/value pairs in the KV store. def put (String keysString, String valueString) { } Declare two String parameterized List instances, one for the major key path components and one for the minor key path components. List majorComponents = new ArrayList () List minorComponents = new ArrayList () Create an array for the different key path components. Key path components are demarcated with a ‘/’ and major key path components are demarcated from the minor key path components with a ‘-‘. String [] keysArray = keysString.split ("/") Using an enhanced for loop iterate over the array and add the key path components to the List for the major key path if the component precedes the ‘-‘ character and add the component to the List for the minor key path if the component is after the ‘-‘. boolean isMajor = true for (i in 0..keysArray.length -1) { if (keysArray [i] == "-") { isMajor = false } if (isMajor) { majorComponents.add (keysArray [i]) } else { if (keysArray [i] != "-") { minorComponents.add (keysArray [i]) } } } The Key class provides the static method createKey(List majorPath) to create a Key instance to be used if the key path has only major key path components and the createKey(List majorPath, List minorPath) method to be used to create a Key instance if the key path has both major and minor key path components. To determine if a List has any components invoke the size() method. Create a Key instance using the createKey(List majorPath, List minorPath) method if the sizes of both the List s are greater than 0 and create a Key instance using the createKey(List majorPath) method if the only the List for the major key paths has components. if ((majorComponents.size () > 0) && (minorComponents.size () > 0)) { fieldKey = Key.createKey (majorComponents, minorComponents) } else if ((majorComponents.size () > 0) & (minorComponents.size () majorComponents = new ArrayList () List minorComponents = new ArrayList () String [] keysArray = keysString.split ("/") boolean isMajor = true for (i in 0..keysArray.length -1) { if (keysArray [i] == "-") { isMajor = false } if (isMajor) { majorComponents.add (keysArray [i]) } else { if (keysArray [i] != "-") { minorComponents.add (keysArray [i]) } } } if ((majorComponents.size () > 0) && (minorComponents.size () > 0)) { fieldKey = Key.createKey (majorComponents, minorComponents) } else if ((majorComponents.size () > 0) & (minorComponents.size () groovy Create_oraclenosql.grrovy If an exception is not generated, as in the following output, the K/V pairs get added to the Oracle NoSQL Database. Getting Data In this section we shall get the data stored in Oracle NoSQL Database for which create a Groovy script Get_oraclenosql.groovy . As in the preceding sections declare variables for connection parameters store name, host and port. static String storeName = "kvstore" static String hostName = "localhost" static String port = "5000" Add a method connect (storeName, connectionString) in which obtain a handle to the KVStore instance as discussed in the previous section. Add another method called get (String keysString) in which we shall get the value for a key in the KV store. def get (String keysString) { } In the get(String) method declare two String parameterized List instances, one for the major key path components and one for the minor key path components, as discussed in the preceding section. Create an array for the different key path components and iterate over the array to split the array components into major key path List and minor key path List . Also as discussed in the preceding section create a Key instance using the major key path components List and the minor key path components List . Invoke the get ( Key key) method in KVStore to obtain the ValueVersion instance for the supplied Key instance. ValueVersion valueVersion = store.get (fieldKey) If the ValueVersion is not null invoke the getValue() method to get the Value associated with the ValueVersion instance. Subsequently invoke the getValue() method again to get the byte[] array in the Value instance. Create a String instance from the byte[] array and output the String . if (valueVersion != null) { fieldValue = new String ( valueVersion.getValue (). getValue (), encoding) println (fieldValue) In the connect (storeName, connectionString) method invoke the get (String keysString) method to get value for the different k/v pairs stored in the KV store. get ("catalog1/-/journal") get ("catalog1/-/publisher") get ("catalog1/-/edition") get ("catalog1/-/title") get ("catalog1/-/author") get ("catalog2/-/journal") get ("catalog2/-/publisher") get ("catalog2/-/edition") get ("catalog2/-/title") get ("catalog2/-/author") In the main method create an instance of Get_oraclenosql and invoke the connect (storeName, connectionString) method supplying a connection String for the host and the port of the Oracle NoSQL Database. Get_oraclenosql connect = new Get_oraclenosql() String connectionString = hostName + ":" + port connect.connect(storeName,connectionString) When the script is run the main method is invoked from which the connect (storeName, connectionString) method is invoked and from which the get (String keysString) method is invoked for each key in the KV store. The Get_oraclenosql class is listed below. import oracle.kv.KVStore import oracle.kv.KVStoreConfig import oracle.kv.KVStoreFactory import oracle.kv.Key import oracle.kv.Value import oracle.kv.ValueVersion class Get_oraclenosql { KVStore store Key fieldKey String fieldValue static String storeName = "kvstore" static String hostName = "localhost" static String port = "5000" String encoding = "UTF-8" public static void main (args) { try { Get_oraclenosql connect = new Get_oraclenosql() String connectionString = hostName + ":" + port connect.connect(storeName,connectionString) } catch (Exception ex) { println (ex.toString ()) } } public Get_oraclenosql() { } def connect (storeName, connectionString) { KVStoreConfig kVStoreConfig = new KVStoreConfig (storeName, connectionString) try { store = KVStoreFactory.getStore(kVStoreConfig) get ("catalog1/-/journal") get ("catalog1/-/publisher") get ("catalog1/-/edition") get ("catalog1/-/title") get ("catalog1/-/author") get ("catalog2/-/journal") get ("catalog2/-/publisher") get ("catalog2/-/edition") get ("catalog2/-/title") get ("catalog2/-/author") } catch (Exception ex) { println ("Error: " + ex.toString ()) } } def get (String keysString) { List majorComponents = new ArrayList () List minorComponents = new ArrayList () String [] keysArray = keysString.split ("/") boolean isMajor = true for (i in 0..keysArray.length -1) { if (keysArray [i] == "-") { isMajor = false } if (isMajor) { majorComponents.add (keysArray [i]) } else { if (keysArray [i] != "-") { minorComponents.add (keysArray [i]) } } } if ((majorComponents.size () > 0) && (minorComponents.size () > 0)) { fieldKey = Key.createKey (majorComponents, minorComponents) } else if ((majorComponents.size () > 0) & (minorComponents.size () groovy Get_oraclenosql.groovy The key/value pairs stored in Oracle NoSQL get output. Updating Data In this section we shall update the value in the key/value pairs stored in oracle NoSQL database from which create the Update_oraclenosql.groovy script. The KVStore method put ( Key key, Value value) , which is used to put key/value pairs may also be used to update key/value pairs. The Update_oraclenosql.groovy script is exactly the same as the Create_oraclenosql.groovy script except that the values supplied in the invocation of the put (String keysString, String valueString) method are the updated values. put ("catalog1/-/journal", "Oracle-Magazine") put ("catalog1/-/publisher", "Oracle-Publishing") put ("catalog1/-/edition", "Nov-Dec 2004") put ("catalog1/-/title", "From ADF UIX to JSF") put ("catalog1/-/author", "Jacobi, Jonas") put ("catalog2/-/journal", "Oracle-Magazine") put ("catalog2/-/publisher", "Oracle-Publishing") put ("catalog2/-/edition", "March-April 2005") put ("catalog2/-/title", "Starting with Oracle ADF") put ("catalog2/-/author", "Muench, Steve") The Update_oraclenosql.groovy script is listed below. import oracle.kv.KVStore import oracle.kv.KVStoreConfig import oracle.kv.KVStoreFactory import oracle.kv.Key import oracle.kv.Value class Update_oraclenosql { KVStore store Key fieldKey Value fieldValue static String storeName = "kvstore" static String hostName = "localhost" static String port = "5000" public static void main (args) { try { Update_oraclenosql connect = new Update_oraclenosql() String connectionString = hostName + ":" + port connect.connect(storeName,connectionString) } catch (Exception ex) { println (ex.toString ()) } } public Update_oraclenosql() { } def connect (storeName, connectionString) { KVStoreConfig kVStoreConfig = new KVStoreConfig (storeName, connectionString) try { store = KVStoreFactory.getStore(kVStoreConfig) put ("catalog1/-/journal", "Oracle-Magazine") put ("catalog1/-/publisher", "Oracle-Publishing") put ("catalog1/-/edition", "Nov-Dec 2004") put ("catalog1/-/title", "From ADF UIX to JSF") put ("catalog1/-/author", "Jacobi, Jonas") put ("catalog2/-/journal", "Oracle-Magazine") put ("catalog2/-/publisher", "Oracle-Publishing") put ("catalog2/-/edition", "March-April 2005") put ("catalog2/-/title", "Starting with Oracle ADF") put ("catalog2/-/author", "Muench, Steve") } catch (Exception ex) { println ("Error: " + ex.toString ()) } } def put (String keysString, String valueString) { List majorComponents = new ArrayList () List minorComponents = new ArrayList () String [] keysArray = keysString.split ("/") boolean isMajor = true for (i in 0..keysArray.length -1) { if (keysArray [i] == "-") { isMajor = false } if (isMajor) { majorComponents.add (keysArray [i]) } else { if (keysArray [i] != "-") { minorComponents.add (keysArray [i]) } } } if ((majorComponents.size () > 0) && (minorComponents.size () > 0)) { fieldKey = Key.createKey (majorComponents, minorComponents) } else if ((majorComponents.size () > 0) & (minorComponents.size () groovy Update_oraclenosql.groovy The updated values get stored and subsequently output. Counting Rows In this section we shall count the number of records in the Oracle NoSQL Database for which create a Groovy script Count_oraclenosql.groovy . Add a method called connect (storeName, connectionString) to connect to Oracle NoSQL Database and add a method called count () to count the number of records. In the connect method obtain an instance of KVStore as discussed in an earlier section. Invoke the count() method from the connect() method. In the count() method invoke the KVStore method storeKeysIterator ( Direction direction, int batchSize) to obtain an Iterator over the keys stored in the KV store. Iterator iterator = store.storeKeysIterator (Direction.UNORDERED, 0) Using an Integer variable and a while loop iterate over the keys and increment the Integer variable. Integer i = 0 while (iterator.hasNext ()){ i = i + 1 iterator.next () } Subsequently output the Integer variable. println ("Total number of Records: " + i) The Count_oraclenosql.groovy script is listed below. import oracle.kv.KVStore import oracle.kv.KVStoreConfig import oracle.kv.KVStoreFactory import oracle.kv.Direction class Count_oraclenosql { KVStore store Key fieldKey Value fieldValue static String storeName = "kvstore" static String hostName = "localhost" static String port = "5000" public static void main (args) { try { Count_oraclenosql count = new Count_oraclenosql() String connectionString = hostName + ":" + port count.connect(storeName,connectionString) } catch (Exception ex) { println (ex.toString ()) } } public Count_oraclenosql() { } def connect (storeName, connectionString) { KVStoreConfig kVStoreConfig = new KVStoreConfig (storeName, connectionString) try { store = KVStoreFactory.getStore(kVStoreConfig) count() } catch (Exception ex) { println ("Error: " + ex.toString ()) } return } def count () { try { Iterator iterator = store.storeKeysIterator (Direction.UNORDERED, 0) Integer i = 0 while (iterator.hasNext ()){ i = i + 1 iterator.next () } println ("Total number of Records: " + i) } catch (Exception ex) { errorMessage = "ERROR in countAll: " + ex.toString () println("ERROR in countAll: " + ex.toString ()) } return } } Run the script with the following command. groovy>groovy Count_oraclenosql.groovy The output indicates that the number of records in the KV store is 10. Deleting Data In this section we shall delete the data stored in Oracle NoSQL Database for which create a Groovy script Delete_oraclenosql.groovy . As in the preceding sections declare variables for connection parameters store name, host and port. Add a method connect (storeName, connectionString) in which obtain a handle to the KVStore instance and add another method called delete (String keysString) in which we shall delete the value for a key in the KV store. In the connect (storeName, connectionString) method invoke the delete (String keysString) method to delete the key paths to delete. delete ("catalog2/-/journal") delete ("catalog2/-/publisher") delete ("catalog2/-/edition") delete ("catalog2/-/title") delete ("catalog2/-/author") In the delete (String keysString) method declare two String parameterized List instances, one for the major key path components and one for the minor key path components. From the supplied keysString create an array for the different key path components and iterate over the array to split the array components into major key path List and minor key path List . Create a Key instance using the major key path components List and the minor key path components List . Invoke the delete ( Key key) method in KVStore to delete the Key instance from the KV store. The delete ( Key key) method method returns a boolean to indicate if the Key is deleted. Output the boolean value. boolean bool = store.delete (fieldKey) println ("Field Key deleted: "+bool) In the main method create an instance of Delete_oraclenosql and invoke the connect (storeName, connectionString) method supplying a connection String for the host and the port of the Oracle NoSQL Database. Delete_oraclenosql delete = new Delete_oraclenosql() String connectionString = hostName + ":" + port delete.connect(storeName,connectionString) When the script is run the main method is invoked from which the connect (storeName, connectionString) method is invoked and from which the delete (String keysString) method is invoked for each key path to delete. The Delete_oraclenosql class is listed below. import oracle.kv.KVStore import oracle.kv.KVStoreConfig import oracle.kv.KVStoreFactory import oracle.kv.Direction class Delete_oraclenosql { KVStore store Key fieldKey Value fieldValue static String storeName = "kvstore" static String hostName = "localhost" static String port = "5000" public static void main (args) { try { Delete_oraclenosql delete = new Delete_oraclenosql() String connectionString = hostName + ":" + port delete.connect(storeName,connectionString) } catch (Exception ex) { println (ex.toString ()) } } public Delete_oraclenosql() { } def connect (storeName, connectionString) { KVStoreConfig kVStoreConfig = new KVStoreConfig (storeName, connectionString) try { store = KVStoreFactory.getStore(kVStoreConfig) delete ("catalog1/-/journal") delete ("catalog1/-/publisher") delete ("catalog1/-/edition") delete ("catalog1/-/title") delete ("catalog1/-/author") } catch (Exception ex) { println ("Error: " + ex.toString ()) } return } def delete (String keysString) { List majorComponents = new ArrayList () List minorComponents = new ArrayList () String [] keysArray = keysString.split ("/") boolean isMajor = true for (i in 0..keysArray.length -1) { if (keysArray [i] == "-") { isMajor = false } if (isMajor) { majorComponents.add (keysArray [i]) } else { if (keysArray [i] != "-") { minorComponents.add (keysArray [i]) } } } if ((majorComponents.size () > 0) && (minorComponents.size () > 0)) { fieldKey = Key.createKey (majorComponents, minorComponents) } else if ((majorComponents.size () > 0) & (minorComponents.size () groovy Delete_oraclenosql.groovy The output indicates that the key paths for which the delete (String keysString) method is invoked get deleted. Subsequently, invoke the Get_oraclenosql.groovy to output the key/value pairs still in the KV store. groovy>groovy Get_oraclenosql.groovy Only five of the ten key/value pairs get listed as the other five have been deleted. In this article we discussed connection with Oracle NoSQL Database with a Groovy script and running CRUD operations in Oracle NoSQL Database.

Viewing all articles
Browse latest Browse all 1814

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>