前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图数据库查询语言

图数据库查询语言

作者头像
JadePeng
发布2020-11-24 15:38:16
1K0
发布2020-11-24 15:38:16
举报
文章被收录于专栏:JadePeng的技术博客

本文介绍图数据库支持的gremlin和Cypher查询语言。

初始化数据

可使用gremlin api执行

gremlin api

POST http://localhost:8080/gremlin

代码语言:javascript
复制
{"gremlin":"这里是语句",
 "bindings": {},
"language": "gremlin-groovy",
"aliases": {
		"graph": "graphname", 
		"g": "__g_graphname"
	}
}

schema

代码语言:javascript
复制
schema = hugegraph.schema()

schema.propertyKey("name").asText().ifNotExist().create()
schema.propertyKey("age").asInt().ifNotExist().create()
schema.propertyKey("time").asInt().ifNotExist().create()
schema.propertyKey("reason").asText().ifNotExist().create()
schema.propertyKey("type").asText().ifNotExist().create()

schema.vertexLabel("character").properties("name", "age", "type").primaryKeys("name").nullableKeys("age").ifNotExist().create()
schema.vertexLabel("location").properties("name").primaryKeys("name").ifNotExist().create()

schema.edgeLabel("father").link("character", "character").ifNotExist().create()
schema.edgeLabel("mother").link("character", "character").ifNotExist().create()
schema.edgeLabel("battled").link("character", "character").properties("time").ifNotExist().create()
schema.edgeLabel("lives").link("character", "location").properties("reason").nullableKeys("reason").ifNotExist().create()
schema.edgeLabel("pet").link("character", "character").ifNotExist().create()
schema.edgeLabel("brother").link("character", "character").ifNotExist().create()

插入数据

代码语言:javascript
复制
// add vertices
Vertex saturn = graph.addVertex(T.label, "character", "name", "saturn", "age", 10000, "type", "titan")
Vertex sky = graph.addVertex(T.label, "location", "name", "sky")
Vertex sea = graph.addVertex(T.label, "location", "name", "sea")
Vertex jupiter = graph.addVertex(T.label, "character", "name", "jupiter", "age", 5000, "type", "god")
Vertex neptune = graph.addVertex(T.label, "character", "name", "neptune", "age", 4500, "type", "god")
Vertex hercules = graph.addVertex(T.label, "character", "name", "hercules", "age", 30, "type", "demigod")
Vertex alcmene = graph.addVertex(T.label, "character", "name", "alcmene", "age", 45, "type", "human")
Vertex pluto = graph.addVertex(T.label, "character", "name", "pluto", "age", 4000, "type", "god")
Vertex nemean = graph.addVertex(T.label, "character", "name", "nemean", "type", "monster")
Vertex hydra = graph.addVertex(T.label, "character", "name", "hydra", "type", "monster")
Vertex cerberus = graph.addVertex(T.label, "character", "name", "cerberus", "type", "monster")
Vertex tartarus = graph.addVertex(T.label, "location", "name", "tartarus")

// add edges
jupiter.addEdge("father", saturn)
jupiter.addEdge("lives", sky, "reason", "loves fresh breezes")
jupiter.addEdge("brother", neptune)
jupiter.addEdge("brother", pluto)
neptune.addEdge("lives", sea, "reason", "loves waves")
neptune.addEdge("brother", jupiter)
neptune.addEdge("brother", pluto)
hercules.addEdge("father", jupiter)
hercules.addEdge("mother", alcmene)
hercules.addEdge("battled", nemean, "time", 1)
hercules.addEdge("battled", hydra, "time", 2)
hercules.addEdge("battled", cerberus, "time", 12)
pluto.addEdge("brother", jupiter)
pluto.addEdge("brother", neptune)
pluto.addEdge("lives", tartarus, "reason", "no fear of death")
pluto.addEdge("pet", cerberus)
cerberus.addEdge("lives", tartarus)

创建索引

创建索引,使用REST API:

POST http://localhost:8080/graphs/hugegraph/schema/indexlabels

代码语言:javascript
复制
{
	"name": "characterAge",
	"base_type": "VERTEX_LABEL",
	"base_value": "character",
	"index_type": "RANGE",
	"fields": [
		"age"
	]
}

查询

API 说明

支持gremlinsparqlCypher api,推荐gremlin和Cypher

cypher api
代码语言:javascript
复制
http://127.0.0.1:8080/graphs/hugegraph/cypher?cypher=

eg:

代码语言:javascript
复制
curl http://127.0.0.1:8080/graphs/hugegraph/cypher?cypher=MATCH%20(n:character)-[:lives]-%3E(location)-[:lives]-(cohabitants)%20where%20n.name=%27pluto%27%20return%20cohabitants.name
gremlin api

POST http://localhost:8080/gremlin

代码语言:javascript
复制
{"gremlin":"这里是语句",
 "bindings": {},
"language": "gremlin-groovy",
"aliases": {
		"graph": "graphname", 
		"g": "__g_graphname"
	}
}
sparql api
代码语言:javascript
复制
GET http://127.0.0.1:8080/graphs/hugegraph/sparql?sparql=SELECT%20*%20WHERE%20{%20}

1. 查询hercules的祖父

代码语言:javascript
复制
g.V().hasLabel('character').has('name','hercules').out('father').out('father')

也可以通过repeat方式:

代码语言:javascript
复制
g.V().hasLabel('character').has('name','hercules').repeat(__.out('father')).times(2)

cypher

代码语言:javascript
复制
MATCH (n:character)-[:father]->()-[:father]->(grandfather) where n.name='hercules' return grandfather

2. Find the name of hercules's father

代码语言:javascript
复制
g.V().hasLabel('character').has('name','hercules').out('father').value('name')

cypher

代码语言:javascript
复制
MATCH (n:character)-[:father]->(father) where n.name='hercules' return father.name

3. Find the characters with age > 100

代码语言:javascript
复制
g.V().hasLabel('character').has('age',gt(100))

cypher

代码语言:javascript
复制
MATCH (n:character) where n.age > 10 return n

4. Find who are pluto's cohabitants

代码语言:javascript
复制
g.V().hasLabel('character').has('name','pluto').out('lives').in('lives').values('name')

cypher

代码语言:javascript
复制
MATCH (n:character)-[:lives]->(location)-[:lives]-(cohabitants) where n.name='pluto' return cohabitants.name

5. Find pluto can't be his own cohabitant

代码语言:javascript
复制
pluto = g.V().hasLabel('character').has('name', 'pluto')
g.V(pluto).out('lives').in('lives').where(is(neq(pluto)).values('name')

// use 'as'
g.V().hasLabel('character').has('name', 'pluto').as('x').out('lives').in('lives').where(neq('x')).values('name')
代码语言:javascript
复制
cypher> MATCH (src:character{name:"pluto"})-[:lives]->()<-[:lives]-(dst:character) RETURN dst.name

6. Pluto's Brothers

代码语言:javascript
复制
pluto = g.V().hasLabel('character').has('name', 'pluto').next()
// where do pluto's brothers live?
g.V(pluto).out('brother').out('lives').values('name')

// which brother lives in which place?
g.V(pluto).out('brother').as('god').out('lives').as('place').select('god','place')

// what is the name of the brother and the name of the place?
g.V(pluto).out('brother').as('god').out('lives').as('place').select('god','place').by('name')
代码语言:javascript
复制
MATCH (src:Character{name:"pluto"})-[:brother]->(bro:Character)-[:lives]->(dst)
RETURN bro.name, dst.name

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-11-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 初始化数据
  • gremlin api
    • schema
      • 插入数据
      • 创建索引
      • 查询
        • API 说明
          • cypher api
          • gremlin api
          • sparql api
      相关产品与服务
      图数据库 KonisGraph
      图数据库 KonisGraph(TencentDB for KonisGraph)是一种云端图数据库服务,基于腾讯在海量图数据上的实践经验,提供一站式海量图数据存储、管理、实时查询、计算、可视化分析能力;KonisGraph 支持属性图模型和 TinkerPop Gremlin 查询语言,能够帮助用户快速完成对图数据的建模、查询和可视化分析。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档