Skip to content
PedroGomes edited this page Nov 11, 2010 · 4 revisions

This plug-in is still rather simple, but it fulfills the basic set of expected operations.
So I will try to describe next a small set of examples in how to use the plug-in with JDO based on the example provided from Datanucleus.
You can find the base example were:

http://sourceforge.net/projects/datanucleus/files/

Build the entity

Build an entity like in the original tutorial, but define what field is your primary key:


@PersistenceCapable public class Product { @PrimaryKey protected String name=null; ...

Store it

Simply persist the object


       try
       {
            tx.begin()
            Product product = new Product("Sony Discman","A discman from Sony",20.00);     
            pm.makePersistent(product);
            tx.commit();
        }
        catch (Exception e) {
        	e.printStackTrace();
	}
        finally
        {
            if (tx.isActive())
            {
                tx.rollback();
            }
            pm.close();
        }

Fetch it

You can fetch objects by an identifier extracted from the original object, if when persisting you save its identity like this


Object id = pm.getObjectId(product );

Than you can fetch them using the id:


       try
       {
            tx.begin()
            Product product = (Product) pm.getObjectById(id);
            tx.commit();
        }
        catch (Exception e) {
        	e.printStackTrace();
	}
        finally
        {
            if (tx.isActive())
            {
                tx.rollback();
            }
            pm.close();
        }

You can also chose to use the key value to fetch the object:

      Product product =  pm.getObjectById(Product.class, "Sony Discman");

Use Indexes


        @PersistenceCapable
        @Index(table="index",unique="true",members={"author"})
	public class Product
	{
              @PrimaryKey
              protected String name=null;
              
              protected String author;

With the present annotations, you can define the column family to use for the index, and the member (only one is allowed per index, but more than one index can be defined).

The unique option is used in this case to define if the index should be updated when the field is updated. If you are certain that the field is never updated you can define it as false and speedup the insertion.

Query it

You can query the results to fetch or delete multiple keys from the data store.
Simple ranges are also possible but remember the lexicographic key order in Cassandra, presuming you are using OOP as your partitioner.


        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try
        {
            tx.begin();
            System.out.println("Executing Query with range");

            Extent e=pm.getExtent(Product.class,true);
            Query q=pm.newQuery(e, "");
            q.setRange("1,3");
            Collection c=(Collection)q.execute();
            Iterator iter=c.iterator();
            while (iter.hasNext())
            {
                Object obj = iter.next();
                System.out.println(">  " + obj);
                 if (obj instanceof Product)
                {
                    Product b = ( Product)obj;
                    System.out.println("Product: "+b.getName());
                }
            }
            tx.commit();
        }
        finally
        {
            if (tx.isActive())
            {
                tx.rollback();
            }
            pm.close();
        }

You can also use it to delete data, replacing the execute method in the code above by:


       int numberInstancesDeleted = q.deletePersistentAll();

To retrieve entities based on a single field you can also use simple “=” queries where the used field must be an indexed column.


            ...
            Extent e=pm.getExtent(Product.class,true);
            Query q=pm.newQuery(e, "author== \" " + "Tolkien" + " \" ");
            q.setRange("1,3");
            ...

End

I hope it helped, more info at:
DataNucleus