<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Neo4j入門(四)批量更新節(jié)點屬性

          共 3883字,需瀏覽 8分鐘

           ·

          2021-09-20 21:11

          ??本文將用于介紹如何使用py2neo來實現(xiàn)Neo4j的批量更新節(jié)點屬性。

          單節(jié)點屬性更新

          ??首先我們先來看單個節(jié)點如何實現(xiàn)節(jié)點的屬性,比如現(xiàn)有Neo4j圖數(shù)據(jù)庫中存在節(jié)點,其label為Test,屬性有name=上海,我們需要為該節(jié)點增加屬性enname=shanghai
          ??以下為Python示例代碼:

          # -*- coding: utf-8 -*-
          from py2neo import Graph
          from py2neo import NodeMatcher

          # 連接Neo4j
          url = "http://localhost:7474"
          username = "neo4j"
          password = "******"
          graph = Graph(url, auth=(username, password))
          print("neo4j info: {}".format(str(graph)))

          # 查詢節(jié)點
          node_matcher = NodeMatcher(graph)
          node = node_matcher.match('Test', name="上海").first()
          # 新增enname屬性
          node["enname"] = "shanghai"
          graph.push(node)

          此時Neo4j圖數(shù)據(jù)庫中的該節(jié)點屬性已經(jīng)更新,見下圖:

          多節(jié)點單次更新

          ??我們先利用Python代碼,在Neo4j圖數(shù)據(jù)庫中創(chuàng)建10000個節(jié)點,代碼如下:

          # -*- coding: utf-8 -*-
          from py2neo import Graph, Node, Subgraph

          # 連接Neo4j
          url = "http://localhost:7474"
          username = "neo4j"
          password = "******"
          graph = Graph(url, auth=(username, password))
          print("neo4j info: {}".format(str(graph)))

          # 創(chuàng)建10000個節(jié)點
          node_list = [Node("Test", name=f"上海_{i}"for i in range(10000)]
          graph.create(Subgraph(nodes=node_list))

          如下圖所示:

          創(chuàng)建的10000個節(jié)點中的部分節(jié)點,節(jié)點僅有name屬性

          ??我們對每個節(jié)點進行查詢,再一個個為其新增no屬性,記錄下10000個節(jié)點的屬性更新的耗時。Python代碼如下:

          # -*- coding: utf-8 -*-
          import time
          from py2neo import Graph, NodeMatcher

          # 連接Neo4j
          url = "http://localhost:7474"
          username = "neo4j"
          password = "******"
          graph = Graph(url, auth=(username, password))
          print("neo4j info: {}".format(str(graph)))

          # 查詢節(jié)點
          node_list = []
          for i in range(10000):
              node_matcher = NodeMatcher(graph)
              node = node_matcher.match('Test', name=f"上海_{i}").first()
              node_list.append(node)
          print("find nodes.")

          # 一個個為節(jié)點新增no數(shù)據(jù)
          s_time = time.time()
          for i, node in enumerate(node_list):
              node["no"] = i * 2
              graph.push(node)
          e_time = time.time()
          print("cost time: {}".format(e_time - s_time))

          運行結果如下:

          cost time: 55.00530457496643

          多節(jié)點批量更新

          ??接下來,我們將節(jié)點屬性進行批量更新。

          # -*- coding: utf-8 -*-
          import time
          from py2neo import Graph, NodeMatcher, Subgraph

          # 連接Neo4j
          url = "http://localhost:7474"
          username = "neo4j"
          password = "******"
          graph = Graph(url, auth=(username, password))
          print("neo4j info: {}".format(str(graph)))

          # 查詢節(jié)點
          node_list = []
          for i in range(10000):
              node_matcher = NodeMatcher(graph)
              node = node_matcher.match('Test', name=f"上海_{i}").first()
              node_list.append(node)
          print("find nodes.")
          print(len(node_list))


          # 列表劃分
          def chunks(lst, n):
              for i in range(0, len(lst), n):
                  yield lst[i:i + n]


          # 批量更新
          s_time = time.time()
          for i, node in enumerate(node_list):
              node["no"] = i * 2
          for _ in chunks(node_list, 1000):
              graph.push(Subgraph(nodes=_))
          e_time = time.time()
          print("cost time: {}".format(e_time - s_time))

          運行結果是:

          cost time: 15.243977546691895
          多節(jié)點批量屬性更新后的結果,每個節(jié)點新增了no屬性

          ??可以看到,批量更新節(jié)點屬性效率會高很多!當然,如果需要更新的批數(shù)比較多,還可以借助多線程或多進程來進一步提升性能。


          瀏覽 194
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  操人视频免费 | 自拍偷拍网址 | 成人大香蕉最新视频 | 逼视频网站| 丁香激情国产色五月 |