python: simple example of working with neo4j

 

# 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 @ 2025-01-22 23:23  ®Geovin Du Dream Park™  阅读(18)  评论(0)    收藏  举报