OrientDB应用举例-Social

基础信息

  • orientdb console: v2.2.31
  • gremlin v2.6.0
  • orientdb api: 2.2.x

ip: 172.28.26.214:2480

先看一个效果展示:

image.png
image.png

orientdb {db=social}> select * from V;

+----+------+---------+-------+-------+-------+--------+--------+---------+---------+--
-------+---------+---------+---------+---------+---------+---------+---------+---------
+
|#   |@RID  |@CLASS   |out_Eat|type   |in_Eat |in_Loves|in_Twins|name     |in_Friend|ou
t_Twins|out_WrokI|out_Frien|out_Study|in_Classm|out_Loves|out_Class|in_WrokIn|in_StudyI
|
+----+------+---------+-------+-------+-------+--------+--------+---------+---------+--
-------+---------+---------+---------+---------+---------+---------+---------+---------
+
|0   |#26:0 |Person   |[#58:0]|       |       |        |        |Luca     |         |
       |         |         |         |         |         |         |         |
|
|1   |#27:0 |Person   |       |       |       |        |        |Bill     |         |
       |         |         |         |         |         |         |         |
|
|2   |#28:0 |Person   |       |       |       |        |        |Jay      |         |
       |         |         |         |         |         |         |         |
|
|3   |#34:0 |Teacher  |       |       |       |[#99:0] |        |Miss Li  |         |
       |[#139:0] |         |         |         |         |         |         |
|
|4   |#35:0 |Teacher  |       |       |       |        |        |Mr Luo   |         |
       |[#138:0] |         |         |         |         |         |         |
|
|5   |#42:0 |Student  |       |       |       |        |        |LiLei    |         |
       |         |[#82:0]  |[#130:0] |         |[#98:0...|[#90:0]  |         |
|
|6   |#43:0 |Student  |       |       |       |[#98:0] |        |LiMin    |[#82:0]  |
       |         |         |         |[#90:0]  |         |         |         |
|
|7   |#44:0 |Student  |       |       |       |        |        |HanMeimei|         |
       |         |         |[#131:0] |         |         |         |         |         |
|8   |#45:0 |Student  |       |       |       |        |        |Lucy     |         |[#106:0] |         |         |         |         |         |[#91:0]  |         |         |
|9   |#46:0 |Student  |       |       |       |        |[#106:0]|Lily     |         |         |         |         |         |[#91:0]  |         |         |         |         |
|10  |#50:0 |Restau...|       |Pizza  |[#58:0]|        |        |Dante    |         |         |         |         |         |         |         |         |         |         |
|11  |#51:0 |Restau...|       |French |       |        |        |Charlie  |         |         |         |         |         |         |         |         |         |         |
|12  |#114:0|School   |       |Junior |       |        |        |No.2     |         |         |         |         |         |         |         |         |[#138:...|[#130:...|
+----+------+---------+-------+-------+-------+--------+--------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+

13 item(s) found. Query executed in 0.001 sec(s).


orientdb {db=social}> select * from E;

+----+------+---------+-----+------+
|#   |@RID  |@CLASS   |out  |in    |
+----+------+---------+-----+------+
|0   |#58:0 |Eat      |#26:0|#50:0 |
|1   |#82:0 |Friend   |#42:0|#43:0 |
|2   |#90:0 |Classmate|#42:0|#43:0 |
|3   |#91:0 |Classmate|#45:0|#46:0 |
|4   |#98:0 |Loves    |#42:0|#43:0 |
|5   |#99:0 |Loves    |#42:0|#34:0 |
|6   |#106:0|Twins    |#45:0|#46:0 |
|7   |#130:0|StudyIn  |#42:0|#114:0|
|8   |#131:0|StudyIn  |#44:0|#114:0|
|9   |#138:0|WrokIn   |#35:0|#114:0|
|10  |#139:0|WrokIn   |#34:0|#114:0|
+----+------+---------+-----+------+

11 item(s) found. Query executed in 0.002 sec(s).

建立一个social 的 database.

storage type: plocal
database type: graph

image.png

连接:

orientdb> connect remote:localhost/social root root

Connecting to database [remote:localhost/social] with user 'root'...OK

建立一个顶点 Person

orientdb {db=social}>  CREATE CLASS Person EXTENDS V

Class created successfully. Total classes in database now: 12.

NOTE: 建立一个Vertex class Teacher

orientdb {db=social}>  CREATE CLASS Teacher EXTENDS V, Person

Class created successfully. Total classes in database now: 19.

NOTE: 建立一个Vertex class Student

orientdb {db=social}>  CREATE CLASS Student  EXTENDS V, Person

Class created successfully. Total classes in database now: 20.

建立 Restaurant

orientdb {db=social}> CREATE CLASS Restaurant EXTENDS V

Class created successfully. Total classes in database now: 13.

建立 school

orientdb {db=social}> CREATE CLASS School EXTENDS V

Class created successfully. Total classes in database now: 23.

建立 Person 下的记录, Luca, Bill, Jay

orientdb {db=social}> CREATE VERTEX Person SET name='Luca'

Created vertex 'Person#26:0{name:Luca} v1' in 0.073000 sec(s).

orientdb {db=social}> CREATE VERTEX Person SET name='Bill'

Created vertex 'Person#27:0{name:Bill} v1' in 0.009000 sec(s).

orientdb {db=social}> CREATE VERTEX Person SET name='Jay'

Created vertex 'Person#28:0{name:Jay} v1' in 0.023000 sec(s).

建立 Teacher: Miss Li, Mr Luo

orientdb {db=social}> CREATE VERTEX Teacher SET name='Miss Li'

Created vertex 'Teacher#82:0{name:Miss Li} v1' in 0.055000 sec(s).

orientdb {db=social}> CREATE VERTEX Teacher SET name='Mr Luo'

Created vertex 'Teacher#83:0{name:Mr Luo} v1' in 0.014000 sec(s).

建立 Teacher: LiLei, LiMin, HanMeiMei,Lily, Lucy

orientdb {db=social}>  CREATE VERTEX Student SET name='LiLei'

Created vertex 'Student#90:0{name:LiLei} v1' in 0.028000 sec(s).

orientdb {db=social}>  CREATE VERTEX Student SET name='LiMin'

Created vertex 'Student#91:0{name:LiMin} v1' in 0.038000 sec(s).

orientdb {db=social}>  CREATE VERTEX Student SET name='HanMeimei'

Created vertex 'Student#92:0{name:HanMeimei} v1' in 0.021000 sec(s).


orientdb {db=social}>   CREATE VERTEX Student SET name='Lucy'

Created vertex 'Student#45:0{name:Lucy} v1' in 0.002000 sec(s).


orientdb {db=social}>  CREATE VERTEX Student SET name='Lily'

Created vertex 'Student#46:0{name:Lily} v1' in 0.001000 sec(s).

NOTE: 建立Restaurant 的记录, Dante, Charlie

orientdb {db=social}>  CREATE VERTEX Restaurant SET name='Dante', type='Pizza'

Created vertex 'Restaurant#34:0{name:Dante,type:Pizza} v1' in 0.043000 sec(s).

orientdb {db=social}> CREATE VERTEX Restaurant SET name='Charlie', type='French'

Created vertex 'Restaurant#35:0{name:Charlie,type:French} v1' in 0.070000 sec(s).

NOTE: 建立School 的记录, NO.2

orientdb {db=social}> CREATE VERTEX School SET name='No.2', type='Junior '

Created vertex 'School#114:0{name:No.2,type:Junior } v1' in 0.001000 sec(s).

NOTE: 建立关系 Eat

orientdb {db=social}> CREATE CLASS Eat EXTENDS E

Class created successfully. Total classes in database now: 14.

NOTE: 建立关系 love

orientdb {db=social}> CREATE CLASS Love EXTENDS E

Class created successfully. Total classes in database now: 15.

NOTE: 建立关系 Know

orientdb {db=social}> CREATE CLASS Know EXTENDS E

Class created successfully. Total classes in database now: 16.

NOTE: 建立关系 Friend

orientdb {db=social}> CREATE CLASS Friend EXTENDS E, Know

Class created successfully. Total classes in database now: 17.

NOTE: 建立关系 Classmate

orientdb {db=social}> CREATE CLASS Classmate  EXTENDS E, Know

Class created successfully. Total classes in database now: 18.

NOTE: 建立关系 Loves

orientdb {db=social}> CREATE CLASS Loves  EXTENDS E, Know

Class created successfully. Total classes in database now: 21.

NOTE: 建立关系 Twins

orientdb {db=social}>  CREATE CLASS Twins  EXTENDS E, Know

Class created successfully. Total classes in database now: 22.

NOTE: 建立关系 WorkIn

orientdb {db=social}> CREATE CLASS WorkIN  EXTENDS E

Class created successfully. Total classes in database now: 24.

NOTE: 建立关系 StudyIn

orientdb {db=social}> CREATE CLASS StudyIn  EXTENDS E

Class created successfully. Total classes in database now: 25.

NOTE: 建立一个Map: Luca Eat Dante

orientdb {db=social}>  create EDGE Eat from (select from Person where name = 'Luca') to (select from Restaurant where name = 'Dante');

+----+-----+------+-----+-----+
|#   |@RID |@CLASS|out  |in   |
+----+-----+------+-----+-----+
|0   |#42:0|Eat   |#26:0|#34:0|
+----+-----+------+-----+-----+
Created '1' edges in 0.014000 sec(s).

LiLei Friend Limin

orientdb {db=social}> create EDGE Friend from (select from Student where name = 'LiLei') to (select from Student where name = 'LiMin');

+----+-----+------+-----+-----+
|#   |@RID |@CLASS|out  |in   |
+----+-----+------+-----+-----+
|0   |#82:0|Friend|#42:0|#43:0|
+----+-----+------+-----+-----+
Created '1' edges in 0.003000 sec(s).

NOTE: LiLei Loves LiMin

orientdb {db=social}> create EDGE Loves from (select from Student where name = 'LiLei') to (select from Student where name = 'LiMin');

+----+-----+------+-----+-----+
|# |@RID |@CLASS|out |in |
+----+-----+------+-----+-----+
|0 |#98:0|Loves |#42:0|#43:0|
+----+-----+------+-----+-----+
Created '1' edges in 0.002000 sec(s).

NOTE: LiLei Classmate LiMin

orientdb {db=social}> create EDGE Classmate from (select from Student where name = 'LiLei') to (select from Student where name = 'LiMin');

+----+-----+---------+-----+-----+
|#   |@RID |@CLASS   |out  |in   |
+----+-----+---------+-----+-----+
|0   |#90:0|Classmate|#42:0|#43:0|
+----+-----+---------+-----+-----+
Created '1' edges in 0.002000 sec(s).

NOTE: LiLei Loves Miss Li

orientdb {db=social}> create EDGE Loves from (select from Student where name = 'LiLei') to (select from Teacher where name = 'Miss Li');

+----+-----+------+-----+-----+
|#   |@RID |@CLASS|out  |in   |
+----+-----+------+-----+-----+
|0   |#99:0|Loves |#42:0|#34:0|
+----+-----+------+-----+-----+
Created '1' edges in 0.003000 sec(s).

NOTE: 建立一个Map: Lucy Twins Lily

orientdb {db=social}> create EDGE Twins from (select from Student where name = 'Lucy') to (select from Student where name = 'Lily');

+----+------+------+-----+-----+
|#   |@RID  |@CLASS|out  |in   |
+----+------+------+-----+-----+
|0   |#106:0|Twins |#45:0|#46:0|
+----+------+------+-----+-----+
Created '1' edges in 0.002000 sec(s).


NOTE: 建立一个Map: Lucy Classmate Lily

orientdb {db=social}> create EDGE Classmate from (select from Student where name = 'Lucy') to (select from Student where name = 'Lily');

+----+-----+---------+-----+-----+
|#   |@RID |@CLASS   |out  |in   |
+----+-----+---------+-----+-----+
|0   |#91:0|Classmate|#45:0|#46:0|
+----+-----+---------+-----+-----+
Created '1' edges in 0.002000 sec(s).

NOTE: 建立一个Map: LiLei StudyIn NO2

orientdb {db=social}> create EDGE StudyIn from (select from Student where name = 'LiLei') to (select from School where name = 'No.2');

+----+------+-------+-----+------+
|#   |@RID  |@CLASS |out  |in    |
+----+------+-------+-----+------+
|0   |#130:0|StudyIn|#42:0|#114:0|
+----+------+-------+-----+------+
Created '1' edges in 0.002000 sec(s).

NOTE: 建立一个Map: HanMeiMei StudyIn NO2

orientdb {db=social}> create EDGE StudyIn from (select from Student where name = 'HanMeimei') to (select from School where name = 'No.2');

+----+------+-------+-----+------+
|#   |@RID  |@CLASS |out  |in    |
+----+------+-------+-----+------+
|0   |#131:0|StudyIn|#44:0|#114:0|
+----+------+-------+-----+------+
Created '1' edges in 0.003000 sec(s).

NOTE: 建立一个Map: Mr Luo WrokIn NO2

orientdb {db=social}> create EDGE WrokIn from (select from Teacher where name = 'Mr Luo') to (select from School where name = 'No.2');

+----+------+------+-----+------+
|#   |@RID  |@CLASS|out  |in    |
+----+------+------+-----+------+
|0   |#138:0|WrokIn|#35:0|#114:0|
+----+------+------+-----+------+
Created '1' edges in 0.045000 sec(s).

NOTE: 建立一个Map: Miss Li WrokIn NO2

orientdb {db=social}> create EDGE WrokIn from (select from Teacher where name = 'Miss Li') to (select from School where name = 'No.2');

+----+------+------+-----+------+
|#   |@RID  |@CLASS|out  |in    |
+----+------+------+-----+------+
|0   |#139:0|WrokIn|#34:0|#114:0|
+----+------+------+-----+------+
Created '1' edges in 0.002000 sec(s).

查询

查看有几个Person

这里可以把Person 的子类 Teacher, Student 查出来。

orientdb {db=social}> select * from Person;

+----+-----+-------+-------+--------+--------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
|#   |@RID |@CLASS |out_Eat|in_Loves|in_Twins|name     |in_Friend|out_Twins|out_WrokI|out_Frien|out_Study|in_Classm|out_Loves|out_Class|
+----+-----+-------+-------+--------+--------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
|0   |#26:0|Person |[#58:0]|        |        |Luca     |         |         |         |         |         |         |         |         |
|1   |#27:0|Person |       |        |        |Bill     |         |         |         |         |         |         |         |         |
|2   |#28:0|Person |       |        |        |Jay      |         |         |         |         |         |         |         |         |
|3   |#34:0|Teacher|       |[#99:0] |        |Miss Li  |         |         |[#139:0] |         |         |         |         |         |
|4   |#35:0|Teacher|       |        |        |Mr Luo   |         |         |[#138:0] |         |         |         |         |         |
|5   |#42:0|Student|       |        |        |LiLei    |         |         |         |[#82:0]  |[#130:0] |         |[#98:0...|[#90:0]  |
|6   |#43:0|Student|       |[#98:0] |        |LiMin    |[#82:0]  |         |         |         |         |[#90:0]  |         |         |
|7   |#44:0|Student|       |        |        |HanMeimei|         |         |         |         |[#131:0] |         |         |         |
|8   |#45:0|Student|       |        |        |Lucy     |         |[#106:0] |         |         |         |         |         |[#91:0]  |
|9   |#46:0|Student|       |        |[#106:0]|Lily     |         |         |         |         |         |[#91:0]  |         |         |
+----+-----+-------+-------+--------+--------+---------+---------+---------+---------+---------+---------+---------+---------+---------+

10 item(s) found. Query executed in 0.002 sec(s).

查看定义了几种关系(关系列表)

orientdb {db=social}> select * from Know;

+----+------+---------+-----+-----+
|#   |@RID  |@CLASS   |out  |in   |
+----+------+---------+-----+-----+
|0   |#82:0 |Friend   |#42:0|#43:0|
|1   |#90:0 |Classmate|#42:0|#43:0|
|2   |#91:0 |Classmate|#45:0|#46:0|
|3   |#98:0 |Loves    |#42:0|#43:0|
|4   |#99:0 |Loves    |#42:0|#34:0|
|5   |#106:0|Twins    |#45:0|#46:0|
+----+------+---------+-----+-----+

6 item(s) found. Query executed in 0.002 sec(s).


orientdb {db=social}>  select * from E;

+----+------+---------+-----+------+
|#   |@RID  |@CLASS   |out  |in    |
+----+------+---------+-----+------+
|0   |#58:0 |Eat      |#26:0|#50:0 |
|1   |#82:0 |Friend   |#42:0|#43:0 |
|2   |#90:0 |Classmate|#42:0|#43:0 |
|3   |#91:0 |Classmate|#45:0|#46:0 |
|4   |#98:0 |Loves    |#42:0|#43:0 |
|5   |#99:0 |Loves    |#42:0|#34:0 |
|6   |#106:0|Twins    |#45:0|#46:0 |
|7   |#130:0|StudyIn  |#42:0|#114:0|
|8   |#131:0|StudyIn  |#44:0|#114:0|
|9   |#138:0|WrokIn   |#35:0|#114:0|
|10  |#139:0|WrokIn   |#34:0|#114:0|
+----+------+---------+-----+------+

11 item(s) found. Query executed in 0.002 sec(s).

NOTE: 查询 LiMin 的 Friend StudyIn 的 School

orientdb {db=social}> MATCH {class: Person, as: people, where:(name = 'LiMin')}.both('Friend').both('StudyIn') {as: school} RETURN person, school.name

+----+------+-----------+
|#   |person|school_name|
+----+------+-----------+
|0   |      |No.2       |
+----+------+-----------+

1 item(s) found. Query executed in 0.002 sec(s).

NOTE: 查询 LiMin 的 Friend StudyIn 的 School 中的 Teacher

orientdb {db=social}>  MATCH {class: Person, as: people, where:(name = 'LiMin')}.both('Friend').both('StudyIn').both('WrokIn') {as: teacher} RETURN person, teacher.name

+----+------+------------+
|#   |person|teacher_name|
+----+------+------------+
|0   |      |Mr Luo      |
|1   |      |Miss Li     |
+----+------+------------+

2 item(s) found. Query executed in 0.002 sec(s).

NOTE: 查询 LiMin 的 Friend StudyIn 的 School 中的 Teacher 和 Student

orientdb {db=social}> MATCH {class: Person, as: people, where:(name = 'LiMin')}.both('Friend').both('StudyIn').both('E') {as: person} RETURN person.name

+----+-----------+
|#   |person_name|
+----+-----------+
|0   |Mr Luo     |
|1   |Miss Li    |
|2   |LiLei      |
|3   |HanMeimei  |
+----+-----------+

4 item(s) found. Query executed in 0.003 sec(s).

扩展阅读

orientdb sql query to select edge and vertex fields property.

https://stackoverflow.com/questions/28534120/orientdb-sql-query-to-select-edge-and-vertex-fields-property
简介:
有一些例子可以参考。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,616评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,020评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,078评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,040评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,154评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,265评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,298评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,072评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,491评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,795评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,970评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,654评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,272评论 3 318
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,985评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,223评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,815评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,852评论 2 351

推荐阅读更多精彩内容