当前位置:网站首页>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/

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

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 :
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 :

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 :

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 :

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 .

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

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/
边栏推荐
- WebRTC系列--计算帧率及帧间隔
- 了解图数据库neo4j(一)
- 【Redis学习12】分布式缓存之哨兵机制,分片集群
- How do you view the multi runtime architecture of dapr and layotto?
- 微信小程序之获取用户基本信息
- Detailed introduction to MySQL basic data types
- [texstudio] [1] fractional dfrac, tfrac undefined control square error
- GLCC 首届编程夏令营|欢迎报名 Layotto、KusionStack、Nydus、Kata Containers!
- What's wrong with Android development today? Is the interview question I asked so difficult?
- 解决apscheduler报错:Run time of job …… next run at: ……)” was missed by
猜你喜欢

MySQL基础 数据库创建基础

The integrated monitoring system of ankerui distribution room realizes the online monitoring of the environment in the distribution room and ensures the safe operation of the equipment in the distribu

Android 开发面试心得总结,实录整理(必看)

了解图数据库neo4j(二)

Attachment 17: limited articles on network program interpretation

附十七章 网络程序解读限定文章

Que pensez - vous des architectures Multi - temps comme DAPR et layotto?

使用Canvas画出多个多边形Polygon

Android常见原理性面试题(初步整理)~
![[code comment] Doxygen](/img/db/0017e30ef8ff5d9d71a9d02a7ed7cf.png)
[code comment] Doxygen
随机推荐
Draw multiple polygons using canvas
three.js学习笔记(十五)——着色器图案
MySQL basic knowledge
MySQL基础 数据类型精讲
Wechat applet to obtain basic user information
[texstudio] [2] general picture and table presentation
MySQL basic addition, deletion, modification and query exercise
The fire door monitoring system carries out 24-hour real-time automatic inspection on the status of the fire door
2022-2028 global leisure special sand industry research and trend analysis report
GLCC's first programming summer camp welcome to sign up for layotto, kusionstack, Nydus, Kata containers!
[texstudio] [3] relatively complete paper typesetting template and bib file reference method
[code comment] Doxygen
HVV blue team points north
MySQL基础 基础认知
QT开发--串口助手的编写
Open application for "cloud native technology application and practice" demonstration course project in Colleges and Universities
How do you view the multi runtime architecture of dapr and layotto?
Esp32 learning notes [WiFi network] - 03tcp server server connection
FreeRtos信号量复习
Application of acrel bus intelligent lighting control system in hospital