📜 ⬆️ ⬇️

OrientDB - a simple example of working with graphs for beginners.

OrientDB - a view of a person who is used to working with relational databases.
Let me remind you that OrientDB is a graph, document-oriented database implemented in Java.

I decided to write an article for beginners, because in the beginning it is the most difficult, but in Russian. There are practically no introductory articles with intelligible examples.

I think telling how to download orientDB is not necessary, but just in case www.orientechnologies.com/download
I don’t want to talk about the theory in this article, it seems to me that many just want to touch OrientDB and at least understand a little. But for those who want a little theory in Russian, there is an unfinished article .
Well, we will start and first start the orientDB server:
/programs/orientDB/bin/server.sh

Secondly, let's go to the orientDB console:
/programs/orientDB/bin/console.sh

Third, we need to know the root password, and it is registered in the file:
/config/orientdb-server-config.xml

look for the line with "password" and you will understand everything at once.
Knowing the password, you can create a database:
orientdb> create database remote: localhost / people root PASWORD local

I recall the syntax is:
create database <database-url> <user> <password> <storage-type> [<db-type>]

View information about our database:
orientdb {people}> info

and we see that during the creation standard classes, clusters and indexes appear.

Vertex Vertices


Create a person class (Person):
create class Person extends V

notice we created a class like vertex (vertex)
Add the string property name to the class.
create property Person.name string

Create two vertices:
create vertex Person set name = 'Joanie'
create vertex Person set name = 'Chachie'

Edge Edges



In the article that I was going to translate, the author creates a class in which they will allegedly indicate the connections between these persons.
create class Loves extends E

in fact, this class does not play any role and does not need to be created (not yet necessary).
Add one connection between the persons:
create edge Loves from # 11: 1 to # 11: 0

notice we created an EDGE link (edge).
So, we have connected two persons, we will check our work:

  orientdb {people}> select from Person

 ---- + ----- + ------- + -------- + ---------
 # | @ RID | name | in_Loves | out_Loves

 ---- + ----- + ------- + -------- + ---------
 0 | # 11: 0 | Joanie | # 11: 1 | null     
 1 | # 11: 1 | Chachie | null | # 11: 0    

 ---- + ----- + ------- + -------- + --------- 

Notice, there are 2 fields in the Person class:

these fields are created automatically, and you will not be able to abandon their creation, and you don’t need to, because in these fields you can see the connection of our persons (@ RID), but what does it give us? And it gives us the fact that these fields can be accessed as objects, for example, if you write a query:
')
  orientdb {people}> select name, in_Loves.name as in, out_Loves.name as out from Person

 ---- + ----- + ------- + ------- + ------
 # | @ RID | name | in | out   

 ---- + ----- + ------- + ------- + ------
 0 | # -2: 1 | Joanie | Chachie | null  
 1 | # -2: 2 | Chachie | null | Joanie

 ---- + ----- + ------- + ------- + ------ 

then we will get not @ RID links, but data from related objects.
Pay attention because if our Person class is inherited from class V, then all the data connections will also exist in class V:

  orientdb {people}> select from V        

 ---- + ----- + ------- + -------- + ---------
 # | @ RID | name | in_Loves | out_Loves

 ---- + ----- + ------- + -------- + ---------   
 5 | # 11: 0 | Joanie | # 11: 1 | null     
 6 | # 11: 1 | Chachie | null | # 11: 0    

 ---- + ----- + ------- + -------- + --------- 

Someone who has studied a bit of OrientDB may ask, but what about E (EDGE), why didn't the edge go in there?
It's very simple that the link is registered in E, the edge must be given a name. Those. we wrote:
create edge Loves from # 11: 1 to # 11: 0

but you had to write:
create edge from # 11: 1 to # 11: 0 set MyFieldName = 1

In addition, you can create a class Loves (see above, we missed it) and creating a link to indicate the name of the class:
create edge Loves from # 11: 1 to # 11: 0 set MyFieldName = 1

thus, by specifying the field names (MyFieldName), we can differentiate the types of relationships (in one class), and by specifying the class name, we can further distinguish between the types of relationships by classes. However, in class E all kinds of connections will always be indicated, since it is from this class that we extend (pay attention to how we created the Loves class).
Some of you will say: why do I need to extend from class E, if all the same all the data gets there?
I also asked this question and the author of OrientDB answered me this way: plus the fact that OrientDB is better partitioned using graphs, and of course this affects the speed of data sampling.
Recall that partitioning is the partitioning of large tables into logical parts according to selected criteria.
If you liked the article, I can prepare a sequel.
Successful development of the gentlemen, and if you have any questions, please in the comments or in the group OrientDB .

Source: https://habr.com/ru/post/214837/


All Articles