JanusGraph客户端创建图结构和索引

客户端创建图结构和索引

一、Schema创建

1.gremlin客户端创建
cd /opt/janusgraph/janusgraph-0.3.3
#启动客户端
./bin/gremlin.sh

在这里插入图片描述

#获取链接
JanusGraphFactory.open('conf/janusgraph-hbase-es.properties')
#创建顶点标签,相当于顶点的类型,可以创建多种
mgmt = graph.openManagement()
mgmt.makeVertexLabel('event_node').make()
mgmt.getVertexLabel('endpoint').make()
mgmt.commit()
#创建边标签,相当于边的类型,可以创建多种
mgmt = graph.openManagement()
mgmt.makeEdgeLabel("point").make()
mgmt.commit()
#创建属性
mgmt = graph.openManagement();
tid = mgmt.makePropertyKey("tid").dataType(String.class).make()
date = mgmt.makePropertyKey("date").dataType(String.class).make()
pathid = mgmt.makePropertyKey("pathid").dataType(String.class).make()
client = mgmt.makePropertyKey("client").dataType(String.class).make()
eventid = mgmt.makePropertyKey("eventid").dataType(String.class).make()
count = mgmt.makePropertyKey("count").dataType(Integer.class).make()
mgmt.commit()
#关联属性和标签
mgmt = graph.openManagement()
event_node = mgmt.getVertexLabel('event_node')
endpoint = mgmt.getVertexLabel('endpoint')
point = mgmt.getEdgeLable('point')
tid = mgmt.getPropertyKey("tid")
mgmt.addProperties(event_node,tid)
mgmt.addProperties(endpoint,tid)
mgmt.addProperties(point,tid)
mgmt.commit()

#查看是否创建成功
mgmt.printSchema()

注:图片的schema是以前创建的,所以count类型是string不是integer
在这里插入图片描述

二、创建索引

这里介绍两种图形索引
组合索引(Composite Index):
组合索引通过一个或者一组属性组成的固定的属性组合进行等值检索,这些属性必须是预定义且是固定顺序的。 组合索引支持唯一约束,设置为unique,则该属性组合必须保证唯一性。 排序在内存中,成本高。
混合索引(Mixed Index)
使用混合索引遍历顶点或边的时候,可以使用任何预先添加的属性key的组合。混合索引比组合索引更灵活,支持更多的条件谓词,而组合索引只支持相等判断查询。混合索引比组合索引查询速度要慢。
和组合索引不同,混合索引需要配置索引后端,如elasticsearch, solr, luncen。混合索引支持全文检索、范围查询、地理位置查询等,并且不支持唯一性检查。排序效率高。

1.客户端创建索引

创建索引前先检查是否存在幽灵实例,有就关掉,最好也不要先写入数据,否则会很痛苦。。

#查看
mgmt = graph.openManagement()
mgmt.getOpenInstances()
mgmt.commit()

在这里插入图片描述

#关闭
mgmt = graph.openManagement()
ids = mgmt.getOpenInstances()
for(String id : ids){if(!id.contains("(")){mgmt.forceCloseInstance(id)}}
mgmt.commit()
mgmt = graph.openManagement()
#获取属性
tid =  mgmt.getPropertyKey("tid")
date = mgmt.getPropertyKey("date")
event_id = mgmt.getPropertyKey("event_id")
client = mgmt.getPropertyKey("client")
#创建索引,indexOnly(vertexLabel)表示只在顶点上创建了索引,使用时需要用hasLable('')指定顶点lable信息,要不然不会命中索引
mgmt.buildIndex("composite_index_tid",Vertex.class).addKey(tid).buildCompositeIndex()
mgmt.buildIndex("mixed_index_date",Vertex.class).addKey(date).indexOnly(vertexLabel).buildMixedIndex("search")
mgmt.buildIndex("mixed_index_date_event_id",Vertex.class).addKey(date).addKey(event_id).indexOnly(vertexLabel).buildMixedIndex("search")
mgmt.buildIndex("mixed_index_date_event_id_client",Vertex.class).addKey(date).addKey(client).addKey(event_id).indexOnly(vertexLabel).buildMixedIndex("search")
mgmt.commit()

#更新索引状态到 REGISTER
mgmt = graph.openManagement()
mgmt.updateIndex(mgmt.getGraphIndex("index_tid"),SchemaAction.REGISTER_INDEX).get()
mgmt.updateIndex(mgmt.getGraphIndex('mixed_index_date'),SchemaAction.REGISTER_INDEX).get()
mgmt.updateIndex(mgmt.getGraphIndex('mixed_index_date_event_id'),SchemaAction.REGISTER_INDEX).get()
mgmt.updateIndex(mgmt.getGraphIndex("mixed_index_date_event_id_client"),SchemaAction.REGISTER_INDEX).get()
mgmt.commit()

#等待索引生效,这步可能会超时报错,多等一会,或者隔段时间试一次
ManagementSystem.awaitGraphIndexStatus(graph,"index_tid").status(SchemaStatus.REGISTERED).call()
ManagementSystem.awaitGraphIndexStatus(graph,"mixed_index_date").status(SchemaStatus.REGISTERED).call()
ManagementSystem.awaitGraphIndexStatus(graph,"mixed_index_date_event_id").status(SchemaStatus.REGISTERED).call()
ManagementSystem.awaitGraphIndexStatus(graph,"mixed_index_date_event_id_client").status(SchemaStatus.REGISTERED).call()

#更新索引状态到 ENABLE
m = graph.openManagement()
m.updateIndex(m.getGraphIndex('index_tid'), SchemaAction.ENABLE_INDEX).get() 
m.updateIndex(m.getGraphIndex('mixed_index_date'), SchemaAction.ENABLE_INDEX).get() 
m.updateIndex(m.getGraphIndex('mixed_index_date_event_id'), SchemaAction.ENABLE_INDEX).get() 
m.updateIndex(m.getGraphIndex('mixed_index_date_event_id_client'), SchemaAction.ENABLE_INDEX).get() 
m.commit() 

#等待索引生效,这步可能也会超时报错,所以创建索引前尽量不要写数据在里面
ManagementSystem.awaitGraphIndexStatus(graph, 'index_tid').status(SchemaStatus.ENABLED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'mixed_index_date').status(SchemaStatus.ENABLED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'mixed_index_date_event_id').status(SchemaStatus.ENABLED).call()
ManagementSystem.awaitGraphIndexStatus(graph, 'mixed_index_date_event_id_client').status(SchemaStatus.ENABLED).call()

#最后查看索引状态,已经从INSTALLED 变成 REGISTERED 最后变成ENABLED就可用了
m = graph.openManagement()
m.printSchema()

在这里插入图片描述

索引真的很有用!查询速度提升百倍不止!!!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值