Neo4J自学

一:删除所有节点

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

查询指定节点

Match (n: Intersection) where n.name =6011018263 return n

创建节点与关系

CREATE (I_1540027738:Intersection {name:'1540027738'})-[Road:R_140582515]->(I_5257715003:Intersection {name:'5257715003'})
CREATE
(I_1540027738: Intersection{name: 1540027738})
CREATE
(I_5257715003: Intersection{name: 5257715003})
CREATE
(I_5257346282: Intersection{name: 5257346282})
CREATE
(I_5257714959: Intersection{name: 5257714959})

MATCH (I_1540027738: Intersection{name: 1540027738}),(I_5257715003: Intersection{name: 5257715003})
CREATE
	(I_1540027738)-[Road:R_140582515]->(I_5257715003)

MATCH (I_5257715003: Intersection{name: 5257715003}),(I_5257346282: Intersection{name: 5257346282})
CREATE
	(I_5257715003)-[Road:R_140582515]->(I_5257346282)

MATCH (I_5257346282: Intersection{name: 5257346282}),(I_5257714959: Intersection{name: 5257714959})
CREATE
	(I_5257346282)-[Road:R_140582515]->(I_5257714959)

用Python操作Neo4J

from py2neo import Graph
from xml.dom.minidom import parse
import xml.dom.minidom
graph = Graph("bolt://127.0.0.1:7687", username="neo4j", password="1026")
n=graph.nodes.match("Intersection")
for i in n:
    print(i)
graph.run('MATCH (I_5257346282: Intersection{name: 5257346282}),(I_5257714959: Intersection{name: 5257714959}) CREATE(I_5257346282)-[Road:R_140582515]->(I_5257714959)')

根据openstreetmap数据在Neo4J中插入路线

#说明 haikou_map.osm数据来自于openstreetmap
from pygeohash import encode, decode
import plotly
import numpy as np
import pandas as pd
import math
from matplotlib.path import Path
import numpy as np
import plotly.offline as of
import plotly.graph_objs as go
import chart_studio.plotly as py
import numpy as np
import pandas as pd
import folium
import webbrowser
from folium.plugins import HeatMap
import datetime
import time
import pymysql.cursors
import decimal
import geohash
from py2neo import Graph
from xml.dom.minidom import parse
import xml.dom.minidom
graph = Graph("bolt://127.0.0.1:7687", username="neo4j", password="1026")
#dom解析xml
DOMTree = xml.dom.minidom.parse("haikou_map.osm")
#返回文档的根节点
root1 = DOMTree.documentElement
#观察新闻发现,内容都在Event元素下
ContentNodes = root1.getElementsByTagName("way")
#定义一个字符串,保存解析出的数据
content = ""
#遍历所有的Event
k=1
for i in range(len(ContentNodes)):
    #获取第i个Event下的子节点
    wayNodes = ContentNodes[i]._attrs['id']._value #这条路的ID
    # print(wayNodes)
    waynd=ContentNodes[i].getElementsByTagName("nd")
    waytag = ContentNodes[i].getElementsByTagName("tag") #这条路的名称
    wayJiedian=[]
    for j in range(len(waynd)):
        wayJiedia=waynd[j]._attrs['ref']._value #存放这条路的节点
        wayJiedian.append(wayJiedia)#存放这条路的节点
    # print(wayJiedian)
    s=0
    while s < len(wayJiedian): #第一步创建节点
        qucf='Match (n: Intersection) where n.name ='+str(wayJiedian[s])+' return n'#如果已经存在就不插入了
        tmp_df = pd.DataFrame(graph.run(qucf)).empty #判断是否为空
        if tmp_df==True:
            graph.run('CREATE (I_' + str(wayJiedian[s]) + ': Intersection' + '{name: ' + str(wayJiedian[s]) + '})')
        else:
            print('***************')
            print('***'+str(wayJiedian[s])+'是重复****')
            print('***************')
        s+=1
    n=0
    while n < (len(wayJiedian)-1):   #第二步创建节点间的关系
        graph.run('MATCH(I_' + str(wayJiedian[n]) + ': Intersection' + '{name: ' + str(wayJiedian[n]) + '}),(I_' + str(
            wayJiedian[n + 1]) + ': Intersection' + '{name:' + str(wayJiedian[n + 1]) + '}) CREATE' + '(I_' + str(
            wayJiedian[n]) + ') - [Road: R_' + str(wayNodes) + ']->(I_' + str(wayJiedian[n + 1]) + ')')
        n+=1
    print('第'+str(k)+'个路段已创建')
    k+=1
    # for k in range(len(waytag)):
    #      waytagNodes=waytag[k]._attrs['k']._value #'highway'
    #      if waytagNodes== "name":
    #         waytagNodes1 = waytag[k]._attrs['v']._value #'茉莉路'
    #         print(waytagNodes1)
    # print(wayJiedian)



# Match (n: Intersection) where n ='I_5267204253' return n
# Match (n: Intersection) where n.name =5267204224 return n
## 联合查询

```sql
MATCH p=()-[r:R_215530842]->() RETURN p
UNION ALL
MATCH p=()-[r:R_215562722]->() RETURN p
UNION ALL
MATCH p=()-[r:R_215560399]->() RETURN p
UNION ALL
MATCH p=()-[r:R_140582515]->() RETURN p
UNION ALL
MATCH p=()-[r:R_215569614]->() RETURN p
UNION ALL
MATCH p=()-[r:R_215571357]->() RETURN p
UNION ALL
MATCH p=()-[r:R_215566172]->() RETURN p
UNION ALL
MATCH p=()-[r:R_211621149]->() RETURN p

你可能感兴趣的:(个人学习)