当前位置:网站首页>Learn about graph database neo4j (I)

Learn about graph database neo4j (I)

2022-06-09 09:29:00 Great for the rest of my life

Introduce

neo4j Is a graphic database, also known as Knowledge map , The data of the knowledge map contains entities 、 attribute 、 Relationship . Knowledge map is to form a knowledge map through the relevance of different knowledge A network of knowledge structures . At present AI Popular computer graphics in the field 、 Speech recognition even NLP, All of them are AI Perception of , real AI Cognitive ability , It depends on the knowledge map .

At present, the application of knowledge map is mainly in search 、 Intelligent q&a 、 Recommendation system and other aspects .

For example, we often recommend people you may know when using social software 、 People of common concern 、 Your friend also pays attention to him And so on , This is called... In the social field Focus on the model , Let's try to use neo4j To achieve it .

Neo4J brief introduction

Graphic databases are also known as Graphic database management system (GDBMS), At present, there are relatively mature graph databases Neo4j、OracleNoSQL、OrientDB、HypherGraphDB and GraphBase etc.

among Neo4j Is based on Java Graphic database written in , It stores information in the form of nodes and relationships , And on this basis, provide a user-friendly visual demonstration ,Neo4j The main components of graphic database are :

  • node : That is, entity , Used to denote an individual that exists alone , Nodes typically contain multiple attributes
  • Relationship : That is to say “ edge ”, The two nodes can only be connected by relationship , Each relationship also has its own noun , Can pass Cypher Retrieve the relationship name to find all nodes with that relationship
  • attribute : It can be regarded as an extended description of a node ,id、 These names also belong to the attributes of the node , Detailed attributes need to be displayed through the Text Tag to view
  • label : That is, grouping ,Neo4j When establishing nodes or relationships, it is required to group in advance
  • Data browser :Neo4j Own visual interface , Used to provide user execution Cypher Query commands and view output text and graphics

Neo4J install

In my previous articles, I used linux Installed neo4j, You can draw lessons from :Linux Install one online Neo4j Graph database

Neo4J Basic operation

Neo4J The visualization page has been provided after installation , And you can directly execute statements to manipulate data and view database relationships, namely labels , It's very easy to use .

Visit after installation neo4j Of web page :http:// Yours ip Address :7474/browser/

 Insert picture description here

The main page has A window for executing commands , The left navigation bar shows the selected database, the total number of nodes in the database, and the Labels and relationships , You can clearly see what tags and relationships our database has

Cypher query language

Cypher yes Neo4J The declarative Graphic Query Language , Allows users to write traversal code without having to write graphical structures , You can query the graphic data efficiently .Cypher Is designed for similar purposes SQL, It's suitable for developers and doing point-to-point mode on database (ad-hoc) Professional operators of inquiry . Its capabilities include : - establish 、 to update 、 Delete nodes and relationships - Query and modify nodes and relationships through pattern matching - Manage indexes and constraints, etc .

Common commands are as follows :

#  Delete all previous nodes and relationships ,MATCH It's a matching operation ,() Represents a node ,n Is an identifier. 
MATCH (n) DETACH DELETE n
 
#  Create a label for Person The node of , Node has one name attribute , The property value is 'John'
CREATE (n:Person{
    name:'John'}) RETURN n
 
#  from a To b Set up FRIENDS Relationship , The relationship has a since attribute , The property value is 2001
MATCH (a:Person{
    name:'Liz'}),(b:Person{
    name:'Mike'})MERGE (a)-[:FRIENDS{
    since:2001}]->b
 
#  The query in Boston All born Person
MATCH (a:Person)-[:BORN_IN]->(b:location{
    city:'Boston'}) RETURN a,b
 
#  Query all nodes with external relations 
MATCH (a)-->() RETURN a
 
#  Query all nodes with relationships 
MATH (a)--() RETURN a
 
#  Query all nodes with external relations , And return the... Of the node name Attribute values and relationship types 
MATCH (a)-[r]->() RETURN a.name, type(r)
 
#  to a Set a node age attribute , The property value is 34
MATCH (a:Person{
    name:'Liz'}) SET a.age = 34
 
#  Delete a Node test attribute 
MATCH ... REMOVE a.test
 
#  Delete a node 
MATCH ... DELETE a

#  Just delete the relationship 
match (n:Person{
    name:" Long Aotian "})<-[r:BIGBROTHER]-(m:Person{
    name:" Kamo "}) DELETE  r

#  Add relationship attributes 
MATCH p=({
    name:' Long er di '})-[r:BIGBROTHER ]->() SET r={
    since:"2017-01-02"}  RETURN p;   

neo4j actual combat

Let's use neo4j Implement a relational model for social networking

1. Clean up the database

Initialize the database , Ensure that our operation properties are not affected , Execute the following commands in the run box

MATCH (n) DETACH DELETE n

In this order MATCH For matching , parentheses () Write matching nodes in ,n For identifier ,DETACH DELETE For operation .
When combined, the matching identifier is n To delete

 Insert picture description here
As shown in the figure above, execute the command to clear successfully

2. Create a character

The room is ready , Then it's time for our characters to move in , First create our first guest : Long Aotian

CREATE (n:Person {
    name:' Long Aotian '}) RETURN n

CREATE Is the creation operation ,Person Is the label , Represents the type of node . Curly braces {} representative Properties of a node , Properties are similar to Python Dictionary .
The meaning of this statement is to create a label as Person The node of , This node has a name attribute , The attribute value is long Aotian .

The created effect is as follows :
 Insert picture description here

Seeing us, brother Aotian came first , My little brother hasn't arrived yet , How inappropriate , Hurry to create brother Tian's younger brother :

CREATE (n:Person {
    name:' Kamo '}) RETURN n;
CREATE (n:Person {
    name:' Long er di '}) RETURN n;
CREATE (n:Person {
    name:' Third brother of the Dragon '}) RETURN n;
CREATE (n:Person {
    name:' The fourth brother of the Dragon '}) RETURN n;
CREATE (n:Person {
    name:' Dragon five younger brothers '}) RETURN n;

After the creation is successful, let's take a look :

 Insert picture description here

Little brother has , But now they have nothing to do with brother Tian , This is not acceptable. , There is no reason why my younger brother doesn't recognize my elder brother , Then we should let the younger brother establish a relationship with the elder brother .

Create a relationship between brother and brother :

MATCH (a:Person {
    name:' Kamo '}), 
      (b:Person {
    name:' Long Aotian '}) 
MERGE (a)-[:BIGBROTHER]->(b)

After the execution, let's see if brother long has paid a visit to brother :

 Insert picture description here
According to the relationship diagram, we can see that the relationship between the younger brother and the elder brother points to long Aotian , So brother Aotian has a little brother , Next, I will meet other younger brothers, long Aotian , The order is as follows

MATCH (a:Person {
    name:' Long er di '}), (b:Person {
    name:' Long Aotian '}) MERGE (a)-[:BIGBROTHER]->(b);
MATCH (a:Person {
    name:' Third brother of the Dragon '}), (b:Person {
    name:' Long Aotian '}) MERGE (a)-[:BIGBROTHER]->(b);
MATCH (a:Person {
    name:' The fourth brother of the Dragon '}), (b:Person {
    name:' Long Aotian '}) MERGE (a)-[:BIGBROTHER]->(b);
MATCH (a:Person {
    name:' Dragon five younger brothers '}), (b:Person {
    name:' Long Aotian '}) MERGE (a)-[:BIGBROTHER]->(b);

Look at the effect :

 Insert picture description here
So long Aotian looks like a big brother , a myriad of stars surround the moon , But we want to know when the younger brothers followed long Aotian , It's convenient to row seats later , So you need to add attributes to the relationship , as follows :

CREATE (n:Person {
    name:' Six dragon brothers '}) RETURN n;
MATCH (a:Person {
    name:' Six dragon brothers '}), 
      (b:Person {
    name:' Long Aotian '}) 
MERGE (a)-[:BIGBROTHER {
    since:"2022-01-01"}]->(b)

At this time, a new dragon six younger brothers , And you can see that the new dragon sixth brother has the attribute of time , In this way, the sixth brother of the dragon can say that you still play with mud when I go to the rain with Lao Feng .

 Insert picture description here

At this time, the other younger brothers were not happy , Our first boss , Why do you have Time nameplate It's the new one , In this way, it is necessary to solve the problem that brothers had no identity before , But how did I add attributes to my younger brother before , The sixth brother of the dragon can't be before his five brothers , So the following is how to modify the attributes of the relationship

MATCH p=({
    name:' Kamo '})-[r:BIGBROTHER ]->() SET r={
    since:"2017-01-01"}  RETURN p    

 Insert picture description here
Long Xiaodi was very happy to get the time plate , And the time is also earlier than the sixth brother of the dragon , He snorted to the sixth brother of the dragon with disdain , The other brothers saw it and asked for it , We also give them time nameplates

MATCH p=({
    name:' Long er di '})-[r:BIGBROTHER ]->() SET r={
    since:"2017-01-02"}  RETURN p;   
MATCH p=({
    name:' Third brother of the Dragon '})-[r:BIGBROTHER ]->() SET r={
    since:"2017-01-03"}  RETURN p;   
MATCH p=({
    name:' The fourth brother of the Dragon '})-[r:BIGBROTHER ]->() SET r={
    since:"2017-01-04"}  RETURN p;   
MATCH p=({
    name:' Dragon five younger brothers '})-[r:BIGBROTHER ]->() SET r={
    since:"2017-01-05"}  RETURN p;   

The basic team of brother Aotian has had contradictions and eliminated them , It's time to go out , But where should I go ?

At present, there are only character labels in our atlas , Brother Aotian can't develop the plot even if he wants to go shopping , Then we should create the label and attribute of the location and establish the relationship with brother Aotian , Let's move on to the next chapter , Brother Aotian has to rest .

If the students here want to practice, they don't have their own neo4j You can use my , The address is here :http://110.40.220.41:7474/browser/

原网站

版权声明
本文为[Great for the rest of my life]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090851581605.html