Although OrientDB can work in schema-less mode, sometimes you need to enforce your data model using a schema. OrientDB supports schema-full or schema-hybrid solutions where the second one means to set such constraints only for certain fields and leave the user to add custom fields to the records. This mode is at class level, so you can have the "Employee" class as schema-full and "EmployeeInformation" class as schema-less.
NOTE: Changes to the schema are not transactional, so execute them outside a transaction.
For a tutorial look at the following links:
A Class, or type, is a concept taken from the Object Oriented paradigm. In OrientDB defines a type of record. It's the closest concept to a Relational DBMS Table. Class can be schema-less, schema-full or mixed. A class can inherit from another shaping a tree of classes. Inheritance means that the sub-class extends the parent one inheriting all the attributes as they was own.
A class must have at least one cluster defined (as its default cluster), but can support multiple ones. In this case By default OrientDB will write new records in the default cluster, but reads will always involve all the defined clusters. When you create a new class by default a new physical cluster is created with the same name of the class in lower-case.
The Graph structure is based on two classes: "V" for Vertices and "E" for Edges. These class are automatically built once a database is built using the mode "graph". If you don't have these classes just create them (see below).
You can build a graph using V and E instances but it's strongly suggested to use custom types for vertices and edges.
To create a custom Vertex class (or type) use the createVertexType(<name>)
:
OrientGraph graph = new OrientGraph("local:/temp/db");
OrientVertexType account = graph.createVertexType("Account");
To create a vertex of type "Account" pass a string with the format "class:<name>"
as vertex id:
Vertex v = graph.addVertex("class:Account");
Since classes are polymorphic if you look for generic Vertices also "Account" instances are returned:
Iterable<Vertex> allVertices = graph.getVertices();
To retrieve only the vertices of "Account" class:
Iterable<Vertex> accountVertices = graph.getVerticesOfClass("Account");
In Blueprints Edges has the concept of "label" to distinguish between edge types. In OrientDB we binds the concept of Edge label to Edge class. To create an Edge custom type use the similar method createEdgeType(<name>)
:
OrientGraph graph = new OrientGraph("local:/temp/db");
OrientVertexType accountVertex = graph.createVertexType("Account");
OrientVertexType addressVertex = graph.createVertexType("Address");
// CREATE THE EDGE TYPE
OrientEdgeType livesEdge = graph.createEdgeType("Lives");
Vertex account = graph.addVertex("class:Account");
Vertex address = graph.addVertex("class:Address");
// CREATE THE EDGE
Edge e = account.addEdge("Lives", address);
To retrieve such custom classes use the methods graph.getVertexType(<name>)
and graph.getEdgeType(<name>)
. Example:
OrientVertexType accountVertex = graph.getVertexType("Account");
OrientEdgeType livesEdge = graph.getEdgeType("Lives");
To drop a persistent class use the dropVertexType(<name>)
and dropVertexType(<name>)
methods.
graph.dropVertexType("Address");
graph.dropEdgeType("Lives");
Properties are the fields of the class. In this guide Property is synonym of Field.
Once the class has been created, you can define fields (properties). Below an example:
OrientVertexType accountVertex = graph.getVertexType("Account");
accountVertex.createProperty("id", OType.INTEGER);
accountVertex.createProperty("birthDate", OType.DATE);
Please note that each field must belong to one of [Types supported types].
To drop a persistent class property use the OClass.dropProperty(String)
method.
accountVertex.dropProperty("name");
The dropped property will not be removed from records unless you explicitly delete them using the [SQLUpdate SQL UPDATE + REMOVE statement]. Example:
accountVertex.dropProperty("name");
database.command(new OCommandSQL("UPDATE Account REMOVE name")).execute();
Constraints with distributed databases could cause problems because some operations are executed at 2 steps: create + update. For example in some circumstance edges could be first created, then updated, but constraints like MANDATORY and NOTNULL against fields would fail at the first step making the creation of edges not possible on distributed mode. |
OrientDB supports a number of constrains for each field:
setMin()
setMax()
setMandatory()
setReadonly()
setNotNull()
setOrdered()
Example:
profile.createProperty("nick", OType.STRING).setMin("3").setMax("30").setMandatory(true).setNotNull(true);
profile.createIndex("nickIdx", OClass.INDEX_TYPE.UNIQUE, "nick"); // Creates unique constraint
profile.createProperty("name", OType.STRING).setMin("3").setMax("30");
profile.createProperty("surname", OType.STRING).setMin("3").setMax("30");
profile.createProperty("registeredOn", OType.DATE).setMin("2010-01-01 00:00:00");
profile.createProperty("lastAccessOn", OType.DATE).setMin("2010-01-01 00:00:00");
To let to a property value to be UNIQUE use the UNIQUE index as constraint by passing a Parameter object with key "type":
graph.createKeyIndex("id", Vertex.class, new Parameter("type", "UNIQUE"));
This constraint will be applied to all the Vertex and sub-types instances. To specify an index against a custom type use the additional parameter "class":
graph.createKeyIndex("name", Vertex.class, new Parameter("class", "Member"));
You can also have both UNIQUE index against custom types:
graph.createKeyIndex("id", Vertex.class, new Parameter("type", "UNIQUE"), new Parameter("class", "Member"));
To get a vertex or an edge by key prefix the class name to the field. For the example above use "Member.name" in place of only "name" to use the index created against the field "name" of class "Member":
for( Vertex v : graph.getVertices("Member.name", "Jay") ) {
System.out.println("Found vertex: " + v );
}
If the class name is not passed, then "V" is taken for vertices and "E" for edges:
graph.getVertices("name", "Jay");
graph.getEdges("age", 20);
For more information about indexes look at Index guide.
(Go back to Graph-Database-Tinkerpop)