linux系统安装janusGraph

1.首先安装jdk-1.8

Java Downloads | Oracle

 安装解压到/usr/local 目录下

tar -zxvf  jdk-8u311-linux-x64.tar.gz  -C /usr/local

重命名  jdk-8u311-linux-x64 为java   

mv  jdk-8u311-linux-x64 java

配置环境变量:

export JAVA_HOME=/usr/local/java
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

2.Elasticsearch7.14.0安装以及使用

安装步骤

Elasticsearch中文学习文档

虽然janusGraph中自带Elasticsearch,但是我这里自己安装了一个Elasticsearch7.14.0.

Download and install archive for Linux

The Linux archive for Elasticsearch v7.14.2 can be downloaded and installed as follows:

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.14.0-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.14.0-linux-x86_64.tar.gz.sha512
shasum -a 512 -c elasticsearch-7.14.0-linux-x86_64.tar.gz.sha512 
tar -xzf elasticsearch-7.14.0-linux-x86_64.tar.gz
cd elasticsearch-7.14.0/ 

Compares the SHA of the downloaded .tar.gz archive and the published checksum, which should output elasticsearch-{version}-linux-x86_64.tar.gz: OK.

This directory is known as $ES_HOME.

Running Elasticsearch from the command line

Elasticsearch can be started from the command line as follows:

./bin/elasticsearch

最后确认一下elasticsearch是否启动成功:

curl -X GET "localhost:9200/?pretty"

不出意外的话会返回如下结果:

peter@peter-B365M-D3V:~$ curl -X GET "localhost:9200/?pretty"
{
  "name" : "peter-B365M-D3V",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "Rq7gOSwWSWG0Mc1THx3d0g",
  "version" : {
    "number" : "7.14.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1",
    "build_date" : "2021-07-29T20:49:32.864135063Z",
    "build_snapshot" : false,
    "lucene_version" : "8.9.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

接下来就是启动Janusgraph.

3.janusGraph安装使用

janusGraph有个很有名的图数据案例,叫做"The Graph of the Gods",也称罗马诸神。JanusGraphFactory提供了静态的方法来加载图数据。默认情况下,JanusGraph使用Berkeley DB作为后端存储引擎,使用Elasticsearch作为索引引擎。

janusGraph0.6版本对应环境

  • Apache Cassandra 3.0.14, 3.11.10
  • Apache HBase 1.6.0, 2.2.7
  • Google Bigtable 1.3.0, 1.4.0, 1.5.0, 1.6.0, 1.7.0, 1.8.0, 1.9.0, 1.10.0, 1.11.0, 1.14.0
  • Oracle BerkeleyJE 7.5.11
  • Elasticsearch 6.0.1, 6.6.0, 7.14.0
  • Apache Lucene 8.9.0
  • Apache Solr 7.7.2, 8.9.0
  • Apache TinkerPop 3.5.1
  • Java 1.8

遇到的问题:我从官网下载了官网下载地址,这里官网推荐的是https://github.com/JanusGraph/janusgraph/releases/download/v0.6.0/janusgraph-full-0.6.0.zip

这个版本,正常情况下直接unzip janusgraph--full-0.6.0.zip就可以但是,我的老报错,解压不了,最后在window本地解压完后上传到linux系统,用的filezilla这个软件上传的。

将诸神图加载到 JanusGraph 中

linux系统安装janusGraph_第1张图片

视觉符号 意义
加粗键 图形索引键
带星号的粗体键 必须具有唯一值的图形索引键
下划线键 以顶点为中心的索引键
空头边缘 功能性/独特的优势(无重复)
尾交叉边缘 单向边(只能在一个方向上遍历)

将诸神图加载到 JanusGraph 中

下面的示例将打开一个 JanusGraph 图形实例并加载上图所示的 Gods数据集的图形。JanusGraphFactory 提供了一组静态open方法,每个方法都以一个配置作为参数并返回一个图实例。本教程演示使用GraphOfTheGodsFactory具有不同配置的帮助器类加载The Graph of the Gods。本节跳过配置详细信息,但有关存储后端、索引后端及其配置的其他信息可在存储后端、索引后端和 配置参考中找到。

使用索引后端加载

下面的示例open在使用BerkeleyDB存储后端和 Elasticsearch索引后端的配置上调用这些方法之一:

gremlin> graph = JanusGraphFactory.open('conf/janusgraph-berkeleyje-es.properties')
==>standardjanusgraph[berkeleyje:../db/berkeley]
gremlin> GraphOfTheGodsFactory.load(graph)
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[standardjanusgraph[berkeleyje:../db/berkeley], standard]

这些JanusGraphFactory.open() and GraphOfTheGodsFactory.load()方法在返回之前对新构造的图执行以下操作:

  1. 在图形上创建全局索引和以顶点为中心的索引的集合。
  2. 将所有顶点及其属性添加到图中。
  3. 将所有边及其属性添加到图中。

演示:

eter@peter-B365M-D3V:~/janusgraph-full-0.6.0/janusgraph-full-0.6.0$ ls
bin  cassandra  conf  data  elasticsearch  examples  ext  javadocs  lib  LICENSE.txt  logs  NOTICE.txt  scripts
peter@peter-B365M-D3V:~/janusgraph-full-0.6.0/janusgraph-full-0.6.0$ cd bin
peter@peter-B365M-D3V:~/janusgraph-full-0.6.0/janusgraph-full-0.6.0/bin$ ls
checkconfig.sh  checksocket.sh  gremlin.bat  gremlin-server.bat  gremlin.sh  janusgraph-server.sh  janusgraph.sh
peter@peter-B365M-D3V:~/janusgraph-full-0.6.0/janusgraph-full-0.6.0/bin$ ./gremlin.sh
-bash: ./gremlin.sh: 权限不够
peter@peter-B365M-D3V:~/janusgraph-full-0.6.0/janusgraph-full-0.6.0/bin$ ./gremlin.sh
-bash: ./gremlin.sh: 权限不够
peter@peter-B365M-D3V:~/janusgraph-full-0.6.0/janusgraph-full-0.6.0/bin$ chmod u+x *.sh
peter@peter-B365M-D3V:~/janusgraph-full-0.6.0/janusgraph-full-0.6.0/bin$ ./gremlin.sh

         \,,,/
         (o o)
-----oOOo-(3)-oOOo-----
WARN [org.codehaus.groovy.tools.shell.util.PackageHelperImpl] Error opening jar file : '/home/peter/janusgraph-full-0.6.0/janusgraph-full-0.6.0/lib/hadoop-yarn-client-2.8.5.jar' : java.util.zip.ZipException: error in opening zip file
WARN [org.codehaus.groovy.tools.shell.util.PackageHelperImpl] Error opening jar file : '/home/peter/janusgraph-full-0.6.0/janusgraph-full-0.6.0/lib/hbase-shaded-client-2.2.7.jar' : java.util.zip.ZipException: error in opening zip file
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/peter/janusgraph-full-0.6.0/janusgraph-full-0.6.0/lib/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/peter/janusgraph-full-0.6.0/janusgraph-full-0.6.0/lib/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
plugin activated: tinkerpop.server
plugin activated: tinkerpop.tinkergraph
13:55:27 WARN  org.apache.hadoop.util.NativeCodeLoader  - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
plugin activated: tinkerpop.hadoop
plugin activated: tinkerpop.spark
plugin activated: tinkerpop.utilities
plugin activated: janusgraph.imports
gremlin> graph = JanusGraphFactory.open('conf/janusgraph-berkeleyje-es.properties')
14:50:48 WARN  org.elasticsearch.client.RestClient  - request [GET http://127.0.0.1:9200/] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:50:48 WARN  org.elasticsearch.client.RestClient  - request [GET http://127.0.0.1:9200/_cluster/health?timeout=30s&wait_for_status=yellow] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:50:48 WARN  org.elasticsearch.client.RestClient  - request [PUT http://127.0.0.1:9200/_cluster/settings] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:50:48 WARN  org.elasticsearch.client.RestClient  - request [GET http://127.0.0.1:9200/_scripts/janusgraph-add] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:50:48 WARN  org.elasticsearch.client.RestClient  - request [POST http://127.0.0.1:9200/_scripts/janusgraph-add] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:50:48 WARN  org.elasticsearch.client.RestClient  - request [GET http://127.0.0.1:9200/_scripts/janusgraph-del] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:50:49 WARN  org.elasticsearch.client.RestClient  - request [POST http://127.0.0.1:9200/_scripts/janusgraph-del] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
==>standardjanusgraph[berkeleyje:/home/peter/janusgraph-full-0.6.0/janusgraph-full-0.6.0/conf/../db/berkeley]
gremlin> GraphOfTheGodsFactory.load(graph)
14:52:14 WARN  org.elasticsearch.client.RestClient  - request [HEAD http://127.0.0.1:9200/janusgraph_vertices] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:15 WARN  org.elasticsearch.client.RestClient  - request [PUT http://127.0.0.1:9200/janusgraph_vertices] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:15 WARN  org.elasticsearch.client.RestClient  - request [PUT http://127.0.0.1:9200/janusgraph_vertices/_settings] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:15 WARN  org.elasticsearch.client.RestClient  - request [HEAD http://127.0.0.1:9200/janusgraph_vertices] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:15 WARN  org.elasticsearch.client.RestClient  - request [POST http://127.0.0.1:9200/_aliases] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:15 WARN  org.elasticsearch.client.RestClient  - request [PUT http://127.0.0.1:9200/janusgraph_vertices/_mapping] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:16 WARN  org.elasticsearch.client.RestClient  - request [HEAD http://127.0.0.1:9200/janusgraph_edges] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:17 WARN  org.elasticsearch.client.RestClient  - request [PUT http://127.0.0.1:9200/janusgraph_edges] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:17 WARN  org.elasticsearch.client.RestClient  - request [PUT http://127.0.0.1:9200/janusgraph_edges/_settings] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:17 WARN  org.elasticsearch.client.RestClient  - request [HEAD http://127.0.0.1:9200/janusgraph_edges] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:17 WARN  org.elasticsearch.client.RestClient  - request [POST http://127.0.0.1:9200/_aliases] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:18 WARN  org.elasticsearch.client.RestClient  - request [PUT http://127.0.0.1:9200/janusgraph_edges/_mapping] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:18 WARN  org.elasticsearch.client.RestClient  - request [HEAD http://127.0.0.1:9200/janusgraph_edges] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:18 WARN  org.elasticsearch.client.RestClient  - request [HEAD http://127.0.0.1:9200/janusgraph_edges] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:18 WARN  org.elasticsearch.client.RestClient  - request [POST http://127.0.0.1:9200/_aliases] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:18 WARN  org.elasticsearch.client.RestClient  - request [PUT http://127.0.0.1:9200/janusgraph_edges/_mapping] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
14:52:19 WARN  org.elasticsearch.client.RestClient  - request [POST http://127.0.0.1:9200/_bulk] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[standardjanusgraph[berkeleyje:/home/peter/janusgraph-full-0.6.0/janusgraph-full-0.6.0/conf/../db/berkeley], standard]
gremlin> saturn = g.V().has('name', 'saturn').next()
==>v[4184]
gremlin>  g.V(saturn).valueMap()
==>[name:[saturn],age:[10000]]
gremlin> g.V(saturn).in('father').in('father').values('name')
==>hercules
gremlin> g.E().has('place', geoWithin(Geoshape.circle(37.97, 23.72, 50)))
15:40:36 WARN  org.elasticsearch.client.RestClient  - request [POST http://127.0.0.1:9200/janusgraph_edges/_search?scroll=60s] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
15:40:36 WARN  org.elasticsearch.client.RestClient  - request [DELETE http://127.0.0.1:9200/_search/scroll/FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFmtlYUpTMmlWVERTbkwxUUpCc3BrV2cAAAAAAAAALxZnVGZfNGpwMVRmeV9NZ2gwaDR1V1NR] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
==>e[2k1s-35s-9hx-cn4][4096-battled->16384]
==>e[2kg0-35s-9hx-6f4][4096-battled->8320]
gremlin> g.E().has('place', geoWithin(Geoshape.circle(37.97, 23.72, 50))).as('source').inV().as('god2').select('source').outV().as('god1').select('god1', 'god2').by('name')
==>[god1:hercules,god2:nemean]
==>[god1:hercules,god2:hydra]
gremlin> pluto = g.V().has('name', 'pluto').next()
==>v[12288]
gremlin> g.V(pluto).out('lives').in('lives').values('name')
==>cerberus
==>pluto
gremlin> g.V(pluto).out('lives').in('lives').where(is(neq(pluto))).values('name')
==>pluto
gremlin> g.V(pluto).as('x').out('lives').in('lives').where(neq('x')).values('name')
==>cerberus
gremlin> g.V(pluto).out('brother').out('lives').values('name')
==>sky
==>sea
gremlin> g.V(pluto).out('brother').as('god').out('lives').as('place').select('god', 'place')
==>[god:v[4208],place:v[4224]]
==>[god:v[8376],place:v[4280]]
gremlin> g.V(pluto).out('brother').as('god').out('lives').as('place').select('god', 'place').by('name')
==>[god:jupiter,place:sky]
==>[god:neptune,place:sea]
gremlin> g.V(pluto).outE('lives').values('reason')
==>no fear of death
gremlin>  g.E().has('reason', textContains('loves'))
15:45:29 WARN  org.elasticsearch.client.RestClient  - request [POST http://127.0.0.1:9200/janusgraph_edges/_search?scroll=60s] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
15:45:29 WARN  org.elasticsearch.client.RestClient  - request [DELETE http://127.0.0.1:9200/_search/scroll/FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFmtlYUpTMmlWVERTbkwxUUpCc3BrV2cAAAAAAAAAMBZnVGZfNGpwMVRmeV9NZ2gwaDR1V1NR] returned 1 warnings: [299 Elasticsearch-7.14.0-dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1 "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.14/security-minimal-setup.html to enable security."]
==>e[4d3-6go-b2t-3aw][8376-lives->4280]
==>e[3ke-38w-b2t-39c][4208-lives->4224]
gremlin> g.E().has('reason', textContains('loves')).as('source').values('reason').as('reason').select('source').outV().values('name').as('god').select('source').inV().values('name').as('thing').select('god', 'reason', 'thing')
==>[god:neptune,reason:loves waves,thing:sea]
==>[god:jupiter,reason:loves fresh breezes,thing:sky]
gremlin> ==>[god:neptune, reason:loves waves, thing:sea]
 

全局图形索引

访问图形数据库中数据的典型模式是首先使用图形索引定位图形的入口点。该入口点是一个元素(或一组元素)——即一个顶点或边。从入口元素开始,Gremlin 路径描述描述了如何通过显式图结构遍历图中的其他元素。

鉴于name属性上有一个唯一索引,可以检索土星顶点。然后可以检查属性映射(即土星的键/值对)。如图所示,土星顶点有一个name“土星”、一个age10000 和一个type“泰坦”。可以通过遍历来检索 Saturn 的孙子:“谁是 Saturn 的孙子?” (“父亲”的反义词是“孩子”)。结果是赫拉克勒斯。

gremlin> saturn = g.V().has('name', 'saturn').next()
==>v[256]
gremlin> g.V(saturn).valueMap()
==>[name:[saturn], age:[10000]]
gremlin> g.V(saturn).in('father').in('father').values('name')
==>hercules

该属性place也在图形索引中。该属性place是边缘属性。因此,JanusGraph 可以索引图索引中的边。可以查询The Graph of the Gods了解雅典50 公里范围内发生的所有事件 (纬度:37.97,经度:23.72)。然后,根据这些信息,哪些顶点参与了这些事件。

gremlin> g.E().has('place', geoWithin(Geoshape.circle(37.97, 23.72, 50)))
==>e[a9x-co8-9hx-39s][16424-battled->4240]
==>e[9vp-co8-9hx-9ns][16424-battled->12520]
gremlin> g.E().has('place', geoWithin(Geoshape.circle(37.97, 23.72, 50))).as('source').inV().as('god2').select('source').outV().as('god1').select('god1', 'god2').by('name')
==>[god1:hercules, god2:hydra]
==>[god1:hercules, god2:nemean]

图索引是 JanusGraph 中的一种索引结构。图索引由 JanusGraph 自动选择以回答要求满足一个或多个约束(例如或)的所有顶点(g.V)或所有边()。JanusGraph 中索引的第二个方面被称为以顶点为中心的索引。以顶点为中心的索引用于加速图内部的遍历。稍后描述以顶点为中心的索引。g.Ehasinterval

图遍历示例

赫拉克勒斯,朱庇特和阿尔克墨涅的儿子 ,拥有超人的力量。赫拉克勒斯是半 神,因为他的父亲是神,他的母亲是人。 朱庇特的妻子朱诺对朱庇特的不忠感到愤怒。作为报复,她用暂时的精神错乱使赫拉克勒斯失明,并让他杀死了他的妻子和孩子。为了赎罪,德尔斐神谕命令赫拉克勒斯 为欧律斯透斯服务。欧律斯透斯任命赫拉克勒斯进行 12 次劳动。

在上一节中,证明了土星的孙子是大力士。这可以用 表示loop。本质上,赫拉克勒斯是沿in('father') 路径距离土星 2 步的顶点。

gremlin> hercules = g.V(saturn).repeat(__.in('father')).times(2).next()
==>v[1536]

赫拉克勒斯是半神。为了证明赫拉克勒斯是半人半神,必须检查他父母的出身。可以从赫拉克勒斯顶点穿越到他的母亲和父亲。最后,可以确定type它们中的每一个——产生“上帝”和“人类”。

gremlin> g.V(hercules).out('father', 'mother')
==>v[1024]
==>v[1792]
gremlin> g.V(hercules).out('father', 'mother').values('name')
==>jupiter
==>alcmene
gremlin> g.V(hercules).out('father', 'mother').label()
==>god
==>human
gremlin> hercules.label()
==>demigod

迄今为止的例子都是关于罗马万神殿中不同角色的遗传谱系。该物业图模型 是表现足以代表多种类型的事物和关系。就这样,《众神图》也鉴定了赫拉克勒斯的各种英雄功绩——他著名的十二功。在上一节中,人们发现赫拉克勒斯参与了雅典附近的两场战斗。可以通过遍历battledHercules 顶点之外的边来探索这些事件。

gremlin> g.V(hercules).out('battled')
==>v[2304]
==>v[2560]
==>v[2816]
gremlin> g.V(hercules).out('battled').valueMap()
==>[name:[nemean]]
==>[name:[hydra]]
==>[name:[cerberus]]
gremlin> g.V(hercules).outE('battled').has('time', gt(1)).inV().values('name')
==>cerberus
==>hydra

边缘属性timebattled边缘通过一个顶点的顶点为中心的索引编入索引。battled根据约束/过滤器检索与 Hercules 相关的边缘time比对所有边缘进行线性扫描和过滤(通常为O(log n),其中n是入射边缘的数量)更快。JanusGraph 足够智能,可以在可用时使用以顶点为中心的索引。toString()Gremlin 表达式的A显示分解为各个步骤。

gremlin> g.V(hercules).outE('battled').has('time', gt(1)).inV().values('name').toString()
==>[GraphStep([v[24744]],vertex), VertexStep(OUT,[battled],edge), HasStep([time.gt(1)]), EdgeVertexStep(IN), PropertiesStep([name],value)]

更复杂的图遍历示例

在 Tartarus 的深处住着冥王星。由于赫拉克勒斯与他的宠物塞伯鲁斯战斗,他与赫拉克勒斯的关系变得紧张。然而,赫拉克勒斯是他的侄子——他该如何让赫拉克勒斯为他的傲慢付出代价?

下面的 Gremlin 遍历提供了更多关于The Graph of the Gods 的例子。每次遍历的解释在前一行中作为//注释提供。

塔尔塔罗斯的同居者

gremlin> pluto = g.V().has('name', 'pluto').next()
==>v[2048]
gremlin> // who are pluto's cohabitants?
gremlin> g.V(pluto).out('lives').in('lives').values('name')
==>pluto
==>cerberus
gremlin> // pluto can't be his own cohabitant
gremlin> g.V(pluto).out('lives').in('lives').where(is(neq(pluto))).values('name')
==>cerberus
gremlin> g.V(pluto).as('x').out('lives').in('lives').where(neq('x')).values('name')
==>cerberus

冥王星的兄弟

gremlin> // where do pluto's brothers live?
gremlin> g.V(pluto).out('brother').out('lives').values('name')
==>sky
==>sea
gremlin> // which brother lives in which place?
gremlin> g.V(pluto).out('brother').as('god').out('lives').as('place').select('god', 'place')
==>[god:v[1024], place:v[512]]
==>[god:v[1280], place:v[768]]
gremlin> // what is the name of the brother and the name of the place?
gremlin> g.V(pluto).out('brother').as('god').out('lives').as('place').select('god', 'place').by('name')
==>[god:jupiter, place:sky]
==>[god:neptune, place:sea]

最后,冥王星住在塔尔塔罗斯,因为他对死亡毫不关心。另一方面,他的兄弟们根据他们对这些地点的某些品质的热爱来选择他们的地点。!

gremlin> g.V(pluto).outE('lives').values('reason')
==>no fear of death
gremlin> g.E().has('reason', textContains('loves'))
==>e[6xs-sg-m51-e8][1024-lives->512]
==>e[70g-zk-m51-lc][1280-lives->768]
gremlin> g.E().has('reason', textContains('loves')).as('source').values('reason').as('reason').select('source').outV().values('name').as('god').select('source').inV().values('name').as('thing').select('god', 'reason', 'thing')
==>[god:neptune, reason:loves waves, thing:sea]
==>[god:jupiter, reason:loves fresh breezes, thing:sky]

你可能感兴趣的:(知识图谱,知识图谱)