OrientDB应用举例-Social

基础信息

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

ip: 172.28.26.214:2480

先看一个效果展示:

OrientDB应用举例-Social_第1张图片
image.png
OrientDB应用举例-Social_第2张图片
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

OrientDB应用举例-Social_第3张图片
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
简介:
有一些例子可以参考。

你可能感兴趣的:(OrientDB应用举例-Social)