编译安装

# root下
useradd agens
mkdir -p /usr/local/agens
chown -R agens:agens /usr/local/agens/

# agens下
git clone https://github.com/bitnine-oss/agensgraph.git

# --prefix指定安装目录
./configure --prefix=/usr/local/agens
make
make install

初始化启动数据库


initdb -D /home/agens/work/data/agens

# 修改端口号
echo 'port = 5462' >> /home/agens/work/data/agens/postgresql.conf

ag_ctl start -D /home/agens/work/data/agens

# 官方文档说默认创建的库同当前用户,即agens,
# 但是我实验创建的是postgres库

# 登陆默认库
agens -d postgres -p 5462

# 创建新库
createdb agens -p 5462

# 登陆新库
agens -d agens -p 5462

简单使用


# graph_path,类似于schema
# 默认为空,session级别,创建后,自动赋值给graph_path
CREATE GRAPH network;
SET graph_path = network;

# 创建顶点(vertice),边(edge)
CREATE VLABEL person;
CREATE ELABEL knows;

# 创建点及边
CREATE (:person {name: 'Tom'})-[:knows {fromdate:'2011-11-24'}]->(:person {name: 'Summer'});
CREATE (:person {name: 'Pat'})-[:knows {fromdate:'2013-12-25'}]->(:person {name: 'Nikki'});
CREATE (:person {name: 'Olive'})-[:knows {fromdate:'2015-01-26'}]->(:person {name: 'Todd'});

MATCH (p:Person {name: 'Tom'}),(k:Person{name: 'Pat'}) 
CREATE (p)-[:KNOWS {fromdate:'2017-02-27'} ]->(k);

生成图如下:
image.png

查询

# 
MATCH (n:person {name: 'Tom'})-[:knows]->(m:person) RETURN n.name AS n, m.name AS m;
   n   |    m
-------+----------
 "Tom" | "Summer"
 "Tom" | "Pat"
(2 rows)


MATCH (p:person {name: 'Tom'})-[:knows]->(f:person)
RETURN f.name
UNION ALL
MATCH (p:person {name: 'Tom'})-[:knows]->()-[:knows]->(f:person)
RETURN f.name;

   name
----------
 "Summer"
 "Pat"
 "Nikki"
(3 rows)


MATCH (p:person {name: 'Tom'})-[r:knows*1..2]->(f:person)
RETURN f.name, r[1].fromdate;
   name   |   fromdate
----------+--------------
 "Summer" |
 "Pat"    |
 "Nikki"  | "2013-12-25"
(3 rows)

参考资料:
https://github.com/bitnine-os...
https://cdn.modb.pro/wiki/4440
https://www.jianshu.com/p/bc6...
http://bitnine.net/documentat...


黑暗森林
12 声望2 粉丝

弱小和无知不是生存的障碍,傲慢才是!