Thursday 28 January 2016

OrientDB graph hello world example

Below is the code which explain how to create vertex and edges.
In example given below i have create vertex v1 and v2 .

Then i have created friends edge to create friends relationship.








import java.util.Iterator;

import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.index.OIndexException;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;

public class OrientDbGraphHello {
// static ODatabaseDocumentTx db =
// ODatabaseDocumentPool.global().acquire("remote:localhost/temp","admin",
// "admin");
static OrientGraph graph = new OrientGraph(
"remote:localhost/harvest_migration", "admin", "admin");

static {
// OGraphDatabase rawGraph = (OGraphDatabase) graph.getRawGraph();
// OGraphDatabase rawGraph=new
// OGraphDatabase("remote:localhost/harvest_migration");
// rawGraph.open("admin", "admin");
// System.out.println(rawGraph);
// No need to write transaction logs
OGlobalConfiguration.TX_USE_LOG.setValue(false);
// Keep connection open
OGlobalConfiguration.STORAGE_KEEP_OPEN.setValue(Boolean.TRUE);
// graph.setUseLightweightEdges(false);
// graph.setUseClassForEdgeLabel(false);
// graph.setUseClassForVertexLabel(false);
// graph.setUseVertexFieldsForEdgeLabels(false);

}

/**
* @param args
*/
public static void main(String[] args) {
createVertex();
//getData();
if (graph != null) {
graph.shutdown();
}
}

private static void getData() {
Iterable<ODocument> vertexes = graph
.command(
new OCommandSQL(
"select in.fid as fid from (traverse V.out, E.in from #11:0 while $depth <=1) where @class='Friends'"))
.execute();
Iterator<ODocument> it = vertexes.iterator();
while (it.hasNext()) {
ODocument oDocument = it.next();
System.out.println(oDocument.field("fid"));
}
}

private static Vertex getVertex(String field) {
Iterable<Vertex> iterable = graph.getVertices("User.fid", field);
Iterator<Vertex> iterator = iterable.iterator();
if (iterator.hasNext()) {
return iterator.next();
}
return null;
}

private static void createVertex() {
OrientVertex v1 = (OrientVertex) getVertex("fid1");
if (v1 == null) {
v1 = (OrientVertex) addUserVertex("v3", "class:Person");
}
OrientVertex v2 = (OrientVertex)getVertex("v4");
if (v2 == null) {
v2 = (OrientVertex)addUserVertex("v4", "class:Person");
}

Iterable<Edge> edges = v1.getEdges( v2, Direction.BOTH, "Friends" );

if(!edges.iterator().hasNext()){
v1.addEdge("Friends", v2);
v2.addEdge("Friends", v1);
}
System.out.println(v1);
}

public static Vertex addUserVertex(String name, String vertexName) {
try {

Vertex v = graph.addVertex(vertexName);
v.setProperty("fid", name);
v.setProperty("lid", "lid");

return v;
} catch (OIndexException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;

}

}

No comments:

Post a Comment