I have connected to multi-database model in neo4j, Also have a set of python scripts to push and pull data to and from multiple database in NEO4J. We need to specify the name of the database we are operating these scripts against. Also to this i want to find currently which database which driver connected.
from neo4j import GraphDatabase
# create the uri for the connection
uri = "neo4j://localhost:7687"
# create a driver object
driver = GraphDatabase.driver(uri, auth=("neo4j", "password"))
print("driver created")
# get info about the driver
print("connected: {}".format(driver.verify_connectivity()))
print("Host: {}".format(driver.default_host))
print("Port: {}".format(driver.default_port))
# close the driver
driver.close()
Here
print("connected: {}".format(driver.verify_connectivity()))
this given me the info about the driver below only:
connected: Neo4j/4.4.11
Along this, as part of integration, i want to list the database with which driver connected. How can i achieve that, appreciate your help ?
If by
you mean the server, you can use
driver.get_server_info()https://neo4j.com/docs/api/python-driver/5.7/api.html#neo4j.Driver.get_server_infoIf you mean against which database in your DBMS a query would be executed: that's whatever the currently configured home database of the user you're authenticated as (
neo4jin your example) is.If you look at https://neo4j.com/docs/operations-manual/current/manage-databases/configuration/ you can see that
SHOW HOME DATABASEwill give you the current home database.Here is a quick example to show how that could look like:
Or little more elaborate and production ready:
(1)specifying the database makes the driver work slightly more efficient as it doesn't have to resolve the home database before executing the actual query (always recommend, when possible)(2)tell the driver that this is a read query and in a clustered environment can be routed to followers/reader to reduce load on the leader/writer server(3)turn the result stream into a single record and (strict=True) raise an exception if there's not exactly one recordThis assumes you're using a relatively recent version of the driver. If not, you can achieve the same thing using lower-level APIs like
driver.sessionandsession.execute_read.