? QA Design Gurus: Implement OrientDB using java

Jul 1, 2015

Implement OrientDB using java

OrientDB is an open source NoSQL database management system written in Java. It is a multi-model database, supporting graph, document, key/value, and object models, but the relationships are managed as in graph databases with direct connections between records.
Here, we are going to discuss more on Document Model of orient database and its implementation using Java.
The data in this model is stored inside documents. A document is a set of key/value pairs (also referred to as fields or properties) where a key allows access to its value. Values can hold primitive data types, embedded documents, or arrays of other values. Documents are not typically forced to have a schema which can be a benefit because they remain flexible and easy to modify. Documents are stored in collections enabling developers to group data as they decide.
OrientDB uses the concepts of "classes" and "clusters" instead of "collections" for grouping documents. OrientDB's Document model adds the concept of a "LINK" as a relationship between documents. With OrientDB you can decide whether to embed documents or link to them directly. When you fetch the document all the links are automatically resolved by OrientDB. This is the most important difference with any other Document Database like MongoDB.

The table below illustrates the comparison between the relational model, the document model, and the OrientDB document model:
Relational Model
Document Model
OrientDB Document Model
Table
Collection
Class or Cluster
Row
Document
Document
Column
Key/value pair
Document field
Relationship
not available
Link


We need to import below packages required to access orient database using java.
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;

Get ready to access the database, for which we need to establish connection to database and open it.
ODatabaseDocumentTx db = new ODatabaseDocumentTx("remote:hostname/databasename");      

Below lines help us to create a class (Table in RDBMS), insert data and save to Orient DB from java.
//Create Class
OClass class1 = db.getMetadata().getSchema().createClass("ClassName");
class1.createProperty("Field1", OType.INTEGER);
class1.createProperty("Field2", OType.STRING);

//Insert Results
ODocument d1 = new ODocument("ClassName");
d1.field("Field1", 1);
d1.field("Field1", "test");
d1.save();
db.close();

To establish relation between the classes, Orientdb provides the concept of links. These links refresh at every fetch and provide appropriate data (Similar to join of a table in RDBMS) Below code does that.

db.command(new OCommandSQL("create link EmployeeSalary type linkset from Employee.EmpId to Salary.EmpId inverse")).execute();
My Favorite OrientDB Features
Here are some of my favourite features in OrientDB. These are not competitive advantages to OrientDB. It’s just some of the things that make me happy when coding against an embeddable database.

  1. I can create the database in code.
  2. I don’t have to use SQL for querying, but most of the time, i do. I already know SQL, and it’s just easy for me.
  3. I use the document database, and it’s very pleasant inserting new documents in Java.
  4. I can store multi-megabyte binary objects directly in the database.
  5. Speed. For me, OrientDB is very fast, and in the few yearsIi’ve been using it, it’s become faster. 

·      

No comments:

Post a Comment