1. 准备
下载v0.5的版本 下载地址-GitHub
提醒:下载最好使用迅雷等其他支持断点下载的工具,因为资源在国外,网速很低,使用浏览器下载,很容易出现下载错误或超时,而导致下载失败。
- 这里我下在的是 v0.5.2
janusgraph-0.5.2.zip
janusgraph-full-0.5.2.zip
是包含janusgraph.sh
和 检查工具 等脚本,cassandra数据库,ES的包 - 后端索引:自安装的ES
- 后端存储:JG自带的BerkeleyDB
BerkeleyDB 数据存储在
$JG_HOME/db/berkeley/
目录下
JG Server运行时,日志存储在$JG_HOME/logs
,pid存放在$JG_HOME/run
2. 安装部署
JanusGraph 提供了docker 容器安装,因为很慢不推荐。这里使用本地安装,直接解压即可。
[root@s03 ~]# unzip janusgraph-0.5.2.zip -d /opt/
···
[root@s03 janusgraph-0.5.2]# cd /opt/janusgraph-0.5.2/
[root@s03 janusgraph-0.5.2]# ls
bin conf data examples ext javadocs lib LICENSE.txt NOTICE.txt scripts
重点看下 bin 和 conf 目录结构
[root@s03 janusgraph-0.5.2]# tree bin/
bin/
├── gremlin.bat
├── gremlin-server.bat
├── gremlin-server.sh
└── gremlin.sh
0 directories, 4 files
[root@s03 janusgraph-0.5.2]# tree conf/
conf/
├── gremlin-server
│ ├── gremlin-server-berkeleyje-es.yaml
│ ├── gremlin-server-berkeleyje.yaml
│ ├── gremlin-server-configuration.yaml
│ ├── gremlin-server-cql-es.yaml
│ ├── gremlin-server.yaml
│ ├── janusgraph-berkeleyje-es-server.properties
│ ├── janusgraph-berkeleyje-server.properties
│ ├── janusgraph-cassandra-es-server.properties
│ ├── janusgraph-cql-es-server.properties
│ └── log4j-server.properties
├── hadoop-graph
│ ├── hadoop-graphson.properties
│ ├── hadoop-gryo.properties
│ ├── hadoop-load.properties
│ ├── hadoop-script.properties
│ ├── read-cassandra.properties
│ ├── read-cassandra-standalone-cluster.properties
│ ├── read-cql.properties
│ ├── read-cql-standalone-cluster.properties
│ ├── read-hbase.properties
│ ├── read-hbase-snapshot.properties
│ └── read-hbase-standalone-cluster.properties
├── janusgraph-berkeleyje-es.properties
├── janusgraph-berkeleyje-lucene.properties
├── janusgraph-berkeleyje.properties
├── janusgraph-berkeleyje-solr.properties
├── janusgraph-cassandra-configurationgraph.properties
├── janusgraph-cassandra-es.properties
├── janusgraph-cassandra.properties
├── janusgraph-cassandra-solr.properties
├── janusgraph-cql-configurationgraph.properties
├── janusgraph-cql-es.properties
├── janusgraph-cql.properties
├── janusgraph-cql-solr.properties
├── janusgraph-hbase-es.properties
├── janusgraph-hbase.properties
├── janusgraph-hbase-solr.properties
├── janusgraph-inmemory.properties
├── log4j-console.properties
├── logback.xml
├── remote-graph.properties
├── remote-objects.yaml
├── remote.yaml
└── solr
├── currency.xml
├── lang
│ └── stopwords_en.txt
├── protwords.txt
├── schema.xml
├── solrconfig.xml
├── stopwords.txt
└── synonyms.txt
4 directories, 49 files
3. 启动运行
JanusGraph 本身就是一组没有执行的jar包,并且提供了开箱即用的配置。提供两种运行模式:
- 控制台模式:Gremlin Console,用于本地快速测试等
- 服务器模式:Gremlin Server,JG作为一个服务器独立运行,对外提供服务
3.1. 运行Gremlin控制台
$ ./bin/gremlin.sh
3.2. 运行Gremlin服务器
运行服务执行 gremlin-server.sh
命令即可。使用start,采用默认配置启动(默认配置 conf/gremlin-server/gremlin-server.yaml),也可以指定配置启动:
$ ./bin/gremlin-server.sh start
# or
$ ./bin/gremlin-server.sh ./conf/gremlin-server/[...].yaml
默认配置(gremlin-server.yaml)使用的内存存储,而不是专门的数据库。默认情况下未使用检索,因为未指定索引后端,所以不支持混合索引(如果不使用索引,载入时使用
GraphOfXxxxFactory.loadWithoutMixedIndex(graph, true)
而不是GraphOfXxxxFactory.load(graph)
)。可以看下
janusgraph.sh
源码,它的启动就比gremlin-server.sh
相对复杂些。
3.2.1. 我这里使用的JG+BerkeleyDB+ES启动server
先看下 vim conf/gremlin-server/gremlin-server-berkeleyje-es.yaml
这个配置:
host: 0.0.0.0 # 实例主机IP
port: 8182 # 端口号
scriptEvaluationTimeout: 30000
channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer # 信道
graphs: { # 图集,可以配置多个
graph: conf/gremlin-server/janusgraph-berkeleyje-es-server.properties # 配置一个叫 graph 的图,使用后面的配置
}
修改配置 vim conf/gremlin-server/janusgraph-berkeleyje-es-server.properties
gremlin.graph=org.janusgraph.core.JanusGraphFactory # 指明gremlin服务器使用的图工厂实现
storage.backend=berkeleyje # 后端存储BerkeleyDB
storage.directory=db/berkeley # 后端存储数据目录
index.search.backend=elasticsearch # 后端索引ES
index.search.hostname=192.168.200.101,192.168.200.102,192.168.200.103 # 后端索引主机
启动Server
$ ./bin/gremlin-server.sh conf/gremlin-server/gremlin-server-berkeleyje-es.yaml
3.3. C/S连接
Gremlin 服务器启动后,将准备好侦听WebSocket连接。默认端口时 8182
(注意防火墙,开放此端口)。
使用 :remote
命令完成客户端(Gremlin Console)与服务器(Gremlin Server)远程连接:
# 正常连接
gremlin> :remote connect tinkerpop.server conf/remote.yaml
==>Configured localhost/127.0.0.1:8182
# 防火墙未开放端口
gremlin> :remote connect tinkerpop.server conf/remote.yaml
11:09:56 WARN org.apache.tinkerpop.gremlin.driver.Host - Marking Host{address=s02/192.168.200.102:8182, hostUri=ws://s02:8182/gremlin} as unavailable. Trying to reconnect.
==>Configured s02/192.168.200.102:8182
remote.yaml 配置文件:
hosts: [localhost] # 可修改为远程Gremlin Serve的IP
port: 8182
serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { serializeResultToString: true }}
连接成功后,可将客户端所有查询重定向到该服务器。这时输入命令还是在本地执行,要发送远程执行,需要使用 :>
符号修饰命令:
gremlin> :> 100-10
gremlin> :> g.V().values('name')
如果所用命令都要发送到远程执行,可以使用 :remote console
,这样就不需要每条命令前面都加 :>
。默认情况下,远程连接是无 session 的,在控制台中发送的每一行命令都被解释为一次请求,可以使用分号在一行上发送多个语句。像下面这样:
gremlin> :remote console # 第一次,开启远程脚本执行
==>All scripts will now be sent to Gremlin Server - [s02/192.168.200.102:8182] - type ':remote console' to return to local mode
gremlin>
gremlin> x=1
==>1
gremlin> y=2
==>2
gremlin> x+y # 无session状态
No such property: x for class: Script9
Type ':help' or ':h' for help.
Display stack trace? [yN]
gremlin> x=1;y=2;x+y # 一行多条命令
==>3
gremlin> :remote console # 第二次,退出远程脚本执行
==>All scripts will now be evaluated locally - type ':remote console' to return to remote mode for Gremlin Server - [s02/192.168.200.102:8182]
关闭远程连接:
gremlin> :remote close
==>Removed - Gremlin Server - [localhost/127.0.0.1:8182]-[···]
创建连接时通过指定 session 来建立带有会话的控制台
gremlin> :remote connect tinkerpop.server conf/remote.yaml session
==>Configured s02/192.168.200.102:8182-[6d44fcde-3d07-484c-8589-5cb9811b7a4d]
gremlin> :remote console
==>All scripts will now be sent to Gremlin Server - [s02/192.168.200.102:8182]-[6d44fcde-3d07-484c-8589-5cb9811b7a4d] - type ':remote console' to return to local mode
gremlin> x = 1
==>1
gremlin> y=2
==>2
gremlin> x+y
==>3
4. JanusGraph 服务器
4.1. 连接端点
JG Server 支持 WebSocket 或/和 HTTP 三种连接方式。在 3.2.1. 章节配置中我们已经看到,可通过 channelizer
配置服务支持的通信方式:
channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer
channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer
channelizer: org.apache.tinkerpop.gremlin.server.channel.WsAndHttpChannelizer
4.2. 安全认证
4.2.1. HTTP 认证
在 gremlin-server-xxx.yaml
中配置:
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()"}' -u user:password
4.2.2. WebSocket 认证
使用 SASL 身份验证,在 gremlin-server-xxx.yaml
中配置:
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
}
}
4.2.3. HTTP 和 WebSocket 认证
使用 HMAC token 认证方式,在 gremlin-server-xxx.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
}
}
网友评论