{"id":2340,"date":"2015-01-16T17:42:51","date_gmt":"2015-01-16T15:42:51","guid":{"rendered":"https:\/\/test.viaboxx.de\/2015\/01\/16\/introduction-to-the-titan-graph-database\/"},"modified":"2021-08-10T22:10:55","modified_gmt":"2021-08-10T22:10:55","slug":"introduction-to-the-titan-graph-database","status":"publish","type":"post","link":"https:\/\/www.viaboxx.de\/en\/blog\/introduction-to-the-titan-graph-database\/","title":{"rendered":"Introduction to the Titan graph database"},"content":{"rendered":"\n<p>This articles is the first articles in a series and introduces the Titan graph database as well as&nbsp;how to access it via the Gremlin console shell. Furthermore, &nbsp;a basic schema for the ESecLog domain is introduced that is going to be used in future articles.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Introduction<\/strong><\/p>\n\n\n\n<p>In the last couple of years, <a title=\"graph databases\" href=\"http:\/\/en.wikipedia.org\/wiki\/Graph_database\">graph databases<\/a> have gained a lot a popularity (or hype). A graph database focuses on entities (vertices) and their relations (edges). As an example, Wikipedia can easily be represented as a graph. In a simple approach one can model the articles as entities and hyperlinks as relationship between the articles.<\/p>\n\n\n\n<p>In our experience it depends on your domain whether a graph or relational database is better suited. A relational database is better suitable for data that can be modelled in tables while graphs are better for domains with interconnections and patterns between entities.<\/p>\n\n\n\n<p>With <a title=\"Domain Driven Design (DDD)\" href=\"http:\/\/en.wikipedia.org\/wiki\/Domain-driven_design\">Domain Driven Design(DDD)<\/a> one often already has a domain graph model that can then be transferred into a graph database.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a title=\"By yaph [CC BY-SA 2.0 (http:\/\/creativecommons.org\/licenses\/by-sa\/2.0)], via Wikimedia Commons\" href=\"http:\/\/commons.wikimedia.org\/wiki\/File%3AProgramming_Languages_Influence_Network_Graph_freebase_yaph.jpg\"><img decoding=\"async\" src=\"\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/0\/0b\/Programming_Languages_Influence_Network_Graph_freebase_yaph.jpg\/512px-Programming_Languages_Influence_Network_Graph_freebase_yaph.jpg\" alt=\"Programming Languages Influence Network Graph freebase yaph\" width=\"512\"><\/a><br>Example: A Wikimedia network graph showing the connections of programming languages.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Titan<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.viaboxx.de\/wp-content\/uploads\/2021\/08\/titan-logo-e1421422090869.png\" alt=\"\" class=\"wp-image-6969\" width=\"225\" height=\"155\"\/><\/figure>\n\n\n\n<p>In the <a title=\"ESecLog\" href=\"http:\/\/www.eseclog.de\/\">ESecLog<\/a> project we use the <a title=\"Titan\" href=\"http:\/\/thinkaurelius.github.io\/titan\/\">Titan<\/a> graph database. Titan is an open source project developed by <a title=\"Aurelius\" href=\"http:\/\/thinkaurelius.com\/\">Aurelius<\/a> with the Apache2 license. It supports different storage backends (Apache Cassandra, Apache HBase or Oracle Berkeley DB) and has a native integration into the <a title=\"Tinkerpop\" href=\"http:\/\/www.tinkerpop.com\/\">TinkerPop<\/a> technology stack.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Tinkerpop<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.viaboxx.de\/wp-content\/uploads\/2015\/01\/tinkerpop-splash.png\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"156\" src=\"http:\/\/www.viaboxx.de\/wp-content\/uploads\/2015\/01\/tinkerpop-splash-300x156.png\" alt=\"tinkerpop-splash\" class=\"wp-image-2345\" srcset=\"https:\/\/www.viaboxx.de\/wp-content\/uploads\/2015\/01\/tinkerpop-splash-300x156.png 300w, https:\/\/www.viaboxx.de\/wp-content\/uploads\/2015\/01\/tinkerpop-splash.png 422w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/figure>\n\n\n\n<p>The Tinkerpop stack is used for providing standard libraries and interfaces for different&nbsp;graph database vendors (e.g. Titan, Neo4J, OrientDB,&#8230;). The Tinkerpop stack can be best explained from bottom to top:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Blueprints:<\/strong> Collection of interfaces and implementations for graph databases, similarly to JDBC.<\/li><li><strong>Pipes<\/strong>: A dataflow processing framework<\/li><li><strong>Gremlin:<\/strong> A graph traversal language<\/li><li><strong>Frames:<\/strong> An object-graph mapper<\/li><li><strong>Furnace:<\/strong> Standard graph algorithms for undirected and directed graphs<\/li><li><strong>Rexster:<\/strong> A graph server that exposes the underlying graph via REST<\/li><\/ul>\n\n\n\n<p>Titan implements the Blueprints API and thus allows to use the complete technology stack of Tinkerpop. In this introductory post we will be using&nbsp;&nbsp;<a title=\"Gremlin\" href=\"http:\/\/gremlin.tinkerpop.com\/\">Gremlin<\/a>&nbsp;and start to define a simple database model that we will continue to use and extend in future articles.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Installation<\/strong><\/p>\n\n\n\n<p>The Titan version 0.5.3 was released in December 2014. &nbsp;Make sure that you have java installed.<\/p>\n\n\n\n<p>Download and unzip the server into a folder of your choice:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[codesyntax lang=\"bash\"]\n&lt;pre>$ wget http:\/\/s3.thinkaurelius.com\/downloads\/titan\/titan-0.5.3-hadoop2.zip\n$ unzip titan-0.5.3-hadoop2.zip\n$ cd titan-0.5.3-haddop2\n$ sudo bin\/titan.sh -c cassandra-es start\nForking Cassandra...\nRunning `nodetool statusthrift`.. OK (returned exit status 0 and printed string \"running\").\nForking Elasticsearch... \nForking Titan + Rexster...\nConnecting to Titan + Rexster (127.0.0.1:8184).... OK (connected to 127.0.0.1:8184).\nRun rexster-console.sh to connect.&lt;\/pre>\n[\/codesyntax]<\/pre>\n\n\n\n<p>Now Titan is running and you can connect either via&nbsp;Rexster or Gremlin. In this post we are going use the Gremlin connection and open a Titangraph .&nbsp;The Gremlin Shell that we are going to use is a Groovy shell, therefore you can load Java or Groovy classes into the shell and write plain&nbsp;Groovy into the shell. A good documentation for those familiar with SQL can be found on the website &nbsp;<a href=\"http:\/\/sql2gremlin.com\/\">http:\/\/sql2gremlin.com\/<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[codesyntax lang=\"groovy\"]\n&lt;pre>$ bin\/gremlin.sh\n\n\\,,,\/\n(o o)\n-----oOOo-(_)-oOOo-----\n\n\/\/ Load a new graph\ngremlin&amp;gt; g = TitanFactory.open(\"cassandra:localhost\")\n==&amp;gt;titangraph[cassandra:[localhost]]\n[\/codesyntax]<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>ESecLog<\/strong><\/p>\n\n\n\n<p>We are part of&nbsp;&nbsp;the joint research project <a title=\"ESecLog \" href=\"http:\/\/www.eseclog.de\">ESecLog<\/a> that is being funded by the Federal Ministry of Education and Research (<em>BMBF<\/em>).<\/p>\n\n\n\n<p>Security is a top priority in air freight logistics but screening procedures can be very time consuming and costly. The freight&#8217;s security status is monitored throughout the entire transport chain aggregated into a digital freight fingerprint.<\/p>\n\n\n\n<p>Lets create&nbsp;a simplified Flight cargo delivery model: &nbsp;A shipment is ordered from a customer. The shipment contains a&nbsp;&nbsp;different &nbsp;amount of pieces. The pieces are later&nbsp;placed&nbsp;onto a&nbsp;pallet and pallets can even be bundled onto other pallets.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"http:\/\/www.viaboxx.de\/wp-content\/uploads\/2015\/01\/ESeclogSimple.png\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"137\" src=\"http:\/\/www.viaboxx.de\/wp-content\/uploads\/2015\/01\/ESeclogSimple-300x137.png\" alt=\"ESeclogSimple\" class=\"wp-image-2357\" srcset=\"https:\/\/www.viaboxx.de\/wp-content\/uploads\/2015\/01\/ESeclogSimple-300x137.png 300w, https:\/\/www.viaboxx.de\/wp-content\/uploads\/2015\/01\/ESeclogSimple.png 398w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption>A simple ESecLog Domain model.<\/figcaption><\/figure><\/div>\n\n\n\n<p>In this model, every vertice has a name, and every edge has a timestamp. We have four vertex labels: Customer, Shipment, Piece and Pallet. We have three &nbsp;edge labels: &#8220;orders&#8221;, &#8220;consists-of&#8221; and &#8220;is-bundled-on&#8221;.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Create the Graph Schema<\/strong><\/p>\n\n\n\n<p>Lets create the schema and indices for the above model. In case you want to clean and clear your entire graph Titan, you can do the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">gremlin&gt; g.shutdown()\n==&gt;null\ngremlin&gt; TitanCleanup.clear(g)\n==&gt;null\ngremlin&gt; g = TitanFactory.open(\"cassandra:localhost\")\n==&gt;titangraph[cassandra:[localhost]]<\/pre>\n\n\n\n<p>Be aware that the graph needs to be shutdown before clearing it.<\/p>\n\n\n\n<p>There is a difference in if you are constructing a TinkerGraph or a TitanGraph in regards to the KeyIndex declaration.<\/p>\n\n\n\n<p class=\"has-normal-font-size\"><strong>TinkerGraph<\/strong><\/p>\n\n\n\n<p>For the TinkerGraph the ESecLog indices&nbsp;can be defined as following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">gremlin&gt; tg = new TinkerGraph()\n==&gt;tinkergraph[vertices:0 edges:0]\ngremlin&gt; tg.createKeyIndex('time', Edge.class)\n==&gt; null\ngremlin&gt; tg.createKeyIndex('name', Vertex.class)\n==&gt; null<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Adding a Vertex with name = Hobbes\ngremlin&gt; tg.addVertex(null).setProperty(\"name\",\"Hobbes\")\n==&gt;null\n\/\/ See how many vertices\/edges the graph has\ngremlin&gt; tg\n==&gt;tinkergraph[vertices:1 edges:0]\n\n\/\/ Listing all vertices (uppercase 'V')\ngremlin&gt; tg.V\n==&gt;v[0]\n\n\/\/ Query a specific vertex (lowercase 'v') by id or name\ngremlin&gt; tg.v(0)\n==&gt;v[0]\ngremlin&gt; tg.V(\"name\",\"Hobbes\")\n==&gt;v[0]\ngremlin&gt; tg.v(0).map\n==&gt;{name=Hobbes}\n\n<\/pre>\n\n\n\n<p class=\"has-normal-font-size\"><strong>Titan Graph<\/strong><\/p>\n\n\n\n<p>In a Titan 0.5.x one needs to use the TitanManagement &nbsp;for configuring the Schema and defining indices. Be aware that you need to initialize indices before creating graph object. One trick that we use to do that is by querying a graph node that is only used for storing the schema version. If that exists we do an upgrade if necessary, otherwise we initialize the schema and indices.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">g = TitanFactory.open(\"cassandra:localhost\");\nTitanManagement management = g.getManagementSystem();\n\nfinal PropertyKey name = management.makePropertyKey(\"name\").dataType(String.class).make();\nTitanGraphIndex namei = management.buildIndex(\"name\",Vertex.class).addKey(name).unique().buildCompositeIndex();\nmanagement.setConsistency(namei, ConsistencyModifier.LOCK);\n\nfinal PropertyKey time = management.makePropertyKey(\"time\").dataType(Integer.class).make();\nTitanGraphIndex timei = management.buildIndex(\"time\",Edge.class).addKey(time).buildCompositeIndex();\nmanagement.setConsistency(timei, ConsistencyModifier.LOCK);\n\nmanagement.makeVertexLabel(\"Customer\").make();\nmanagement.makeVertexLabel(\"Shipment\").make();\nmanagement.makeVertexLabel(\"Piece\").make();\nmanagement.makeVertexLabel(\"Pallet\").make();\nmanagement.makeEdgeLabel(\"orders\").make();\nmanagement.makeEdgeLabel(\"consists-of\").make();\nmanagement.makeEdgeLabel(\"is-bundled-on\").make();\n\nmanagement.commmit();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Adding a Customer Vertex with name = Hobbes\ngremlin&gt; g.addVertexWithLabel(\"Customer\").setProperty(\"name\",\"Hobbes\")\n==&gt;null\n\n\/\/ See how many vertices\/edges the graph has\ngremlin&gt; g.V.count()\n==&gt;1\n\n\/\/ Listing all vertices (uppercase 'V')\ngremlin&gt; g.V\n==&gt;v[256]\n\n\n\/\/ Query a specific vertex (lowercase 'v') by id or name\ngremlin&gt; g.v(256)\n==&gt;v[256]\ngremlin&gt; g.v(256).map\n==&gt;{name=Hobbes}\ngremlin&gt; g.v(256).label\n==&gt;Customer\ngremlin&gt; g.V(\"name\",\"Hobbes\").map\n==&gt;{name=Hobbes} \n\n\/\/ Query the vertices for all vertices with label \"Customer\"\ngremlin&gt; g.query().has(\"label\",\"Customer\").vertices()\n==&gt;v[256]<\/pre>\n\n\n\n<p>In future articles we will see how to use Gremlin to query or transform the graph as well as how to travers the graph recursively with time-frames.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This articles is the first articles in a series and introduces the Titan graph database as well as&nbsp;how to access it via the Gremlin console shell. [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"default","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[79,80,81,82,83],"class_list":["post-2340","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-eseclog","tag-graph-database","tag-nosql","tag-tinkerpop","tag-titan"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Jan Nonnen","author_link":"https:\/\/www.viaboxx.de\/en\/blog\/author\/jan-nonnenviaboxx-de\/"},"uagb_comment_info":0,"uagb_excerpt":"This articles is the first articles in a series and introduces the Titan graph database as well as&nbsp;how to access it via the Gremlin console shell. [&hellip;]","_links":{"self":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts\/2340"}],"collection":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/comments?post=2340"}],"version-history":[{"count":8,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts\/2340\/revisions"}],"predecessor-version":[{"id":6985,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts\/2340\/revisions\/6985"}],"wp:attachment":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/media?parent=2340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/categories?post=2340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/tags?post=2340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}