Skip to content

Instantly share code, notes, and snippets.

@biniama
Last active September 16, 2017 10:00
Show Gist options
  • Select an option

  • Save biniama/f654a15f05359ff2c61a0069893eef3c to your computer and use it in GitHub Desktop.

Select an option

Save biniama/f654a15f05359ff2c61a0069893eef3c to your computer and use it in GitHub Desktop.
GraphDatabase - Titan with AWS DynamoDB
#A simple Gremlin query that finds restaurants based on a type of cuisine is shown below:
gremlin> g.V.has('genreId', 'Pizzeria').in.restaurant_name
==>La Fontana Pizza Restaurante and Cafe
==>Dominos Pizza
==>Little Cesarz
==>pizza clasica
==>Restaurante Tiberius
# A simple recommendation system
g.V.has('userId','U1101').out('friend').outE('visit').has('visit_food', T.gte, 1).as('x').inV.as('y').out('restaurant_genre').has('genreId', 'Seafood').back('x').transform{e, m -> [food: m.x.visit_food, name:m.y.restaurant_name]}.groupCount{it.name}.cap
==>{Restaurante y Pescaderia Tampico=1, Restaurante Marisco Sam=1, Mariscos El Pescador=2}
package com.equb.aws;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Iterator;
import com.thinkaurelius.titan.core.Multiplicity;
import com.thinkaurelius.titan.core.PropertyKey;
import com.thinkaurelius.titan.core.TitanGraph;
import com.thinkaurelius.titan.core.TitanVertex;
import com.thinkaurelius.titan.core.attribute.Geoshape;
import com.thinkaurelius.titan.core.schema.TitanManagement;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
public class RestaurantFactory {
public static final String RESTAURANT_ID = "RestaurantId";
public static final String USER_ID = "userId";
public static final String GENRE = "genreId";
public static final String PROP_RESTAURANT_NAME = "restaurant_name";
public static final String PROP_VISIT_RATING = "visit_rating";
public static final String PROP_VISIT_FOOD = "visit_food";
public static void load(final TitanGraph graph) throws Exception {
// set up the structure of our graph
TitanManagement mgmt = graph.getManagementSystem();
if (mgmt.getGraphIndex(RESTAURANT_ID) == null) {
final PropertyKey RESTAURANTIdKey = mgmt.makePropertyKey(RESTAURANT_ID).dataType(Integer.class).make();
mgmt.buildIndex(RESTAURANT_ID, Vertex.class).addKey(RESTAURANTIdKey).unique().buildCompositeIndex();
mgmt.makePropertyKey(PROP_RESTAURANT_NAME).dataType(String.class).make();
}
if (mgmt.getGraphIndex(USER_ID) == null) {
final PropertyKey userKey = mgmt.makePropertyKey(USER_ID).dataType(String.class).make();
mgmt.buildIndex(USER_ID, Vertex.class).addKey(userKey).unique().buildCompositeIndex();
}
if (mgmt.getGraphIndex(GENRE) == null) {
final PropertyKey genreKey = mgmt.makePropertyKey(GENRE).dataType(String.class).make();
mgmt.buildIndex(GENRE, Vertex.class).addKey(genreKey).unique().buildCompositeIndex();
}
mgmt.makeVertexLabel("restaurant").make();
mgmt.makeVertexLabel("genre").make();
mgmt.makeVertexLabel("user").make();
mgmt.makeEdgeLabel("visit").multiplicity(Multiplicity.MULTI).make();
mgmt.makePropertyKey(PROP_VISIT_RATING).dataType(Integer.class).make();
mgmt.makePropertyKey(PROP_VISIT_FOOD).dataType(Integer.class).make();
mgmt.makeEdgeLabel("restaurant_genre").multiplicity(Multiplicity.MULTI).make();
mgmt.makeEdgeLabel("friend").multiplicity(Multiplicity.MULTI).make();
mgmt.commit();
// load the restaurants
TitanVertex vertex = graph.addVertexWithLabel("restaurant");
vertex.addProperty(RESTAURANT_ID, nextLine[0]);
vertex.addProperty(PROP_RESTAURANT_NAME, nextLine[1]);
// load the genres (types of cuisine)
TitanVertex vertex = graph.addVertexWithLabel("genre");
vertex.addProperty(GENRE, nextLine[0]);
// load the customers (users)
TitanVertex vertex = graph.addVertexWithLabel("user");
vertex.addProperty(USER_ID, nextLine[0]);
// load some visits (from user to restaurant)
Vertex userVertex = get(graph, USER_ID, nextLine[0]);
Vertex RestaurantVertex = get(graph, RESTAURANT_ID, nextLine[1]);
Edge visitEdge = userVertex.addEdge("visit", RestaurantVertex);
visitEdge.setProperty(PROP_VISIT_RATING, nextLine[2]);
visitEdge.setProperty(PROP_VISIT_FOOD, nextLine[3]);
// load the genre for each restaurant
Vertex RESTAURANTVertex = get(graph, RESTAURANT_ID, nextLine[0]);
Vertex genreVertex = get(graph, GENRE, nextLine[1]);
if (RESTAURANTVertex != null && genreVertex != null) {
RESTAURANTVertex.addEdge("restaurant_genre", genreVertex);
}
// load the friends
Vertex userVertex = get(graph, USER_ID, nextLine[0]);
Vertex friendVertex = get(graph, USER_ID, nextLine[2]);
if (userVertex != null && friendVertex != null) {
userVertex.addEdge("friend", friendVertex);
}
}
// helper function to get a vertex based on a property
private static Vertex get(TitanGraph graph, String key, String value) {
Iterator<Vertex> it = graph.getVertices(key, value).iterator();
Vertex vertex = null;
if (it.hasNext()) {
vertex = it.next();
}
return vertex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment