python: simple example of working with neo4j

 

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# encoding: utf-8
# 版权所有 2025 ©涂聚文有限公司
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: pip install neo4j
# pip install py2neo
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm Community Edition 2024.3 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 11g oracle 20c  Neo4j 5.0
# https://neo4j.com/docs/python-manual/current/query-simple/
# https://neo4j.com/docs/getting-started/languages-guides/neo4j-python/
# https://github.com/neo4j-graph-examples/northwind/blob/main/code/python/example.py#L16-L21
# Datetime  : 2025/1/22 20:20
# User      : geovindu
# Product   : PyCharm
# Project   : ictsimple
# File      : neo4jdemo.py
# explain   : 学习
  
from neo4j import GraphDatabase
  
  
class GraphHelper:
    """
    """
    def __init__(self, uri, user, password):
        """
        :param uri:
        :param user:
        :param password:
        """
        self.driver = GraphDatabase.driver(uri, auth=(user, password))
  
    def close(self):
        """
        :return:
        """
        self.driver.close()
  
  
    def printGreeting(self, message):
        """
        create_and_return_greeting
        :param message:
        :return:
        """
        with self.driver.session() as session:
            greeting = session.execute_write(self.createQuery, message)
            print("greeting:",type(greeting))
  
  
  
  
  
    def query(self):
       """
        查询
       :param tx:
       :param message:
       :return:
       """
       records, summary, keys = self.driver.execute_query(
            "MATCH (p:Person) RETURN p.name AS name",
            database_= "neo4j",
            )
       # Loop through results and do something with them
       for record in records:
           print(record.data())  # obtain record as dict
  
       # Summary information
       print("The query `{query}` returned {records_count} records in {time} ms.".format(
           query=summary.query, records_count=len(records),
           time=summary.result_available_after
       ))
  
       return records
  
  
  
    @staticmethod
    def createQuery(tx, message):
        """
        返回是的对象
        :param tx:
        :param message:
        :return:
        """
        result = tx.run("MATCH (n) RETURN n LIMIT 10", message=message)
        print(result)
        print("***********")
        for s in result:
            print(s.data())
        print("***********")
        return result
  
  
  
    @staticmethod
    def create_and_return_greeting(tx, message):
        """
        :param tx:
        :param message:
        :return:
        """
        result = tx.run("CREATE (a:Greeting) "
                        "SET a.message = $message "
                        "RETURN a.message + ', from node ' + id(a)", message=message)
        return result.single()[0]
  
  
# http://localhost:7474/browser/
greeter = GraphHelper("bolt://localhost:7687", "neo4j", "geovindu")
greeter.printGreeting("hello, world")
print("####################################")
greeter.query()
greeter.close()

  

 

posted @   ®Geovin Du Dream Park™  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2024-01-22 svg: table style
2022-01-22 java: create pdf password using itextpdf 5.x and bouncycastle Library
2019-01-22 vis.js 4.21.0 Timeline localization
2018-01-22 Python3.4:splinter or traceback
< 2025年3月 >
23 24 25 26 27 28 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 31 1 2 3 4 5
点击右上角即可分享
微信分享提示