JanusGraph Server

JanusGraph Server

JanusGraph使用Gremlin Server引擎作为服务器组件来处理和回答客户机查询。当封装在JanusGraph中时,Gremlin服务器称为JanusGraph服务器。

JanusGraph服务器必须手动启动才能使用它。JanusGraph服务器提供了一种方法,可以针对其中托管的一个或多个JanusGraph实例远程执行Gremlin遍历。本节将描述如何使用WebSocket配置,以及如何配置JanusGraph服务器来处理客户端的HTTP请求。有关如何从不同语言连接到JanusGraph服务器的信息,请参阅连接到Connecting to JanusGraph

入门

使用预打包发行版

注意:从0.5.1开始,它只是包含在完整包版本中。

JanusGraph预打包发行版预先配置为使用Cassandra和Elasticsearch配置来运行JanusGraph服务器。此配置默认为客户端应用程序,这些应用程序可以通过带有自定义子协议的WebSocket连接到JanusGraph服务器。有许多用不同语言开发的客户端可以帮助支持该子协议。使用Websocket界面最熟悉的客户端是Gremlin Console。快速入门版本包并不能代表生产安装,而是提供一种使用Janusgraph进行开发,运行测试以及查看组件如何连接在一起的方法。要使用此默认配置步骤如下:

  • Releases page下载最新的版本包文件janusgraph-$VERSION.zip。
  • 解压janusgraph-$VERSION,并进入解压文件夹中。
  • 运行bin/janusgraph.sh start这一步将启动Gremlin服务器,并将Cassandra/ES分成一个单独的进程。请注意:出于安全原因请使用非root用户启动ES和janusgraph.sh。
$ bin/janusgraph.sh start
Forking Cassandra...
Running `nodetool statusthrift`.. OK (returned exit status 0 and printed string "running").
Forking Elasticsearch...
Connecting to Elasticsearch (127.0.0.1:9300)... OK (connected to 127.0.0.1:9300).
Forking Gremlin-Server...
Connecting to Gremlin-Server (127.0.0.1:8182)... OK (connected to 127.0.0.1:8182).
Run gremlin.sh to connect.

连接到Gremlin服务器

运行janusgraph.sh之后,Gremlin服务器开始侦听WebSocket连接。测试连接的最简单方法是使用Gremlin Console。

运行bin/gremlin.sh启动Gremlin Console,使用:remote和:>命令向Gremlin服务器发出Gremlin请求。

$  bin/gremlin.sh
         \,,,/
         (o o)
-----oOOo-(3)-oOOo-----
plugin activated: tinkerpop.server
plugin activated: tinkerpop.hadoop
plugin activated: tinkerpop.utilities
plugin activated: janusgraph.imports
plugin activated: tinkerpop.tinkergraph
gremlin> :remote connect tinkerpop.server conf/remote.yaml
==>Connected - localhost/127.0.0.1:8182
gremlin> :> graph.addVertex("name", "stephen")
==>v[256]
gremlin> :> g.V().values('name')
==>stephen

The :remote command tells the console to configure a remote connection to Gremlin Server using the conf/remote.yaml file to connect. That file points to a Gremlin Server instance running on localhost. The :> is the "submit" command which sends the Gremlin on that line to the currently active remote. Instead of prefixing every single script with :>, the command :remote console can be executed once to implicitly send all subsequent scripts to the current remote connection. By default remote connections are sessionless, meaning that each line sent in the console is interpreted as a single request. Multiple statements can be sent on a single line using a semicolon as the delimiter.

gremlin> :remote connect tinkerpop.server conf/remote.yaml
==>Configured localhost/127.0.0.1:8182
gremlin> :remote console
==>All scripts will now be sent to Gremlin Server - [localhost/127.0.0.1:8182] - type ':remote console' to return to local mode
gremlin> graph
==>standardjanusgraph[cql:[127.0.0.1]]
gremlin> g
==>graphtraversalsource[standardjanusgraph[cql:[127.0.0.1]], standard]
gremlin> g.V()
gremlin> user = "Chris"
==>Chris
gremlin> graph.addVertex("name", user)
No such property: user for class: Script21
Type ':help' or ':h' for help.
Display stack trace? [yN]

或者,您可以通过在创建连接时建立带Session的console。在同一个session里面的console建立的变量可以重用。如下所示:

gremlin> :remote connect tinkerpop.server conf/remote.yaml session
==>Configured localhost/127.0.0.1:8182-[9acf239e-a3ed-4301-b33f-55c911e04052]
gremlin> :remote console
==>All scripts will now be sent to Gremlin Server - [localhost/127.0.0.1:8182]-[9acf239e-a3ed-4301-b33f-55c911e04052] - type ':remote console' to return to local mode
gremlin> g.V()
gremlin> user = "Chris"
==>Chris
gremlin> user
==>Chris
gremlin> graph.addVertex("name", user)
==>v[4344]
gremlin> g.V().values('name')
==>Chris

预包装配送后清理????

如果要重新启动并删除数据库和日志,可以使用./bin/janusgraph.sh clean命令。在运行清理操作之前,应停止服务器。

$ cd /Path/to/janusgraph/janusgraph-{project.version}/
$ ./bin/janusgraph.sh stop
Killing Gremlin-Server (pid 91505)...
Killing Elasticsearch (pid 91402)...
Killing Cassandra (pid 91219)...
$ ./bin/janusgraph.sh clean
Are you sure you want to delete all stored data and logs? [y/N] y
Deleted data in /Path/to/janusgraph/janusgraph-{project.version}/db
Deleted logs in /Path/to/janusgraph/janusgraph-{project.version}/log

JanusGraph Server提供WebSocket服务

入门中描述的默认配置已经是WebSocket配置。如果要更改默认配置以使用Cassandra或HBase环境,请执行以下步骤:

JanusGraph Server提供WebSocket服务配置

1. 首先测试本地到JanusGraph数据库的连接。可以通过Gremlin Console或者应用程序测试连接。对环境中./conf目录下的properties配置文件适当的修改。例如:编辑./conf/janusgraph-hbase.properties配置文件以确保storage.backend, storage.hostname and storage.hbase.table等配置参数的正确性。有关更多存储后端的配置,请参照Storage Backends。确保属性文件包含以下行:

gremlin.graph=org.janusgraph.core.JanusGraphFactory

2. 一旦本地配置文件测试通过后,将属性文件从路径./conf拷贝到路径./conf/gremlin-server

cp conf/janusgraph-hbase.properties
conf/gremlin-server/socket-janusgraph-hbase-server.properties

3. 拷贝./conf/gremlin-server/gremlin-server.yaml文件成为一个新文件socket-gremlin-server.yaml。如果需要引用文件的原始版本,请按照此操作执行。

cp conf/gremlin-server/gremlin-server.yaml
conf/gremlin-server/socket-gremlin-server.yaml

4. 按照如下示例对http-gremlin-server.yaml文件进行修改编辑。

a. 如果计划从本地主机以外的其他设备连接到JanusGraph服务器,将host更新为IP:

host: 10.10.10.100

b. 将channelizer更新为HttpChannelizer:

channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer

c. 更新graphs部分以指向新的属性文件,以便JanusGraph服务器可以找到并连接到JanusGraph实例:

    graphs: { graph:
      conf/gremlin-server/http-janusgraph-hbase-server.properties}

5. 启动Janusgraph并指定刚才配置的文件:

bin/gremlin-server.sh ./conf/gremlin-server/http-gremlin-server.yaml

6. JanusGraph服务器现在应该在HTTP模式下运行,并且可以进行测试。curl可用于验证服务器是否正常工作:

      curl -XPOST -Hcontent-type:application/json -d *{"gremlin":"g.V().count()"}* [IP for JanusGraph server host](http://):8182 

JanusGraph Server同时提供WebSocket和HTTP服务配置

0.2.0版本开始JanusGraph支持在同一个端口同时接收WebSocket和HTTP两种服务。通过在gremlin-server.yaml配置文件中修改如下配置:

channelizer: org.apache.tinkerpop.gremlin.server.channel.WsAndHttpChannelizer

JanusGraph Server高级配置

对HTTP服务配置身份认证

重要:在以下的所有示例中,身份验证的用户名和密码数据存储的DB与操作的graph所在DB是分开的。

HTTP Basic 认证配置

如下所示对gremlin-server.yaml文件进行配置修改从而开启HTTP Basic 认证:

authentication: {
   authenticator: org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.JanusGraphSimpleAuthenticator,
   authenticationHandler: org.apache.tinkerpop.gremlin.server.handler.HttpBasicAuthenticationHandler,
   config: {
     defaultUsername: user,
     defaultPassword: password,
     credentialsDb: conf/janusgraph-credentials-server.properties
    }
 }

首先通过以下不带身份认证信息的命令去请求:

curl -v -XPOST http://localhost:8182 -d '{"gremlin": "g.V().count()"}'

执行结果返回认证未通过的401错误状态码,下面我们再使用带有正确认证信息的命令去请求:

curl -v -XPOST http://localhost:8182 -d '{"gremlin": "g.V().count()"}' -u user:password

执行结果返回正确的200状态码和返回结果4。

对WebSocket服务配置身份认证

通过对gremlin-server.yaml配置文件进行如下修改来对WebSocket服务开启SASL认证:

authentication: {
  authenticator: org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.JanusGraphSimpleAuthenticator,
  authenticationHandler: org.apache.tinkerpop.gremlin.server.handler.SaslAuthenticationHandler,
  config: {
    defaultUsername: user,
    defaultPassword: password,
    credentialsDb: conf/janusgraph-credentials-server.properties
  }
}

如果你直接通过Gremlin Console去访问带有SASL认证的Janusgraph Server,在yaml配置文件里面配上正确的username和password,如下所示:

username: user
password: password

对WebSocket和HTTP服务同时配置身份认证

通过对channelizer绑定SaslAndHMACAuthenticator配置,就可以同时为Websocket开启SASL认证和HTTP开启basic以及HMAC两种方式的认证。具体gremlin-server.yaml配置文件修改如下:

authentication: {
  authenticator: org.janusgraph.graphdb.tinkerpop.gremlin.server.auth.SaslAndHMACAuthenticator,
  authenticationHandler: org.janusgraph.graphdb.tinkerpop.gremlin.server.handler.SaslAndHMACAuthenticationHandler,
  config: {
    defaultUsername: user,
    defaultPassword: password,
    hmacSecret: secret,
    credentialsDb: conf/janusgraph-credentials-server.properties
  }
}

下面示例利用HMAC认证和HTTP协议请求Janusgraph Server,首先利用username访问/session

获取一个有限时间内(有效时常通过authentication.config中的tokenTimeout参数来设定)有效的token,如下所示

 

curl http://localhost:8182/session -XGET -u user:password

{"token": "dXNlcjoxNTA5NTQ2NjI0NDUzOkhrclhYaGhRVG9KTnVSRXJ5U2VpdndhalJRcVBtWEpSMzh5WldqRTM4MW89"}

然后携带这个token去请求Janusgraph Server,如下所示:

curl -v http://localhost:8182/session -XPOST -d '{"gremlin": "g.V().count()"}' -H "Authorization: Token dXNlcjoxNTA5NTQ2NjI0NDUzOkhrclhYaGhRVG9KTnVSRXJ5U2VpdndhalJRcVBtWEpSMzh5WldqRTM4MW89"

使用TinkerPop Gremlin Server来配置和实例化JanusGraph Server

建议使用Janusgraph版本包启动JanusGraph Server,对此部分这里不做翻译。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值