Basic operations of Neo4j graph database

Neo4j


Command line window neo4j.bat console

Import files in rdf format

:GET /rdf/ping
CALL n10s.graphconfig.init(); //初始化
call n10s.rdf.import.fetch("file:///F:\\wow.rdf",'Turtle')// 导入注意斜杠///(本地文件需要用这个)

Clear all data

What should be noted here is that because there is no isolated relationship, if you want to delete a node with a relationship, you need to delete all the relationships of the node at the same time.

That is, if you want to delete nodes (a)-[d]-(b)-[e]-(c)a and b in path , you need to delete relationships d and e at the same time.

Therefore, if you want to clear the database, that is, delete all nodes and relationships, you can first use to MATCHfind all the nodes, then use OPTIONAL MATCHto query whether the nodes have relationships, and finally delete them all.

MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n,r

return

Because CREATEthe command can be used without the command at RETURNthe same time, if it is used RETURN, it will return to Graphthe interface.

CQL

Neo4j's Cypher language is built for processing graph data, and CQL stands for Cypher Query Language. Like Oracle database has query language SQL, Neo4j has CQL as query language.

nodes and relationships

Cypher uses a pair of parentheses ()to represent nodes, such as (n:角色)represents a 角色node, nwhich is a variable name, used to access this node when the command is executed n, and cannot be used after the command is executed. At the same time, it alone ()represents an anonymous node, and when matching, it means matching all nodes.

in relationship

  • --Represents a directionless relationship
  • -->Indicates a directional relationship
  • -[r]->Then assign a variable name to the relationship to facilitate operations on this relationship.
  • -[r:配偶]->The matching relationship is of 配偶type
Add, delete, modify and check
//建立结点
create(n:Person{name:"小江",sex:"男"}) return n                 
//建立关系 (小江是小王的爸爸)
MATCH(a:Person{name:"小江"}),(b:Person{name:"小王"})Merge(a)-[r:爸爸]->(b)
//创建节点的时候就建立好关系                                                                  
CREATE (a:Person {name:'苗同学'})-[r:朋友]->(b:Person {name:'叶同学'})
//修改  把name是小王的name改成小江
Match (a:Person{name:"小王"})set a.name="小江"
//搜索 信息是大学生的,返回改结点
match(a:Person{info:"大学生"}) return a
//在一个结点中建立新的属性,在名字为小陈这个结点添加info属性
match (a:Person{name:"小陈"}) set a.info="小学生"
//删除结点的某个属性
match (a:Person{name:"小陈"}) remove a.info
//删除关系
match(a:Person{name:"小陈"})-[r:`爸爸`]-(b:Person{name:"江"}) delete r
//删除结点  To delete this node, you must first delete its relationships.删除结点前要先删除关系
match (a:Person{name:"小江"}) delete a                                                  

mergecreateThe difference between

It can be considered MERGE = MATCH + CREATEthat, therefore, when adding graph data, if you want to skip existing nodes or relationships, use the MERGEcommand. If you don’t care about duplicate nodes or relationships, use CREATEthe command.

match statement
  1. Match nodes based on label

    //匹配所有角色节点
    match (n:角色) return n
    
  2. Match nodes based on labels and attributes

// 匹配 name 为 郭靖 的 角色 节点
match (n:角色{name:'郭靖'}) return n
delete

There are two deletion methods in Neo4j, DELETEand REMOVE. DELETEUsed to delete nodes and relationships, REMOVEused to delete labels and attributes of nodes and relationships. Both need to cooperate MATCH, match the content first, and then perform the operation.

Delete node

To delete a node, you need to delete all edges related to the node, which is consistent with graph theory - there is no edge without a node. Therefore, to delete the node Jinlun Dharma King, you first need to find the node and its relationship, and then delete it.

match(n:`角色`{name:"王重阳"})-[r]-() delete n,r
Import Data

Insert image description here

load csv with headers from 'file:///data\\射雕三部曲.csv' as line
match (book:作品),(person:角色),(skill:武功)
    where
        book.name in split(line.作品, ',') and
        person.name = line.人物 and
        skill.name in split(line.武功, ',')
optional match (father:角色)
    where
        father.name = line.父
optional match (mother:角色)
    where
        mother.name = line.母
optional match (spouse:角色)
    where
        spouse.name = line.配偶
optional match (sect:门派)
    where
        sect.name = line.门派
optional match (children:角色)
    where
        children.name in split(line.子女, ',')
optional match (master:角色)
    where
        master.name in split(line.师傅, ',')
merge (person)-[:所在作品]-(book)
merge (person)-[:师傅]-(master)
merge (person)-[:武功]-(skill)
merge (person)-[:父]-(father)
merge (person)-[:母]-(mother)
merge (person)-[:配偶]->(spouse)
merge (person)-[:所在门派]-(sect)
merge (person)-[:子女]-(children)
Current issues

1. The most important nutrients for high blood pressure are potassium and sodium, but potassium was not included in our previous data.

It is possible to build a map using the two data separately, but not together.

My idea is to first create a diagram of raw materials and nutrients, and then create a diagram of vegetables and raw materials. If the raw materials are available, connect them to them.

You can create a category, and then if it is rich, it can be placed in the potassium element. If there is a small amount, it can be placed in the sodium element. If it is other, the content should be written in the relationship.

menu
load csv with headers from 'file:///data\\export.csv' as line
merge(n:食物{name:line.食物名,kind:line.分类}) return n
create(n:营养素{name:"钠"}) return n  //建立营养素
solved problem

1. How to write the new version of Python to connect to the graph database

A lot of things on the Internet are like this, but I will report an error

graph = Graph("http://localhost:7474", username="root", password='123456')

The reason is the python version problem. The following is the correct way to write it.

graph = Graph('http://localhost:7474', auth=("neo4j", "123456"))

2.报错:UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xaa in position

It's a matter of encoding method

with open("D:\\neo4j-community-4.4.14-windows\\neo4j-community-4.4.14\\import\\data\\export.csv", 'r',encoding='utf-8') as f:

Finally, just add encoding='utf-8'

match (a:nutrients)-[:富含]-(b:recipe_matrial{name:"生菜"})-[:include]-(c:recipe) return a,b,c
match (a:recipe{name:"蚂蚁上树"})-[:include]-(b:recipe_matrial)-[:have]-(c) return a,b,c
match(a:食材{name:{foodName}})-[:推荐食用]-(b:疾病) return a,b

Find all the relationships of Bobo Chicken, but only one level

match (n:recipe{name:"蚂蚁上树"})--(b) return n,b

two-tier relationship

match (n:recipe{name:"蚂蚁上树"})--(b)-->(c) return n,b,c
match n=(x:recipe)-[*1..2]-() where x.name="钵钵鸡" return n

If you look for nutrients directly through vegetables, there will be no sodium content.

If you look for ingredients through dishes and then nutrients, the two data are separate, and some ingredients are not available in the other data.

        if question_type == 'disease_cause':
            sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.cause".format(i) for i in entities]

Guess you like

Origin blog.csdn.net/Aure219/article/details/131956722