目录
• Grakn 下载安装
• 加载表结构及数据 、gql语句实现 增删改查
• 可视化工具Workbase 安装
• Java连接grakn 查询数据
最近因业务需要在学习 grakn.ai 图数据库, 总结几项基础应用篇分享给大家.
grakn.ai 是一个开源知识图谱存储及查询系统,其自带 grakn console 等数据交互接口,具有自动推理功能,具有 CNN 等图谱深度学习扩展组件。
将下载好的grakn.tar.gz包上传服务器, 解压到标准安装目录下。(不需要任何配置解压后既能使用)
cd 到 grakn-core-all-linux-1.5.7目录下通过 ./grakn server start 来启动grakn。
[root@nn1 grakn-core-all-linux-1.5.7]# ./grakn server status
====================================================================================================
________ _____ _______ __ __ __ __ _______ _______ _____ _______
| __ || _ \ | _ || | / /| \ | | | _ || _ || _ \ | ____|
| | |__|| | | | | | | || | / / | \ | | | | |__|| | | || | | | | |
| | ____ | |_| / | |_| || |/ / | \| | | | | | | || |_| / | |____
| ||_ || _ \ | _ || _ \ | _ | | | __ | | | || _ \ | ____|
| |__| || | \ \ | | | || | \ \ | | \ | | |_| || |_| || | \ \ | |____
|________||__| \__\|__| |__||__| \__\|__| \__| |_______||_______||__| \__\|_______|
THE KNOWLEDGE GRAPH
====================================================================================================
Starting Storage............SUCCESS
Starting Engine.....................SUCCESS
[root@nn1 grakn-core-all-linux-1.5.7]#
1. schema.gql + data.gql下载地址:
https://dev.grakn.ai/docs/files/social-network/schema.gql
https://dev.grakn.ai/docs/files/social-network/data.gql
2. grakn建表+增删改查gql语句测试
1. 创建keyspace + 表结构
./grakn console --keyspace grakn_ example --file /home/schema.gql
2. 往grakn_ example中插入数据
./grakn console --keyspace grakn_ example --file /home/data.gql
3. 进到grakn_ example的控制台中
./grakn console -k grakn_ example
4. 提供以下4条gql查询语句测试
// 查询person这个实体
match $p isa person; get;
// 查询person中实体有full-name属性的
match $p isa person, has full-name $n; get;
// 查询有employer,employee这两个关系的
match $emp (employer: $x, employee: $y) isa employment; get;
// 查询person实体中nickname属性为 "Mitzi",并且phone-number中带"+44"的.
match $p isa person, has nickname "Mitzi", has phone-number $pn; $pn contains "+44"; get;
------------
5.提供以下3条插入语句
// 插入属性类型实例
insert $x isa emotion; $x "like";
// 插入实例类型实例
insert $p isa person, has full-name "John Parkson", has gender "male", has email "[email protected]", has phone-number "+44-1234=567890";
// 插入关系类型实例
match
$org isa organisation, has name "Facelook";
$person isa person, has email "[email protected]";
insert $new-employment (employer: $org, employee: $person) isa employment;
$new-employment has reference-id "WGFTSH";
------------
6. 提供以下3条删除语句
// 删除实体类型实例
match $p isa person, has email "[email protected]"; delete $p;
// 删除关系类型实例
match
$org isa organisation, has name "Pharos";
$emp (employer: $org, employee: $p) isa employment;
delete $emp;
// 删除与属性的关联
match $t isa travel, has start-date 2013-12-22 via $r; delete $r;
------------
7. 提供以下3条更新语句
// 更新属性类型实例
## deleting the old
match $org isa organisation, has name "Medicely", has registration-number $rn via $r; delete $r;
## inserting the new
match $org isa organisation, has name "Medicely"; insert $org has registration-number "81726354";
// 更新给定属性的所有实例
## inserting the new
match
$m isa media, has caption $c;
$c contains "inappropriate word";
insert $m has caption "deleted";
## deleting the old
match $c isa caption; $c contains "inappropriate word"; delete $c;
// 更新关系的角色扮演者
## inserting the new
match
$p isa person, has name "Amabo";
$org isa organisation, has name "Etihw Esouh";
insert $emp (employer: $org, $employee: $p) isa employment;
## deleting the old
match
$p isa person, has name "Prumt";
$org isa organisation, has name "Etihw Esouh";
$emp (employer: $org, employee: $p) isa employment;
delete $emp;
3. gql语句支持sql中的大部分功能如聚合函数、分组、排序、or等, 以下是它的关键字
asc, by, compute, contains, count
delete, desc, from, label, limit
get, group, id, in, insert, or
match, max, mean, median, min
offset, order, regex, std, sum, to
1. 下载地址: https://grakn.ai/download#workbase 选择windows版本的workbase安装.
2. 启动并登录workbase; Host改成安装grakn core那台ip, 端口不变.
3. 在右上角选择要使用的keyspace, 输入gql语句查询.
1. 创建maven工程, 导入pom依赖.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>com.cape_7_post_officegroupId> <artifactId>GraknartifactId> <version>1.0version> <packaging>jarpackaging>
<properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> properties>
<dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>3.8.1version> <scope>testscope> dependency>
<dependency> <groupId>io.grakn.clientgroupId> <artifactId>grakn-clientartifactId> <version>1.5.3version> dependency> <dependency> <groupId>io.grakn.coregroupId> <artifactId>conceptartifactId> <version>1.5.6version> dependency> <dependency> <groupId>io.graqlgroupId> <artifactId>langartifactId> <version>1.0.1version> dependency> dependencies> project> |
2. java代码
package com.grakn;
import static graql.lang.Graql.parse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import grakn.client.GraknClient;
import grakn.core.concept.answer.ConceptMap;
import graql.lang.query.GraqlGet;
public class FirstQuery {
private static GraknClient client;
private static GraknClient.Session session;
private static GraknClient.Transaction transaction;
public void connect(String node,String keyspace) {
client = new GraknClient(node);
session = client.session(keyspace);
}
public void querySchema() {
transaction = session.transaction().write();
List queryAsList = Arrays.asList(
"match",
"$tx (driver: $x, driveway: $y) isa driving ,has passTime $a1;",
"$x isa car ,has plateNo $p1 ;",
"$y isa cross ,has crossNo $c1;",
"{$y has crossNo \"lk007\"; $a1>2019-01-14 ; $a1<2019-01-16 ;} or {$y has crossNo \"lk008\";$a1>2019-01-14 ; $a1<2019-01-16 ;} or {$y has crossNo \"lk009\"; $a1>2019-01-16 ; $a1<2019-01-18 ;} ;",
"get $p1 ,$a1 ,$c1 ;"
);
System.out.println("\n------------------Query statement----------------\n" + String.join("\n", queryAsList) + "\n-------------------------------------------------\n");
String query = String.join("", queryAsList);
List result = new ArrayList<>();
List answers = transaction.execute((GraqlGet) parse(query));
for (ConceptMap answer : answers) {
result.add(answer.get("p1").asAttribute().value().toString());
result.add(answer.get("a1").asAttribute().value().toString());
result.add(answer.get("c1").asAttribute().value().toString() + "\n");
}
System.out.println("\n------------------Results----------------\n" + String.join(", ", result) + "\n-----------------------------------------\n" + "\n Query Completed! \n");
}
public static void main(String[] args) {
FirstQuery grakn = new FirstQuery();
try {
grakn.connect("192.168.1.1:48555", "keyspace_test");
grakn.querySchema();
} catch (Throwable t) {
t.printStackTrace();
} finally {
transaction.close();
session.close();
client.close();
}
}
}
原文地址: https://blog.csdn.net/DelevinData/article/details/97385258