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;

}

}

Orient DB query to find first level friend


Problem:
How to find first level friend in my network in orient db

Solution:
select flatten(distinct(in)) as in from (traverse V.out, E.in from #10:1178 while $depth <=1)
    where @class='Friends'

Where #10:1178 is the record id of the User.

How to find 2nd level of friends of a user in orientDb

Problem:
How to find 2nd level of friends of a user in orientDb (Friends of Friend)

Solution
select flatten(distinct(in.out.in)) as in from (traverse V.out, E.in from #10:1178 while $depth <=1)
    where @class='Friends'

do not buy domain from godaddy.in ,instead by from godaddy.com

How I purchase p1bugs.com.I searched for p1bugs.com on godaddy.in ,it show my $9 price.When i logged in to purchase
It shows my different prince $12.I was surprised.Because i have all other domain purchased from godaddy,
i have not option.I clicked on buy button.It show me minimum purchase of Rs 4600 is allowed.I was shocked and stuck.
What to do ,why pay 4600 for a 600 rupee domain.I took a break and tried again on godaddy.com there it showd me for $10.
I logged and purchase it from godaddy.com in just 600 rupee.
So Always careful when buying domain from godaddy.
Delete cookie before buying anything from online.Online sites do behavior targeting and show different price based upon your behavior on Internet.

which is the best webhosting for drupal website

Choosing the right webhosting for your website depend of your website requirement.
If you want to setup simple website with vanila drupal and your website does not have much lets
say 100 pay day.Then go for Justhost or bluehost.Which are very cheap your can try hostgator also.
Just host provide you a SSH access,which is beneficail to people comformable with linux commant.
But if you have a large website you can go for linode or e2network in India.Both are good.
Only thing with VPS is you need to manage everything your self .Setting apache and mysql or installing JAVA.
But both are value for money for large website and you can move to different plan as you site load grows.

How to create custom vertex class property and index in orientdb

Go to orientdb bin folder,Run console.sh

Connecting To database
connect remote:localhost/harvest_dev admin admin

Create Class
create class User extends V;
CREATE PROPERTY User.fid STRING;
CREATE PROPERTY User.lid STRING;
CREATE PROPERTY User.fbn STRING;
CREATE PROPERTY User.au STRING;
CREATE INDEX lidx ON User (lid) notunique;
CREATE INDEX fidx ON User (fid) notunique;
Create Class
create class Company extends V
CREATE PROPERTY Company.fcid STRING
CREATE PROPERTY Company.fcn STRING
CREATE PROPERTY Company.cn STRING
CREATE PROPERTY Company.cid STRING
CREATE INDEX fb_cmp_id_inx ON Company (fcid) notunique
CREATE INDEX cidx ON Company (cid) notunique

How to get edge between to vertex in orientdb

There are 2 ways to get Edge between the edges.

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

But it some times return false even if edges exists between v1 and v2.

Instead Use below code to get Edges.

String query="select from (traverse V.out, E.in from "+node1.getIdentity()+" while $depth <=1) "+
                      "where @class='Friends' and in=?";
            OSQLSynchQuery<ODocument> query1 = new OSQLSynchQuery<ODocument>(query);
            Iterable<ODocument> result = graph.command(query1).execute(node2.getIdentity());
            if (null != result && result.iterator().hasNext()) {
                connAdded = true;
            }