diff --git a/README.md b/README.md index 0940a59..a17ca88 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ unless otherwise stated. ![](blogSql.svg) -```SQL +```mysql create database jrtechs_blog; use jrtechs_blog; @@ -105,7 +105,7 @@ npm install memory-cache --save The color scheme has been changing a lot recently. -[Adobe Color Wheel](https://color.adobe.com/create/color-wheel/?copy=true&base=2&rule=Custom&selected=3&name=Copy%20of%20Site&mode=rgb&rgbvalues=0.231,0.325499999999957,0.42,0,0.7450980392156863,0.6980392156862745,0.10196078431372549,0.36470588235294116,0.38823529411764707,0.8235294117647058,0.7529411764705882,1,0.3165071770335184,0.24148325358851674,0.49&swatchOrder=0,1,2,3,4) +[Adobe Color Wheel](https://color.adobe.com/create/color-wheel/?copy=true&base=2&rule=Custom&selected=4&name=Copy%20of%20Site&mode=cmyk&rgbvalues=0.17254901960784313,0.24313725490196078,0.3137254901960784,0.28627450980392155,0.5607843137254902,0.7450980392156863,0.5329137283008958,0.7301501780381741,1,0.8235294117647058,0.7529411764705882,1,0.042420144797897574,0,0.17000000000000004&swatchOrder=0,1,2,3,4) current: top 2C3E50 diff --git a/entries/programming/gremlin-graph-database-in-10-minutes.md b/entries/programming/gremlin-in-10-minutes.md similarity index 59% rename from entries/programming/gremlin-graph-database-in-10-minutes.md rename to entries/programming/gremlin-in-10-minutes.md index 378a2a8..bb484b2 100644 --- a/entries/programming/gremlin-graph-database-in-10-minutes.md +++ b/entries/programming/gremlin-in-10-minutes.md @@ -1,23 +1,25 @@ -## Graph Data Base Basics +## What is Gremlin? + +Gremlin is a graph traversal language: think of Gremlin as the SQL for graph databases. Gremlin is not +a graph database server, it is a language; but, there is a Gremlin Server and a Gremlin Console available for +interacting with graph databases. It is possible to use Gremlin on large database platforms +like [Titan](https://www.digitalocean.com/community/tutorials/how-to-set-up-the-titan-graph-database-with-cassandra-and-elasticsearch-on-ubuntu-16-04) + and [HBase](https://docs.janusgraph.org/latest/hbase.html). -A graph database is based on graph structures. A graph is composed of nodes, edges, and properties. A key -object/component in a graph database is stored as a node. Nodes are connected together via edges representing -relationships. For example, you may represent people as nodes and have edges representing who people are friends -with. You can assign properties to both nodes and edges. A person (node) may have the properties of age and name, -where a friendship (edge) may have a start date property. -#### Why Graph Databases? +## Graph Data Base Basics -Graph databases are great for modeling data where the value lies in the shape of the graph, or, it would be difficult -to model in a traditional table based database. +A graph database is based on graph theory. A graph is composed of nodes, edges, and properties. A key +object/component in a graph database is stored as a node. Nodes are connected via edges representing +relationships. For example, you may represent people as nodes and have edges representing friendships. +You can assign properties to both nodes and edges. A person (node) may have the properties of age and name, +where a friendship (edge) may have a start date property. -#### What is Gremlin? +## Why Graph Databases? -Gremlin is a graph traversal language; think of Gremlin as SQL but for graph databases. Gremlin is not -a graph database server, it is a language; but, there is a Gremlin Server and Gremlin Console available for -interacting with graph databases using Gremlin. It is possible to use Gremlin on large database platforms -like [Titan](https://www.digitalocean.com/community/tutorials/how-to-set-up-the-titan-graph-database-with-cassandra-and-elasticsearch-on-ubuntu-16-04) - and [HBase](https://docs.janusgraph.org/latest/hbase.html). +Graph databases are great for modeling data where the value lies in the shape of the graph. Graph databases +also allow to to model more complex relationships which would be difficult to model in a normal table-based +database. ## Gremlin Installation @@ -25,17 +27,19 @@ Download and extract the following: - [Gremlin Console](https://www.apache.org/dyn/closer.lua/tinkerpop/3.3.3/apache-tinkerpop-gremlin-console-3.3.3-bin.zip) - [Gremlin Server](https://www.apache.org/dyn/closer.lua/tinkerpop/3.3.3/apache-tinkerpop-gremlin-server-3.3.3-bin.zip) -Start the Gremlin server by running it with the start script in the bin folder. +Start the Gremlin server by running it with the start script in the bin folder. As a prerequisite for running gremlin, you +must have Java installed on your computer. ``` ./gremlin-server.sh ``` -Start the Gremlin console by running the gremlin.sh or gremlin.bat script in the bin folder of the apache-tinkerpop folder. +Start the Gremlin console by running the gremlin.sh or gremlin.bat script in the bin folder. ``` ./gremlin.sh ``` -Now you need to instantiate a new graph on the server to use. To to that, execute the following commands. -```gremlin +Now you need to instantiate a new graph on the server to use. To to that, execute the following commands in +the Gremlin console. +```java #Creates a empty graph gremlin> graph = EmptyGraph.instance() ==>emptygraph[empty] @@ -54,80 +58,81 @@ gremlin> g = graph.traversal().withRemote(DriverRemoteConnection.using(cluster, Now that you have your gremlin server and console set up, you are ready to start executing Gremlin queries. -#### Add a Vertex +### Adding a Vertex -For Gremlin, nodes are referred to as "Vertexes". To add a node/vertex to the graph, you simply use the -command addV('node label') on your graph traversal source. For consistency, most people and documentation -use "g" as their default graph traversal source. To append properties to your your node, you string a series of -.property('property_name', 'property_value') to the queries. +In Gremlin nodes are referred to as "Vertexes". To add a node/vertex to the graph, you simply use the +command addV() on your graph traversal source. For consistency, most people +use "g" as their default graph traversal source. To append properties to your your vertex, you add a series of +".property('property_name', 'property_value')" strings to the add vertex query. -ex: -```gremlin +EX: +```java g.addV('student').property('name', 'Jeffery').property('GPA', 4.0); ``` -#### Update a Property +### Updating a Property -Unlike SQL, you are not limited to a specific schema for a graph database. If you want to add or change -a property of a vertex or edge, you simply call its .property('property_name', 'property_value'). -The g.V(1) in the example refers to a specific node with the primary id of 1, these ids are auto assigned by the graph database. -You can replace g.V(1) with a command to select a specific node. +Unlike SQL, you are not limited to a specific schema in a graph database. If you want to add or change +a property on a vertex or edge, you simply use the property command again. +The "g.V(1)" in the following example refers to a specific vertex with the primary id of 1-- the graph database auto assigns these ids. +You can replace "g.V(1)" with a command to select a specific vertex or edge. -```gremlin +```java g.V(1).property('name', 'Jeffery R'); ``` -#### Selection +### Selection -Selecting nodes and edges is the most complicated part of Gremlin. The concept is not particularly hard but, there -are dozens of ways to do traversals and selections. I will cover the most common and helpful ways to traverse a -graph. +Selecting nodes and edges is the most complicated part of Gremlin. The concept is not particularly hard, but, there +are dozens of ways to do graph traversals and selections. I will cover the most common aways to traverse a graph. -This example will select all vertexes which have the label "student". The .valueMap() appended to the end means -that it will returns a map of all the properties of the nodes it returns. -```gremlin +This example will select all vertexes which have the label "student". The ".valueMap()" command appended to the end of the query +makes Gremlin return a map of all the objects it returns with their properties. +```java g.V().hasLabel('student').valueMap(); ``` -In this example instead of returning a ValueMap of values, we are just returning the names of the students +In this following example, instead of returning a ValueMap of values, we are just returning the names of the students in the graph. -```gremlin +```java g.V().hasLabel('student').values('name'); ``` This example will return the GPA of the student with the name "Jeffery R". -```gremlin +```java g.V().hasLabel('student').has('name', 'Jeffery R').values('gpa'); ``` -This command will all the students in order of their GPA. -```gremlin +This command will return all the students in order of their GPA. +```java g.V().hasLabel('student').order().by('gpa', decr).value('name') ``` -#### Adding Edges +### Adding Edges -If you want to add a edge (relationship/connection) between two nodes, the easiest way (my opinion) to do it in Gremlin is by -using something called aliasing. In this example we select two nodes and give them a name, in this case it is "a", and "b". -After we have selected two edges, we can add an edge to them using the addE('Relation_Name') command. The syntax of this is -nice because we know that "a" is friends with "b"-- it is easy to tell the direction +The easiest way (my opinion) to add edges in Gremlin is by +using aliasing. In this example we select two nodes and assign them a name: in this case it is "a", and "b". +After we have selected two edges, we can add an edge to them using the "addE()" command. The syntax of this is +nice because we know that "a" is friends with "b"-- it is easy to tell the direction of the edge. -```gremlin +```java g.V(0).as('a').V(1).as('b').addE('knows') .from('a').to('b'); ``` -## Using Gremlin With Java +## Using Gremlin with Java -Now that you know the syntax of Gremlin, you are ready to use it somewhere other than just the Gremlin console. If you -are trying to use Gremlin with Java, there is a nice Maven dependency for TinkerPop and Gremlin. If you want to quickly -connect to your server with Java, make sure your server is set up exactly as it was before this tutorial started discussing -Gremlin Syntax. -```maven +Now that you know the basic syntax of Gremlin, you are ready to use it somewhere other than the Gremlin console. If you +are trying to use Gremlin with Java, there is a great Maven dependency for TinkerPop and Gremlin. If you want to quickly +connect to your Gremlin server with Java, make sure your server is set up exactly as it was before this tutorial started discussing +Gremlin syntax. + +#### Maven dependency for Java: +```html com.tinkerpop @@ -150,7 +155,7 @@ Gremlin Syntax. ``` It is helpful to wrap everything relating to the graph database connection into a single Java class. This is roughly -the code that I usually use to interact with a Gremlin Server, anybody is free to use it. +the code that I usually use to interact with a Gremlin Server-- anybody is free to use it. ```java public class GraphConnection @@ -182,7 +187,7 @@ public class GraphConnection } ``` -ex GraphConnection Usage: +#### Basic GraphConnection.java Usage: ```java RemoteConnection con = new RemoteConnection() String query = "g.V().hasLabel('player')" + @@ -193,14 +198,13 @@ String query = "g.V().hasLabel('player')" + ".as('p2')" + ".addE('friends')" + ".from('p1').to('p2')"; -//System.out.println(query); this.con.queryGraph(query); ``` -Overly complex usage with lambda example. +#### Overly complex usage with a lambda statement ```java /** - * Fetches a list of friends from the graph database + * Fetches the list of a player's friends. * * @param id steam id * @return list of friends @@ -225,11 +229,11 @@ private List getFriendsFromGraph(String id) ``` The most important thing to do while playing around with Gremlin in Java is to keep an eye on the -return type. From experience, I can say that it is often easier to return the node/edges from your -query rather than doing a valueMap. +return type. From experience, I can say that it is often easier to return the vertex from your +query rather than returning the valueMap. -Without adding valueMap()/values() to the end of the query, you now can directly access the vertex or edge in the result rather than -doing some voodoo witchcraft and casting between ArrayLists and HashMaps. +Without returning the valueMap in the query, you can directly access the vertex +in the result rather than doing some voodoo witchcraft and casting between ArrayLists and HashMaps. The previous example could be re-written as this: ```java @@ -242,16 +246,15 @@ String query = "g.V().hasLabel('player')" + for(Result r: this.con.queryGraph(query)) { friends.add(new Player(r.getVertex("name").value().toString), - r.getVertex("id").value().toString),) + r.getVertex("id").value().toString)); } ``` -Now you know enough to be dangerous with Gremlin. Yay! If you want to do more than basic things with Gremlin, -I highly suggest that you take a look at the tutorial [SQL 2 Gremlin](http://sql2gremlin.com/). -If you plan on deploying this to production, it is recommended to use HBase with JanusGraph for a persistent back end storage +You now know enough about Gremlin to be dangerous with it. Yay! If you want to do more than basic things with Gremlin, +I highly suggest that you look at the tutorial [SQL 2 Gremlin](http://sql2gremlin.com/). +If you plan on deploying this to production, it is recommended that you use HBase for a persistent back end storage server. - ## Resources - [SQL 2 Gremlin](http://sql2gremlin.com/) diff --git a/img/posts/gremlinConsole.png b/img/posts/gremlinConsole.png new file mode 100644 index 0000000..325844f Binary files /dev/null and b/img/posts/gremlinConsole.png differ