Beauty of Graph Query (images ~ http://bitnine.net/the-needs-of-graph-db/ )

This is how we can avoid complex joins …

01_02_05_rdbms

.. by directly adding the relationship to data elements … saving many round-trips and lookups ….

01_02_05_gdbms

Simple Query 

Code –  https://github.com/data-mining/graph-query/blob/master/src/main/java/com/tinkerpop/graph/test/SimpleTitanQuery1.java

 TitanVertex marko = graph.addVertex(T.label, "user", "name", "marko", "age", 29);
 TitanVertex lop = graph.addVertex(T.label, "effect", "name", "lop", "lang", "java");

 TitanVertex josh = graph.addVertex(T.label, "user", "name", "josh", "age", 32);

 TitanVertex ripple = graph.addVertex(T.label, "effect", "name", "ripple", "lang", "java");

 TitanVertex peter = graph.addVertex(T.label, "user", "name", "peter", "age", 35);

marko.addEdge("knows", josh, "weight", 1.0f, "date", 2012);
marko.addEdge("created", lop, "weight", 0.4f, "date", 2014);
josh.addEdge("created", ripple, "weight", 1.0f, "date", 2012);
josh.addEdge("created", lop, "weight", 0.4f, "date", 2012);
peter.addEdge("created", lop, "weight", 0.2f, "date", 2014);
marko.addEdge("knows", peter, "weight", 0.2f, "date", 2013);

https://github.com/tinkerpop/gremlin/wiki/Basic-Graph-Traversals

http://www.fromdev.com/2013/09/Gremlin-Example-Query-Snippets-Graph-DB.html

> Find out the co-artists and music_albums  with rating between 0.5 and 1.5 and created on 2012

query :  g.V().outE("knows","created").has("weight",P.between(0.5,1.5)).has("date",2012).inV().values("name").fill(collection);

output :  ripple , josh

> Find out all friends of marko

query :  g.V(v_marko).out("knows").map(t -> t.get().value("name") + " is a friend of marko.").forEachRemaining(System.out::println);

output :

peter is a friend of marko.
josh is a friend of marko.

Code –  https://github.com/data-mining/graph-query/blob/master/src/main/java/com/tinkerpop/graph/test/NorthWindGraphExample.java

Dataset : https://github.com/data-mining/graph-query/blob/master/src/main/resources/northwind.json

> Find order with size > 0

query :   gst.V().has("product", "unitsOnOrder", P.neq(0)).valueMap("name", "unitsOnOrder").forEachRemaining(System.out::println)

> Find orders with price between 5 - 10

query :  gst.V().has("product", "unitPrice", P.between(5f, 10f)).valueMap("name", "unitPrice").forEachRemaining(System.out::println)

> Count the number of orders for each customer

gst.V().hasLabel("customer").match(
    __.as("c").values("customerId").as("customerId"),
    __.as("c").out("ordered").count().as("orders")
 ).select("customerId", "orders").forEachRemaining(System.out::println)

Aggregation Query

Code –  https://github.com/data-mining/graph-query/blob/master/src/main/java/com/tinkerpop/graph/test/SimpleTitanQuery2.java

> Find out the aggregations of subject matter expertise 

query : g.V().out("develops").aggregate("x").by("name").cap("x").forEachRemaining(System.out::println);

output :     {  gremlin=3, tinkergraph=2 }

Code – https://github.com/data-mining/graph-query/blob/master/src/main/java/com/tinkerpop/graph/test/AggregationQuery.java

TitanVertex u1 = graph.addVertex(T.label, "user", "name", "U1");
TitanVertex d1 = graph.addVertex(T.label, "device", "name", "D1");
u1.addEdge("connected", d1, "weight", Math.random());

TitanVertex u2 = graph.addVertex(T.label, "user", "name", "U2");
TitanVertex d2 = graph.addVertex(T.label, "device", "name", "D2");
u2.addEdge("connected", d2, "weight", Math.random());

TitanVertex u3 = graph.addVertex(T.label, "user", "name", "U3");
u3.addEdge("connected", d1, "weight", Math.random());

TitanVertex u4 = graph.addVertex(T.label, "user", "name", "U4");
u4.addEdge("connected", d1, "weight", Math.random());

TitanVertex u5 = graph.addVertex(T.label, "user", "name", "U5");
u5.addEdge("connected", d2, "weight", Math.random());

> Find groups of users per device 

query : 
g.V().has(T.label, "device").forEachRemaining(device -> {
System.out.print(device.property("name").value()+" -> ");
device.vertices(Direction.IN, "connected")
 .forEachRemaining(user ->    
    System.out.print(user.property("name").value() + " ")); 
  });

output :
D2 -> U2 U5 
D1 -> U4 U3 U1 

Code –  https://github.com/data-mining/graph-query/blob/master/src/main/java/com/tinkerpop/graph/test/NorthWindGraphExample.java

Recommend 5 products for a specific customer :

  •  determine what the customer has already ordered
  •  determine who else ordered the same products
  •  determine what others also ordered
  •  determine products which were not already ordered by the initial customer, but ordered by the others 

query : 

gst.V().has("customerId", "ALFKI").as("customer1").
// find all products orders by customer1
out("ordered").out("contains").out("is").aggregate("productsByCustomer1"). 

// for each product ,  find its customers
in("is").in("contains").in("ordered").where(P.neq("customer1")).

// for each of other customers find products not ordered by intial customer 
out("ordered").out("contains").out("is").where(P.without("productsByCustomer1")).values("name").order(Scope.local).groupCount().

Time-based Query

Code - https://github.com/data-mining/graph-query/blob/master/src/main/java/com/tinkerpop/graph/test/TimeModelQuery.java

TitanVertex u1 = graph.addVertex(T.label, "user", "name", "U1");
TitanVertex d1 = graph.addVertex(T.label, "device", "name", "D1");
u1.addEdge("connected", d1, "weight", Math.random(), "time", t1);

TitanVertex u2 = graph.addVertex(T.label, "user", "name", "U2");
TitanVertex d2 = graph.addVertex(T.label, "device", "name", "D2");
u2.addEdge("connected", d2, "weight", Math.random(), "time", t1);

TitanVertex u3 = graph.addVertex(T.label, "user", "name", "U3");
u3.addEdge("connected", d1, "weight", Math.random(), "time", t2);

TitanVertex u4 = graph.addVertex(T.label, "user", "name", "U4");
u4.addEdge("connected", d1, "weight", Math.random(), "time", t3);

TitanVertex u5 = graph.addVertex(T.label, "user", "name", "U5");
u5.addEdge("connected", d1, "weight", Math.random(), "time", t4);
u1.addEdge("connected", d2, "weight", Math.random(), "time", t2);

>   Which users connected to what devices 2 days ago 

query  :  gst.E().has("time",P.gte(twoDaysFromNow)).as("connection")
 .inV().as("device").select("connection").outV().as("user")
 .select("device","user").by("name").fill(result);

output :
{device=D1, user=U3}
{device=D1, user=U1}
{device=D2, user=U1} 
{device=D2, user=U2}

> Find the devices that users were connected between 3 days and 4 days from now ..

query :   gst.E().has("time", P.between(t4, t3)).as("connection").inV().as("device").select("connection").outV().as("user")  .select("device","user").by("name")

output :     {device=D1, user=U5}

> Show the devices which U1 was connected most-recently

query :  gst.V().has("name", "U1").outE("connected").order(Scope.local).by("time", Order.decr).inV().values("name").fill(collection);

Netwrok Simulation Query

Code :  https://github.com/data-mining/graph-query/blob/master/src/main/java/com/tinkerpop/graph/test/NetworkAnalyticsSimulator.java

—  Generates large number of dataset of following schema [ user (name,location,duration)   => connected  (@time)  =>  device (name) ]

> Find all users [Uy] and devices [Dy] for a given User Ux such that [Dy] connected to [Dx] and are within (m-1, m+1) meters , where Ux is m mtrs away from CENTRAL HUB

Lets assume USER X is m distance away from a CENTRAL HUB

Find the DEVICES [Dx] that User Ux is connected to.

Fetch all the corresponding USERS [Uy] who are connected with [Dx] and who are located within (m-1, m+1) meters

Also find all the devices [Dy] connected by users [Uy] 

query :   gst.V(userV).out("connected").in().has("distance",            P.between(min,max)).as("user").out("connected").as("device").select("user","device").by("name").toSet()

Historical Data Query

Code – https://github.com/data-mining/graph-query/blob/master/src/main/java/com/tinkerpop/graph/test/PropertyHistoryQuery.java

— Store List of Values (audit info / updates with timestamps) in a property and then query 

ToDo – not working .. someone need to take a look at the query 

More Fun with Queries 

Code – https://github.com/data-mining/graph-query/blob/master/src/main/java/com/tinkerpop/graph/test/GodsOfGraphExample.java

— Creates the Graph of Gods – Datasets 

> Which gods were involved in battle within 50 kilometers of Athens ?

query :   g.E().has("place", Geo.geoWithin(Geoshape.circle(37.97, 23.72, 50))).as("source").inV().as("God2") 

 .select("source").outV().as("God1").select("God1", "God2").by("name")

> Show Hercules is the vertex that is 2-steps away from Saturn along the in(‘father’) path

query :  g.V(saturn).repeat(__.in("father")).times(2).next()

> Find all the other gods with whom Hercules battled > 10 time

query :  g.V(hercules).outE("battled").has("time", P.gt(10)).inV().values("name")

Where do pluto’s brothers live ?

g.V(pluto).out("brother").as("God").out("lives").as("Place").select("God", "Place").by("name")

> Who are pluto’s cohabitants ?

query :   g.V(pluto).as(“V”).out(“lives”).in(“lives”).where(P.neq(“V”)).values(“name”)

> How many 3-step acyclic paths exist in the graph ?

query :  g.V().out().out().out().simplePath().count()
Advertisements