一贝润、Neo4j安裝及服務(wù)啟動(dòng)
Neo4j 是目前最流行的圖形數(shù)據(jù)庫绊茧,支持完整的事務(wù),圖形數(shù)據(jù)庫也就意味著它的數(shù)據(jù)并非保存在表或集合中打掘,而是保存為節(jié)點(diǎn)以及節(jié)點(diǎn)之間的關(guān)系华畏。圖是由頂點(diǎn)(Vertex),邊(Edge)和屬性(Property)組成的尊蚁,頂點(diǎn)和邊都可以設(shè)置屬性亡笑,頂點(diǎn)也稱作節(jié)點(diǎn),邊也稱作關(guān)系横朋,每個(gè)節(jié)點(diǎn)和關(guān)系都可以有一個(gè)或多個(gè)屬性仑乌。
1、Neo4j安裝
[Windows平臺(tái)安裝] (https://www.cnblogs.com/ljhdo/archive/2017/05/19/5521577.html)
[Linux平臺(tái)安裝] (https://blog.csdn.net/qq_21383435/article/details/79557819)
2、啟動(dòng)配置
Neo4j應(yīng)用程序有如下主要的目錄結(jié)構(gòu):
bin/ -->Neo4j可執(zhí)行程序
cypher-shell --> 操作Cypher的程序
neo4j --> 服務(wù)端程序
neo4j-admin -->管理工具晰甚,設(shè)置命令衙传,數(shù)據(jù)恢復(fù)等功能。
neo4j-backup -->熱備份工具(只有企業(yè)版支持)
neo4j-import -->數(shù)據(jù)導(dǎo)入導(dǎo)出工具
conf/ -->配置文件目錄
neo4j.conf -->控制Neo4j啟動(dòng)的配置文件
data/ -->數(shù)據(jù)庫文件目錄
databases/
graph.db -->核心數(shù)據(jù)庫文件
plugins/ -->Neo4j的插件
neo4j.conf的常用配置參數(shù)如下
參數(shù) | 描述 |
---|---|
-dbms.active_database | 數(shù)據(jù)庫名字厕九,默認(rèn)graph.db |
-dbms.directories.data | 數(shù)據(jù)庫路徑(會(huì)自動(dòng)補(bǔ)上databases) |
-dbms.memory.heap.initial_size | jvm初始堆內(nèi)存 |
-dbms.memory.heap.max_size | jvm最大堆內(nèi)存 |
-dbms.memory.pagecache.size | 類似緩存粪牲,越大越好 |
-dbms.connectors.default_listen_address | 可以遠(yuǎn)程訪問neo4j數(shù)據(jù)庫的ip,0.0.0.0表示不限制 |
3、服務(wù)開啟與關(guān)閉
(1)Windows下配置
使用WIN+R運(yùn)行cmd命令執(zhí)行進(jìn)入bin目錄下執(zhí)行
neo4j install-service # 安裝為服務(wù)
neo4j uninstall-service # 卸載服務(wù)
執(zhí)行如下命令開啟止剖、關(guān)閉腺阳、重啟、狀態(tài)查詢
neo4j start
neo4j stop
neo4j restart
neo4j status
(2)Linux下配置
進(jìn)入安裝目錄下穿香,執(zhí)行如下命令開啟亭引、關(guān)閉、重啟皮获、狀態(tài)查詢
neo4j start
neo4j stop
neo4j restart
neo4j status
4焙蚓、打開Neo4j集成的瀏覽器
Neo4j服務(wù)器具有一個(gè)集成的瀏覽器,在一個(gè)運(yùn)行的服務(wù)器實(shí)例上訪問 “http://localhost:7474/”洒宝,打開瀏覽器购公,顯示啟動(dòng)頁面
默認(rèn)的用戶是neo4j,其默認(rèn)的密碼是:neo4j雁歌,第一次成功登陸到Neo4j服務(wù)器之后宏浩,需要重置密碼。
二靠瞎、py2neo安裝
py2neo 是用來對(duì)接 Neo4j的 Python 庫
pip install py2neo
三比庄、使用py2neo操作neo4j數(shù)據(jù)庫
連接neo4j數(shù)據(jù)庫
from py2neo import Graph,Node,Relationship
# 連接neo4j數(shù)據(jù)庫,輸入地址乏盐、用戶名佳窑、密碼
graph = Graph('http://localhost:7474',username='neo4j',password='test')
1、插入數(shù)據(jù)
創(chuàng)建了兩個(gè)Node以及兩者之間的Relationship父能。在這里有必要提一下神凑,如果建立關(guān)系的時(shí)候,起始節(jié)點(diǎn)或者結(jié)束節(jié)點(diǎn)不存在何吝,則在建立關(guān)系的同時(shí)建立這個(gè)節(jié)點(diǎn)溉委。
# 可以一個(gè)一個(gè)創(chuàng)建
a = Node('Person',name='bubu')
graph.create(a)
b = Node('Person',name='kaka')
graph.create(b)
r = Relationship(a,'KNOWS',b)
graph.create(r)
'''
# 也可以一次性創(chuàng)建
s = a | b | r
graph.create(s)
'''
可以通過 setdefault() 方法賦值默認(rèn)屬性(當(dāng)給location賦值了屬性,則它會(huì)把默認(rèn)的屬性覆蓋)
c = Node('City')
c.setdefault('location', '北京')
2岔霸、查詢數(shù)據(jù)
(1)對(duì)Node的查詢
方法一:使用CQL查詢
直接也是最簡(jiǎn)單的方式薛躬,通過數(shù)據(jù)庫語言進(jìn)行尋找,不需要考慮python語法
# graph查詢
graph.run("MATCH (n:leafCategory) RETURN n LIMIT 25").data() # list型
graph.run("MATCH (n:leafCategory) RETURN n LIMIT 25").to_data_frame() # dataframe型
graph.run("MATCH (n:leafCategory) RETURN n LIMIT 25").to_table() # table
方法二:使用NodeMatcher查詢
如果要查詢單個(gè)節(jié)點(diǎn)的話呆细,可以使用 first() 方法
from py2neo import NodeMatcher
matcher = NodeMatcher(graph)
matcher.match("Person", name="Kevin").first()
matcher.match(“Person”, name__not="Rick Astley").first()
描述 | 后綴 | 示例 |
---|---|---|
表達(dá)相等 | __exact | matcher.match(“Person”, name__exact="Kevin Bacon") |
表達(dá)不相等 | __not | matcher.match(“Person”, name__not="Rick Astley") |
表達(dá)大于 | __gt | matcher.match(“Person”, born__gt=1985) |
表達(dá)大于等于 | __gte | matcher.match(“Person”, born__gte=1965) |
表達(dá)小于 | __lt | matcher.match(“Person”, born__lt=1965) |
表達(dá)小于等于 | __lte | matcher.match(“Person”, born__lte=1965) |
以XX開頭 | __startswith | matcher.match(“Person”, name__startswith="Kevin") |
以XX結(jié)尾 | __endswith | matcher.match(“Person”, name__endswith="Smith") |
包含關(guān)系 | __contains | matcher.match(“Person”, name__contains="James") |
另外也可以使用 NodeMatch.where()進(jìn)行更復(fù)雜的查詢型宝,例如下面的查詢是查出以Kevin開頭的 Person Node(用了正則表達(dá)式匹配查詢)
from py2neo import NodeMatcher
matcher = NodeMatcher(graph)
persons = matcher.match("Person").where("_.name =~ 'Kevin.*' ").order_by("_.name", "max(_.a, _.b)").limit(3)
print(list(persons))
通過len()方法八匠,可統(tǒng)計(jì)查找到節(jié)點(diǎn)的個(gè)數(shù)
from py2neo import NodeMatcher
matcher = NodeMatcher(graph)
nodeNum= len(matcher.match("Person").where('_.name =~ "Kevin.*" '))
print(nodeNum)
(2)對(duì)Relationship的查詢
方法一:使用CQL查詢
略
方法二:使用RelationshipMatcher查詢
from py2neo import RelationshipMatcher
relMatch = RelationshipMatcher(graph)
relList = list(relMatch.match())
for i in relList:
print(i) # 查找圖數(shù)據(jù)庫中所有關(guān)系
語法:match(nodes=None, r_type=None, **properties)
分別對(duì)應(yīng)節(jié)點(diǎn)、關(guān)系名趴酣、屬性
from py2neo import RelationshipMatcher
relMatch = RelationshipMatcher(graph)
relList = list(relMatch.match(r_type="女兒"))
for i in relList:
print(i)