介绍
Titan是一个高可扩展的开源图形数据库。图形数据库是NoSQL数据库的一种类型,其中所有数据都被存储为 nodes 和 edges。图形数据库适用于使用高度连接的数据的应用程序,数据之间的关系是应用程序功能的重要组成部分,如社交网络网站。Titan用于存储和查询大量数据,这些数据被分布在多个机器中。它可以配置为使用各种可用的存储后台,如Apache Cassandra,HBase和BerkeleyDB。这使得未来更容易避免供应商锁定,如果您需要更改数据存储。
在本教程中,您将安装Titan 1.0. 然后,您将配置Titan以使用Cassandra和ElasticSearch,这两者都与Titan结合在一起。Cassandra作为持有底层数据的数据库,而ElasticSearch,一个自由文本搜索引擎,可以用来在数据库中进行一些复杂的搜索操作。
前提条件
要完成本教程,您将需要:
- 一个 Ubuntu 16.04 服务器,至少有 2 GB 的 RAM,具有非根用户和防火墙. 您可以通过遵循 Ubuntu 16.04 初始服务器设置来设置此设置。
步骤 1 – 下载,卸载和启动 Titan
要下载 Titan 数据库,请转到 他们的下载页面。 您将看到两个可下载的 Titan 发行版. 对于本教程,我们想要 Titan 1.0.0 with Hadoop 1. 这是稳定版本。 将其下载到您的服务器上用 wget
:
1wget http://s3.thinkaurelius.com/downloads/titan/titan-1.0.0-hadoop1.zip
一旦下载完成,解包 zip 文件. 解包文件的程序没有默认安装. 首先安装:
1sudo apt-get install unzip
下一篇:Titan Unzip:
1unzip titan-1.0.0-hadoop1.zip
这将创建一个名为titan-1.0.0-hadoop
的目录。
让我们开始Titan,以确保一切正常工作. 切换到‘titan-1.0.0-hadoop’目录,并调用壳脚本来启动Titan。
1cd titan-1.0.0-hadoop1
2./bin/titan.sh start
您将看到类似于此的输出:
1[secondary_label Output]
2Forking Cassandra...
3Running `nodetool statusthrift`... OK (returned exit status 0 and printed string "running").
4Forking Elasticsearch...
5Connecting to Elasticsearch (127.0.0.1:9300)...... OK (connected to 127.0.0.1:9300).
6Forking Gremlin-Server...
7Connecting to Gremlin-Server (127.0.0.1:8182)...... OK (connected to 127.0.0.1:8182).
8Run gremlin.sh to connect.
因此,每次启动 Titan 时,Cassandra、ElasticSearch 和 Gremlin-Server 也随之启动。
您可以通过运行以下命令来检查Titan的状态。
1./bin/titan.sh status
你会看到这个输出:
1[secondary_label Output]
2Gremlin-Server (org.apache.tinkerpop.gremlin.server.GremlinServer) is running with pid 7490
3Cassandra (org.apache.cassandra.service.CassandraDaemon) is running with pid 7077
4Elasticsearch (org.elasticsearch.bootstrap.Elasticsearch) is running with pid 7358
在下一步中,您将看到如何查询图表。
步骤 2 — 使用 Gremlin 查询图表
Gremlin是一个 Graph Traversal Language 用于查询、分析和操纵 Graph 数据库. 现在 Titan 已设置并启动,您将使用 Gremlin 创建和查询来自 Titan 的节点和边缘。
要使用 Gremlin,请通过发出以下命令打开 Gremlin 控制台。
1./bin/gremlin.sh
你会看到类似于此的答案:
1[secondary_label Output]
2 \,,,/
3 (o o)
4-----oOOo-(3)-oOOo-----
5plugin activated: tinkerpop.server
6plugin activated: tinkerpop.hadoop
7plugin activated: tinkerpop.utilities
8plugin activated: aurelius.titan
9plugin activated: tinkerpop.tinkergraph
10gremlin>
Gremlin 控制台加载了几个插件,以支持 Titan 和 Gremlin 特定的功能。
首先,实例图表对象. 此对象代表我们目前正在处理的图表. 它有几种方法可以帮助管理图表,例如添加顶点,创建标签和处理交易。
1graph = TitanFactory.open('conf/titan-cassandra-es.properties')
你会看到这个输出:
1[secondary_label Output]
2==>standardtitangraph[cassandrathrift:[127.0.0.1]]
输出指定了通过TitanFactory.open()
方法返回的对象类型,即standardtitangraph
。它还表示图表使用的存储后端(cassandrathrift
)以及通过 localhost(127.0.0.1
)连接的对象。
使用打开()
方法创建一个新的 Titan 图表,或打开一个现有的图表,使用指定的属性文件中存在的配置选项。配置文件包含高级别的配置选项,如使用哪个存储后端,缓存后端和其他几种选项。
执行命令后,图形对象被实例化并存储在图形
变量中. 要查看图形对象的所有可用属性和方法,键入图形
,然后是TAB
键:
1gremlin> graph.
2addVertex( assignID( buildTransaction() close()
3closeTransaction( commit( compute( compute()
4configuration() containsEdgeLabel( containsPropertyKey( containsRelationType(
5containsVertexLabel( edgeMultiQuery( edgeQuery( edges(
6features() getEdgeLabel( getOrCreateEdgeLabel( getOrCreatePropertyKey(
7...
8...
在图表数据库中,您主要通过 traversing来查询数据,而不是像在关系数据库中一样以合并和索引来查找记录。
1g = graph.traversal()
您使用这个g
变量执行跨行。让我们使用该变量创建几个顶部。顶部就像SQL中的行。每个顶部都有一个顶部类型或标签
及其相关属性,类似于SQL中的字段。
1sammy = g.addV(label, 'fish', 'name', 'Sammy', 'residence', 'The Deep Blue Sea').next()
2company = g.addV(label, 'company', 'name', 'DigitalOcean', 'website', 'www.digitalocean.com').next()
在本示例中,我们创建了两个标签为鱼
和公司
的顶部,我们还定义了两个属性,即第一个顶部的名称
和居所
,第二个顶部的名称
和网站
。
例如,要列出第一个 vertex 的所有属性,请执行以下命令:
1g.V(sammy).properties()
输出将看起来像这样:
1[secondary_label Output]
2==>vp[name->Sammy]
3==>vp[residence->The Deep Blue Sea]
您也可以将新属性添加到顶部. 让我们添加一个颜色:
1g.V(sammy).property('color', 'blue')
现在,让我们定义这两个顶点之间的关系,这是通过在它们之间创造一个边缘
来实现的。
1company.addEdge('hasMascot', sammy, 'status', 'high')
这使得sammy
与hasMascot
标签的公司
以及具有高
值的status
的属性之间形成了边缘。
现在,让我们来看看公司的面具:
1g.V(company).out('hasMascot')
这将从公司
顶部返回出发的顶部,并将它们之间的边缘标记为hasMascot
。
1g.V(sammy).in('hasMascot')
要了解更多信息,请参阅描述性 Apache Tinkerpop3 文档。
通过按CTRL+C
离开Gremlin控制台。
现在让我们为Titan添加一些自定义配置选项。
步骤 3 – 配置泰坦
让我们创建一个新的配置文件,您可以使用它来定义Titan的所有自定义配置选项。
Titan有一个可插入的存储层;而不是处理数据存储本身,Titan使用另一个数据库来处理它。Titan目前为存储数据库提供了三个选项:Cassandra,HBase和BerkeleyDB. 在本教程中,我们将使用Cassandra作为存储引擎,因为它高度可扩展和可用性高。
首先,创建配置文件:
1nano conf/gremlin-server/custom-titan-config.properties
添加这些行来定义存储后端是什么以及它在哪里可用。存储后端设置为cassandrathrift
,这表示我们正在使用Cassandra用于存储与Cassandra的 thrift接口:
1[label conf/gremlin-server/custom-titan-config.properties]
2storage.backend=cassandrathrift
3storage.hostname=localhost
然后添加这三个行来定义使用哪个搜索后端. 我们将使用elasticsearch
作为搜索后端。
1[label conf/gremlin-server/custom-titan-config.properties]
2...
3index.search.backend=elasticsearch
4index.search.hostname=localhost
5index.search.elasticsearch.client-only=true
第三行表示ElasticSearch是一个薄型客户端,不会存储任何数据,而将其设置为假
会创建一个常规的ElasticSearch集群节点,可以存储数据,我们现在不需要。
最后,添加这个行来告诉Gremlin Server它将服务的图表类型。
1[label conf/gremlin-server/custom-titan-config.properties]
2...
3gremlin.graph=com.thinkaurelius.titan.core.TitanFactory
在conf
目录中有许多示例配置文件,您可以参阅。
保存文件并离开编辑器。
我们需要将这个新的配置文件添加到Gremlin Server中,打开Gremlin Server的配置文件。
1nano conf/gremlin-server/gremlin-server.yaml
导航到图表
部分,并找到这个行:
1[label conf/gremlin-server/gremlin-server.yaml]
2..
3 graph: conf/gremlin-server/titan-berkeleyje-server.properties}
4..
把它替换成这个:
1[label conf/gremlin-server/gremlin-server.yaml]
2..
3 graph: conf/gremlin-server/custom-titan-config.properties}
4..
保存和退出文件。
现在重新启动Titan,停止Titan并重新启动。
1./bin/titan.sh stop
2./bin/titan.sh start
现在我们有一个自定义配置,让我们配置Titan作为一个服务。
步骤 4 – 使用 Systemd 管理 Titan
如果我们的服务器意外重新启动或由于任何原因不得不重新启动,我们希望Titan也启动。
为了配置,我们将为Titan创建一个Systemd单元文件,以便我们可以管理它。
首先,我们为我们的应用程序在 /etc/systemd/system
目录中创建一个文件,其中包含一个 .service
扩展:
1sudo nano /etc/systemd/system/titan.service
单元文件由部分组成,该[单元]
部分规定了我们服务的元数据和依赖性,包括我们服务的描述以及何时开始我们的服务。
将此配置添加到文件中:
1[label /etc/systemd/system/titan.service]
2[Unit]
3Description=The Titan database
4After=network.target
我们指定该服务应该在实现网络目标后启动,换句话说,我们只在网络服务准备后启动该服务。
在[单位]
部分之后,我们定义了[服务]
部分,我们指定如何启动服务。
1[label /etc/systemd/system/titan.service]
2[Service]
3User=sammy
4Group=www-data
5Type=forking
6Environment="PATH=/home/sammy/titan-1.0.0-hadoop1/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
7WorkingDirectory=/home/sammy/titan-1.0.0-hadoop1/
8ExecStart=/home/sammy/titan-1.0.0-hadoop1/bin/titan.sh start
9ExecStop=/home/sammy/titan-1.0.0-hadoop1/bin/titan.sh stop
首先,我们定义了服务运行的用户和组,然后我们定义了它将是哪种类型的服务。 该类型被假定是简单
的默认。 由于我们使用的启动脚本启动Titan启动了其他儿童程序,我们将服务类型指定为forking
。
然后我们指定PATH
环境变量,Titan的工作目录和执行命令来启动Titan,我们将启动Titan命令分配给ExecStart
变量。
ExecStop
变量定义了该服务应该如何停止。
最后,我们添加了[安装]
部分,它看起来像这样:
1[Install]
2[label /etc/systemd/system/titan.service]
3WantedBy=multi-user.target
安装
部分允许您启用和禁用服务. WantedBy
指令在/etc/systemd/system
目录中创建了一个名为multi-user.target
的目录。
保存文件,关闭编辑器,并启动新服务:
1sudo systemctl start titan
然后启用此服务,以便每次服务器启动时,Titan启动:
1sudo systemctl enable titan
您可以通过以下命令检查Titan的状态:
1sudo systemctl status titan
要了解有关单元文件的更多信息,请参阅理解系统单元和单元文件
(https://andsky.com/tech/tutorials/understanding-systemd-units-and-unit-files)。
结论
您现在已经在您的服务器上安装了基本的Titan设置,如果你想更深入地了解Titan的架构,请不要犹豫,查看他们的官方文档(http://s3.thinkaurelius.com/docs/titan/1.0.0/)。
现在你已经设置了Titan,你应该通过查看官方文件(http://tinkerpop.apache.org/docs/current/reference/)来了解更多关于Tinkerpop3和Gremlin的信息。