Neo4j python driver | AttributeError: 'Session' object has no attribute 'execute_read' - python

In the Neo4j Python code, I have an issue while query from Neo4j DB, with getting below error:
AttributeError: 'Session' object has no attribute 'execute_read'
I want to convert the results of cypher queries into a JSON format.I am using the neo4j python library to connect the graph DB.
Here is my current code:
cypherQuery="MATCH (n:BusinessApplication) RETURN n"
#Run cypher query
with driver.session() as session:
results = session.execute_read(lambda tx: tx.run(cypherQuery).data())
driver.close()

The session.execute_read function was introduced in a more recent version of the Neo4j Python driver (5.0) so you may see that error because you're using a previous version of the Neo4j Python driver that does not support that function.
You could upgrade to a more recent version 5.0+ of the Neo4j Python driver to use the execute_read syntax or use the read_transaction function instead:
def get_business_apps(tx):
results = tx.run("MATCH (n:BusinessApplication) RETURN n")
for record in results:
# do something with each record
print(record['n'])
with driver.session() as session:
session.read_transaction(get_business_apps)
driver.close()

Related

To connect mongodb Atlas with the python?

I want to connect the python with the mongodb Atlas using .py file. I tried to insert data into the collection using following code but got error as follows??
client = pymongo.MongoClient("******************************")//Here *********is connection string
for db in client.list_database_names():
if db in ['admin','local']:
pass
else:
for collection in client[db].list_collection_names():
print(collection)
collection.insert_one(data)
print("successfully inserted")
but got error on the line collection.insert_one as follows?
Attribute:'str' object has no attribute 'insert_one'

Pandas read_sql inconsistent behaviour dependent on driver?

When I run a query from my local machine to a SQL server db, data is returned. If I run the same query from a JupyterHub server (using ssh), the following is returned:
TypeError: 'NoneType' object is not iterable
Implying it isn't getting any data.
The connection string is OK on both systems (albeit different), because running the same stored procedure works fine on both systems using the connection string -
Local= "Driver={SQL Server};Server=DNS-based-address;Database=name;uid=user;pwd=pwd"
Hub = "DRIVER=FreeTDS;SERVER=IP.add.re.ss;PORT=1433;DATABASE=name;UID=dbuser;PWD=pwd;TDS_Version=8.0"
Is there something in the FreeTDS driver that affects chunksize, or means a set nocount is required in the original query as per this NoneType object is not iterable error in pandas - I tried this fix by the way and got nowhere.
Are you using pymssql, which is build on top of FreeTDS?
For SQL-Server you could also try the Microsoft JDBC Driver with the python package jaydebeapi: https://github.com/microsoft/mssql-jdbc.
import pandas as pd
import pymssql
conn = pymssql.connect(
host = r'192.168.254.254',
port = '1433',
user = r'user',
password = r'password',
database = 'DB_NAME'
)
query = """SELECT * FROM db_table"""
df = pd.read_sql(con=conn, sql=query)

How to check connection from Python to Neo4j

I'm doing a microservice in Python 3.7 that connects to a Neo4j database. It's the first time I work connecting Python with Neo4j and I'm using py2neo version 4.3.0.
Everything works OK, but now to adhere to the standard, I need to create a healthcheck to verify the connection to the Database.
I wanted to use the
from py2neo import Graph, Database
and use
db = Database ("bolt: // localhost: 7474", auth = ("neo4j", "xxxx"))
and
db.kernel_version (Dont work)
but with this I do not verify that there is connection is up. Does anybody have any suggestions?
If checking the kernel version doesn't work then the connection is not ok. Below is a script to check if the connection from python to neo4j (via py2neo) is up and running.
from py2neo import Graph
graph = Graph("bolt://localhost:7687", auth=("neo4j", "xxxxx"))
try:
graph.run("Match () Return 1 Limit 1")
print('ok')
except Exception:
print('not ok')

Neo4j Python bolt driver: how to convert the result to Json?

I am using the bolt driver(1.0.1) with python. How can I convert the result into a Json so that I can return it to through a flask app?
That is I need to convert datatype, "neo4j.v1.types.Record" to "json".
I tried this
from flask import Flask
from neo4j.v1 import GraphDatabase, basic_auth
import json
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j","neo4j"))
session = driver.session()
app = Flask(__name__)
#app.route('/hello/<prop>')
def hello_name(prop):
result = session.run("MATCH ...") #this works perfectly fine and the data is captured in result
session.close()
for record in result:
return json.loads(record)
This throws an error :- TypeError: the JSON object must be str, not 'Record'
The neo4j driver gives the result in a generator.(Record Type) You cannot do json.load as it is not a json serialized string.
What you can do :
for record in result:
print result[0]
See the format of the result and take the elements you like.
For further understanding on how to parse neo4j records:
http://neo4j.com/docs/api/python-driver/current/session.html

AttributeError: 'Graph' object has no attribute 'cypher' in migration of data from Postgress to Neo4j(Graph Database)

I am working on migration of data from postgres to Graph Database manually.
I have wrote script below:
import psycopg2
from py2neo import authenticate, Graph
authenticate("localhost:7474", "neo4j", "password")
n4j_graph = Graph("http://localhost:7474/db/data/")
try:
conn=psycopg2.connect("dbname='db_name' user='user' password='password'")
except:
print "good bye"
cur = conn.cursor()
try:
cur.execute("""SELECT * from table_name""")
except:
print "not found"
rows = cur.fetchall()
for row in rows:
username = row[4]
email = row[7]
s = '''MERGE (u:User { username: "%(username)s"}) MERGE (e:Email { email: "%(email)s"}) CREATE UNIQUE (u)-[:BELONGS_TO]->(e)''' %{"username": username, "email": email}
print s
n4j_graph.cypher.execute(s)
Error:
AttributeError: 'Graph' object has no attribute 'cypher'
This issue I resolved by updating py2neo to version 2.0.8.
pip uninstall py2neo
pip install py2neo==2.0.8
I am following documentation of py2neo.
While for production I am still getting:
AttributeError: 'Graph' object has no attribute 'cypher'
GET 404 response
What can be issue?
I had this problem too. In my case I was looking at the py2neo v2 documentation but on my machine was installed py2neo v3. You should check your py2neo version and replace .cyper({query}) with .run({query})
The previous version of py2neo allowed Cypher execution through Graph.cypher.execute(). This facility is now instead accessible via Graph.run() and returns a lazily-evaluated Cursor rather than an eagerly-evaluated RecordList.
I have resolved issue.
Issue was with version of py2neo. I have installed version 3 while version 2.08 is latest in V2.
py2neo allowed Cypher execution through Graph.cypher.execute().
pip uninstall py2neo
pip install py2neo==2.0.8

Categories