Aim: Design Persistent Objects using JDO and implement min 10 queries on objects using JDOQL in ObjectDB NOSQL DATABASE
Procedure:
First install and configure Cassandra from http://www.professionalcipher.com/2018/03/installation-of-nosql-database-cassandra.html
#Start Cassandra Daemons
# The cassandra daemon should start in the foreground
#(don’t press ctrl + c; as it’ll terminate the daemon)
#Now open a new terminal
#Create a keyspace
cqlsh>
cqlsh>
#Now open Eclipse
# Install DataNucleus plugin In Eclipse
# Extract the downloaded zip file
When you open the zip you will find DataNucleus jars files in the lib directory, and dependency jars in the deps directory.
# From the lib directory copy all jar files except following
datanucleus-jodatime-4.1.1.jar
datanucleus-java8-4.2.1.jar
datanucleus-guava-4.1.3.jar
# In eclipse -> right click on project exp6 -> Paste
# One by one, right click on pasted jar file -> Build path -> add to build path
# Add all pasted jar files to build path
# Similarly from the deps folder copy all jar files
# Once again In eclipse -> right click on project exp6 -> Paste
# One by one, right click on pasted jar file -> Build path -> add to build path
# Add all pasted jar files to build path
# In eclipse -> Create a new class - > Product -> add following code in it
# save the file
# In eclipse -> Create a new class - > Book -> add following code in it
# In eclipse -> Create a new class - > Inventory -> add following code in it
# In eclipse -> Create a new class - > MyApp -> add following code in it
# save the file
# Plugin configuration
# Plugin configuration – Enhancer
# Plugin configuration – SchemaTool
# Right-click project exp6 -> select DataNucleus ->" Add DataNucleus Support "
# Right-click on package exp6 -> DataNucleus -> select " Create JDO XML Metadata File " -> Give Filename-> package.jdo -> finish
# Right-click on package exp6 -> DataNucleus -> select " Create persistence.xml file ”
# Edit persistence.xml file
# Save the file
# Download the cassandra-java driver from http://downloads.datastax.com/java-driver/cassandra-java-driver-2.0.2.tar.gz
# Extract the downloaded file.
# Paste all jar files (total 12) from the extracted folder into your eclipse project (Note :: 10 jar files are present under lib folder)
# In eclipse -> One by one, right click on pasted jar file -> Build path -> add to build path
# Add all pasted jar files to the build path
# DONE!!! Right-click on project exp6 -> DataNucleus-> select " Run Enhancer tool "
OUTPUT:
# Make sure that cassandra is running before doing next step
# Right-click on project exp6 -> DataNucleus-> select " Run Schema tool " -> Set Connection URL -> cassandra: -> next -> finish
OUTPUT:
# Right click on MyApp.java -> Run as -> Java Application
OUTPUT:
# In cqlsh prompt, verify the output
cqlsh>
OUTPUT:
# Now edit MyApp.java to add some more JDOQL queries one by one ( minimum 10 are expected)
# Then, Run MyApp.java
# Verify the output in cqlsh prompt by using SELECT query.
Procedure:
First install and configure Cassandra from http://www.professionalcipher.com/2018/03/installation-of-nosql-database-cassandra.html
#Start Cassandra Daemons
~/cassandra/bin/cassandra -f
# The cassandra daemon should start in the foreground
#(don’t press ctrl + c; as it’ll terminate the daemon)
#Now open a new terminal
~/cassandra/bin/cqlsh
#Create a keyspace
cqlsh>
CREATE KEYSPACE keyspace1 WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
cqlsh>
USE keyspace1
#Now open Eclipse
# Install DataNucleus plugin In Eclipse
- Go to Help
- Install new software
- Add
- Set name as DataNucleus
- Set Location as http://www.datanucleus.org/downloads/eclipse-update/
- ok
- Check the checkbox
- Next
- Next
- I accept
- Finish
- Ignore the warning (if any)
- Restart eclipse
- yes
# Extract the downloaded zip file
When you open the zip you will find DataNucleus jars files in the lib directory, and dependency jars in the deps directory.
# From the lib directory copy all jar files except following
datanucleus-jodatime-4.1.1.jar
datanucleus-java8-4.2.1.jar
datanucleus-guava-4.1.3.jar
# In eclipse -> right click on project exp6 -> Paste
# One by one, right click on pasted jar file -> Build path -> add to build path
# Add all pasted jar files to build path
# Similarly from the deps folder copy all jar files
# Once again In eclipse -> right click on project exp6 -> Paste
# One by one, right click on pasted jar file -> Build path -> add to build path
# Add all pasted jar files to build path
# In eclipse -> Create a new class - > Product -> add following code in it
package exp6;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable
public class Product
{
@PrimaryKey
@Persistent (valueStrategy=IdGeneratorStrategy.INCREMENT)
long id;
String name = null;
String description = null;
double price = 0.0;
public Product(String name, String desc,double price)
{
this.name = name;
this.description = desc;
this.price = price;
}
}
# save the file
# In eclipse -> Create a new class - > Book -> add following code in it
package exp6;
import javax.jdo.annotations.PersistenceCapable;
@PersistenceCapable
public class Book extends Product
{
String author=null;
String isbn=null;
String publisher=null;
public Book(String name, String desc,double price, String author,String isbn, String publisher)
{
super(name,desc,price);
this.author=author;
this.isbn=isbn;
this.publisher=publisher;
}
}
# In eclipse -> Create a new class - > Inventory -> add following code in it
package exp6;
import java.util.HashSet;
import java.util.Set;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable
public class Inventory
{
@PrimaryKey
String name=null;
@SuppressWarnings({"rawtypes","unchecked"})Set<Product>products=new HashSet();
public Inventory(String name)
{
this.name=name;
}
public Set<Product> getProducts()
{
return products;
}
}
# In eclipse -> Create a new class - > MyApp -> add following code in it
package exp6;
import java.util.Iterator;
import java.util.List;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;
public class MyApp {
public static void main(String[] args)
{//TEST is the persistent unit name
PersistenceManagerFactory pmf =
JDOHelper.getPersistenceManagerFactory("TEST");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx=pm.currentTransaction();
try
{
tx.begin();
Inventory inv = new Inventory("My Inventory");
Product product = new Product("Sony Discman", "A standard discman from Sony", 49.99);
Product product1 = new Product("Sony xperia z1", "A smart phone", 149.99);
inv.getProducts().add(product);
inv.getProducts().add(product1);
pm.makePersistent(inv);
Query q = pm.newQuery("SELECT FROM " + Product.class.getName() + " WHERE price < 150.00 ORDER BY price ASC");
// add some more JDOQL queries here
List<Product> products = (List<Product>)q.execute();
Iterator<Product> iter = products.iterator();
while (iter.hasNext())
{
Product p = iter.next();
System.out.println("Name: "+p.name+"\t Description: "+p.description+"\tPrice: "+p.price);
}
tx.commit();
}
finally
{
if (tx.isActive())
{
tx.rollback();
}
pm.close();
}
}
}
# save the file
# Plugin configuration
- Right click on exp6 project
- Properties
- DataNucleus
- check “ Enable project specific settings ”
- Add JARS
- add all 18 jar files ( 10 from lib folder, 8 from deps folder)
- apply
- ok
# Plugin configuration – Enhancer
- Right click on exp6 project
- Properties
- double click on DataNucleus
- Enhancer
- check “ Enable project specific settings ”
- check verbose mode
- check Capture Output
- Give persistence-unit name -> TEST
# Plugin configuration – SchemaTool
- Right click on exp6 project
- Properties
- Under DataNucleus
- SchemaTool
- check “ Enable project specific settings ”
- check verbose mode
- Give persistence-unit name -> TEST
# Right-click project exp6 -> select DataNucleus ->" Add DataNucleus Support "
# Right-click on package exp6 -> DataNucleus -> select " Create JDO XML Metadata File " -> Give Filename-> package.jdo -> finish
# Right-click on package exp6 -> DataNucleus -> select " Create persistence.xml file ”
# Edit persistence.xml file
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="TEST">
<mapping-file>/home/huzaif/eclipse-workspace/exp6/src/exp6/package.jdo</mapping-file>
<properties>
<property name = "javax.jdo.option.ConnectionURL" value = "cassandra:" />
<property name = "javax.jdo.mapping.Schema" value = "keyspace1" />
<property name = "datanucleus.schema.autoCreateAll" value = "true" />
</properties>
</persistence-unit>
</persistence>
# Save the file
# Download the cassandra-java driver from http://downloads.datastax.com/java-driver/cassandra-java-driver-2.0.2.tar.gz
# Extract the downloaded file.
# Paste all jar files (total 12) from the extracted folder into your eclipse project (Note :: 10 jar files are present under lib folder)
# In eclipse -> One by one, right click on pasted jar file -> Build path -> add to build path
# Add all pasted jar files to the build path
# DONE!!! Right-click on project exp6 -> DataNucleus-> select " Run Enhancer tool "
OUTPUT:
ENHANCED (Persistable) : exp6.Inventory ENHANCED (Persistable) : exp6.Book ENHANCED (Persistable) : exp6.Product DataNucleus Enhancer completed with success for 3 classes. Timings : input=101 ms, enhance=74 ms, total=175 ms. Consult the log for full details
# Make sure that cassandra is running before doing next step
# Right-click on project exp6 -> DataNucleus-> select " Run Schema tool " -> Set Connection URL -> cassandra: -> next -> finish
OUTPUT:
DataNucleus SchemaTool : Input Files >> /home/huzaif/eclipse-workspace/exp6/bin/exp6/Book.class >> /home/huzaif/eclipse-workspace/exp6/bin/exp6/Inventory.class >> /home/huzaif/eclipse-workspace/exp6/bin/exp6/MyApp.class >> /home/huzaif/eclipse-workspace/exp6/bin/exp6/Product.class >> /home/huzaif/eclipse-workspace/exp6/bin/exp6/package.jdo >> /home/huzaif/eclipse-workspace/exp6/src/exp6/package.jdo
# Right click on MyApp.java -> Run as -> Java Application
OUTPUT:
log4j:WARN No appenders could be found for logger (DataNucleus.General). log4j:WARN Please initialize the log4j system properly. Name: Sony Discman Description: A standard discman from Sony Price: 49.99 Name: Sony xperia z1 Description: A smart phone Price: 149.99
# In cqlsh prompt, verify the output
cqlsh>
SELECT * FROM Product;
OUTPUT:
cqlsh:keyspace1> select * from product; id | description | name | price ----+------------------------------+----------------+-------- 0 | A standard discman from Sony | Sony Discman | 49.99 1 | A smart phone | Sony xperia z1 | 149.99 (2 rows) cqlsh:keyspace1>
# Now edit MyApp.java to add some more JDOQL queries one by one ( minimum 10 are expected)
# Then, Run MyApp.java
# Verify the output in cqlsh prompt by using SELECT query.
Comments
Post a Comment