圖數據庫-Neo4j-初探

圖數據庫-Neo4j-初探

本次初探主要學習如何安裝Neo4j,以及Cypher的基本語法。node

1. 安裝Neo4j

  • Desktop版本git

    neo4j-desktopgithub

  • Server版本(Community版)算法

    比較建議安裝這個版本,由於Desktop版本的總是閃退,且要激活之類的。shell

    • 下載Neo4j數據庫數據庫

      neo4j-server-community緩存

    • 下載經常使用算法的插件app

      將下載下來的算法插件放入到$NEO4J_HOME/plugins文件夾下

    • Service版修改配置文件$NEO4J_HOME/conf/neo4j.conf

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      # 解決登入的時候報沒有受權的錯誤
      dbms.security.auth_enabled=false
      # 添加下載的算法插件
      dbms.security.procedures.unrestricted=apoc.*,algo.*
      apoc.import.file.enabled=true

      #增長頁緩存到至少4G,推薦20G:
      dbms.memory.pagecache.size=4g
      #JVM堆保存留內存從1G起,最大4G:
      dbms.memory.heap.initial_size=1g
      dbms.memory.heap.max_size=4g
    • 啓動/中止 (把server所在的路徑添加到系統的PATH)

      1
      2
      3
      4
      5
      6
      7
      # 建議將neo4j所在的路徑條件到系統$PATH當中,
      # export NEO4J_HOME="path-to-neo4j"
      $NEO4J_HOME/bin/neo4j start
      $NEO4J_HOME/bin/neo4j console
      $NEO4J_HOME/bin/neo4j stop
      $NEO4J_HOME/bin/neo4j start -u neo4j -p neo4j
      $NEO4J_HOME/bin/cypher-shell
      1
      CALL dbms.procedures() // 查看neo4j可用的進程,包括剛剛安裝的插件

2. Cypher基本語法

  • Nodes基本語法

    在Cypher裏面經過一對小括號表明一個節點

    • () 表明匹配任意一個節點
    • (node1) 表明匹配任意一個節點,並給它起了一個別名
    • (:Lable) 表明查詢一個類型的數據
    • (person:Lable) 表明查詢一個類型的數據,並給它起了一個別名
    • (person:Lable {name:」小王」}) 查詢某個類型下,節點屬性知足某個值的數據
    • (person:Lable {name:」小王」,age:23}) 節點的屬性能夠同時存在多個,是一個AND的關係
  • Relationship基本語法

    系用一對-組成,關係分有方向的進和出,若是是無方向就是進和出都查詢

    • —> 指向一個節點
    • -[role]-> 給關係加個別名
    • -[:acted_in]-> 訪問某一類關係
    • -[role:acted_in]-> 訪問某一類關係,並加了別名
    • -[role:acted_in {roles:[「neo」,」Hadoop「]}]->
  • 建立/刪除節點

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    // 插入一個Artist類別的節點,並且這個節點有一個屬性爲Name,值爲Lady Gaga
    CREATE (a:Artist {Name:"Lady Gaga"})

    // 建立並返回
    CREATE (a:Artist {Name:"Lady Gaga", Gemder:"Femal"}) return a

    // 一次性建立多個
    CREATE (a:Album { Name: "Killers"}), (b:Album { Name: "Fear of the Dark"})
    RETURN a, b

    CREATE (a:Album { Name: "Piece of Mind"})
    CREATE (b:Album { Name: "Somewhere in Time"})
    RETURN a, b

    // 刪除節點,若是這個節點和其餘節點有鏈接的話,不能單單刪除這個節點
    MATCH (a:Album {Name: "Killers"}) DELETE a

    // 一次性刪除多個節點
    MATCH (a:Artist {Name: "Iron Maiden"}), (b:Album {Name: "Powerslave"})
    DELETE a, b

    // 刪除全部節點
    MATCH (n) DELETE n
  • 建立/刪除關係

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    // 對Lady Gaga和專輯PieceOfMind之間建立一個released的關係
    MATCH (a:Artist), (b:Album)
    WHERE a.Name = "Lady Gaga" AND b.Name = "Piece of Mind"
    CREATE (a)-[r:RELEASED]->(b)
    RETURN r

    MATCH (a:Artist), (b:Album), (p:Person)
    WHERE a.Name = "Strapping Young Lad" AND b.Name = "Heavy as a Really Heavy Thing" AND p.Name = "Devin Townsend"
    CREATE (p)-[pr:PRODUCED]->(b), (p)-[pf:PERFORMED_ON]->(b), (p)-[pl:PLAYS_IN]->(a)
    RETURN a, b, p

    // 刪除指定的關係
    MATCH (:Artist)-[r:RELEASED]-(:Album)
    DELETE r

    MATCH (:Artist {Name: "Strapping Young Lad"})-[r:RELEASED]-(:Album {Name: "Heavy as a Really Heavy Thing"})
    DELETE r

    // 刪除全部的關係
    MATCH ()-[r:RELEASED]-()
    DELETE r

    // 清除全部節點和關係
    MATCH (n)
    OPTIONAL MATCH
    (n)-[r]-()
    DELETE n,r

    // 刪除整個數據庫
    MATCH (n) DETACH DELETE n

  • 建立/刪除約束

    SQL同樣,Neo4j數據庫支持對Noderelationship的屬性的UNIQUE約束

    1
    2
    3
    CREATE CONSTRAINT ON (a:Artist) ASSERT a.Name IS UNIQUE

    DROP CONSTRAINT ON (a:Artist) ASSERT a.Name IS UNIQUE
  • 建立/刪除索引

    1
    2
    3
    4
    5
    6
    CREATE INDEX ON :Album(Name)

    // View the schema
    :schema

    DROP INDEX ON :Album(Name)
  • 更新一個節點/邊

    1
    2
    MATCH (n:Person { name: "Andres" })
    SET n.name = "Taylor";
  • 篩選過濾

    1
    2
    3
    4
    5
    6
    7
    8
    9
    // WHERE
    MATCH (p1: Person)-[r:friend]->(p2: Person)
    WHERE p1.name=~"K.+" or p2.age=24 or "neo" in r.rels
    RETURN p1, r, p2

    // NOT
    MATCH (p:Person)-[:ACTED_IN]->(m)
    WHERE NOT (p)-[:DIRECTED]->()
    RETURN p, m
  • 結果集返回

    1
    2
    3
    4
    5
    MATCH (p:Person)
    RETURN p, p.name AS name, upper(p.name), coalesce(p.nickname,"n/a") AS nickname, { name: p.name, label:head(labels(p))} AS person

    MATCH (n)
    RETURN DISTINCT n.name;
  • 聚合函數

    Cypher支持count, sum, avg, min, max

    聚合的時候null會被跳過

    count 語法 支持 count( distinct role )

    1
    2
    3
    4
    5
    6
    MATCH (actor:Person)-[:ACTED_IN]->(movie:Movie)<-[:DIRECTED]-(director:Person)
    RETURN actor,director,count(*) AS collaborations

    // 收集聚合結果
    MATCH (m:Movie)<-[:ACTED_IN]-(a:Person)
    RETURN m.title AS movie, collect(a.name) AS cast, count(*) AS actors
  • 排序和分頁

    1
    2
    3
    MATCH (a:Person)-[:ACTED_IN]->(m:Movie)
    RETURN a, count(*) AS appearances
    ORDER BY appearances DESC SKIP 3 LIMIT 10;
  • Union 聯合

    1
    2
    3
    4
    5
    MATCH (actor:Person)-[r:ACTED_IN]->(movie:Movie)
    RETURN actor.name AS name, type(r) AS acted_in, movie.title AS title
    UNION (ALL)
    MATCH (director:Person)-[r:DIRECTED]->(movie:Movie)
    RETURN director.name AS name, type(r) AS acted_in, movie.title AS title
  • With語句

    with語句給Cypher提供了強大的pipeline能力,能夠一個或者query的輸出,或者下一個query的輸入 和return語句很是相似,惟一不一樣的是,with的每個結果,必須使用別名標識。

    使用with咱們能夠在查詢結果裏面在繼續嵌套查詢。

    1
    2
    3
    4
    MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
    WITH p, count(*) AS appearances, COLLECT(m.Title) AS movies
    WHERE appearances > 1
    RETURN p.name, appearances, movies

    有點相似SQL中的having,這裏是with + where兩個一塊兒來實現的。

  • 查詢最短路徑

    1
    2
    MATCH (ms:Person { name: "Node A" }),(cs:Person { name:"Node B" }), p = shortestPath((ms)-[r:Follow]-(cs)) 
    RETURN p;
  • 加載數據

    Cypher Neo4j Couldn’t load the external resource

    neo4j初探

    加載存在本地server上的數據,會在路徑前面自動加個前綴 /path-to-neo4j/neo4j-community-3.4.5/import,即Server對應所在的路徑下的import

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // 加載address
    LOAD CSV WITH HEADERS FROM "file:///data/addresses.csv" AS csvLine
    CREATE (p:Person {id: toInt(csvLine.id), email: csvLine.address })


    // 加載email
    LOAD CSV WITH HEADERS FROM "file:///data/emails.csv" AS csvLine
    CREATE (e:Email {id: toInt(csvLine.id), time: csvLine.time, content: csvLine.content })


    // 建立收發關係
    USING PERIODIC COMMIT 500 // 分段加載
    LOAD CSV WITH HEADERS FROM "file:///data/relations.csv" AS csvLine
    MATCH (p1:Person {id: toInt(csvLine.fromId)}),(e:Email { id: toInt(csvLine.emailId)}),(p2:Person{ id: toInt(csvLine.toId)})
    CREATE UNIQUE (p1)-[:FROM]->(e)
    CREATE(e)-[:TO]->(p2)

    若是須要導入其餘地方的,可使用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    LOAD CSV FROM "https://path-to-csv" AS csvLine
    CREATE (:Genre {GenreId: csvLine[0], Name: csvLine[1]})

    // 使用csv中的header
    LOAD CSV WITH HEADERS FROM "https://path-to-csv" AS csvLine
    CREATE (:Genre {GenreId: csvLine.Id, Name: csvLine.Track, Length: csvLine.Length})

    // 自定義csv文件中的分隔符
    LOAD CSV WITH HEADERS FROM "https://path-to-csv" AS csvLine FIELDTERMINATOR ";"
  • 使用 neo4j-import 導入數據

    使用neo4j-import導入數據

    • 使用條件
      • 須要先關閉neo4j
      • 沒法再原有的數據庫添加,只能從新生成一個數據庫
      • 導入文件格式爲csv
    • 參數
      • —into:數據庫名稱
      • —bad-tolerance:能容忍的錯誤數據條數(即超過指定條數程序直接掛掉),默認1000
      • —multiline-fields:是否容許多行插入(即有些換行的數據也可讀取)
      • —nodes:插入節點
      • —relationships:插入關係
      • 更多參數可容許命令bin/neo4j-import
    1
    bin/neo4j-import --multiline-fields=true --bad-tolerance=1000000 --into graph.db --id-type string --nodes:person node.csv  --relationships:related relation_header.csv,relation.csv

    運行完後,將生成的graph.db放入data/databases,覆蓋原有數據庫,啓動運行便可

3. References

原文地址:https://chenson.cc/2018/08/17/%E5%9B%BE%E6%95%B0%E6%8D%AE%E5%BA%93-Neo4j-%E5%88%9D%E6%8E%A2/
相關文章
相關標籤/搜索