nicolewhite / neo4j-flask

Flaskr Extended with Neo4j and Py2neo.
http://nicolewhite.github.io/neo4j-flask/
311 stars 114 forks source link

Example with unique relationship names #4

Closed scheung38 closed 8 years ago

scheung38 commented 8 years ago

Hi Nicole,

I am trying to do this:

port = gdb.labels.create(“Port”)

buenos_aires = gdb.nodes.create(name=”Buenos Aires”)
new_york = gdb.nodes.create(name=”New York”)
liverpool = gdb.nodes.create(name=”Liverpool”)
casablanca = gdb.nodes.create(name=”Casablana”)
cape_town = gdb.nodes.create(name=”Cape Town”)

port.add(buenos_aires, new_york, liverpool, casablanca, cape_town)

buenos_aires.relationships.create(“6 days”, new_york)
buenos_aires.relationships.create(“5 days”, casablanca)
buenos_aires.relationships.create(“4 days”, cape_town)

then how to do the query as each relationship is not the same as generic ‘LIKE’ but different number of days?

say needing to find out number of days from New York to Liverpool to Cape Town etc?

nicolewhite commented 8 years ago

I think that's a modeling issue. The days should be stored as a property, not as a part of the relationship type. Then you could query for just the :DAYS relationship type and do manipulations with the property on that relationship.

scheung38 commented 8 years ago

Could you give simple example? Thanks

Also noticed that starting:

from py2neo import Graph
graph = Graph()

.bashrc: export NEO4J_USERNAME=neo4j export NEO4J_PASSWORD=mypassword

and even after changing /usr/local/Cellar/neo4j/2.2.5/libexec/conf: dbms.security.auth_enabled=false

http://localhost:7474/db/data/ still returns

{
    errors: [
{
    code: "Neo.ClientError.Security.AuthorizationFailed",
    message: "No authorization header supplied."
}
]
}
nicolewhite commented 8 years ago

I'm not sure what methods you are using in the code you included. But with py2neo, you can set relationship properties like so:

from py2neo import Graph, Node, Relationship
graph = Graph()

buenos_aires = Node("Port", name="Buenos Aires")
new_york = Node("Port", name="New York")
casablanca = Node("Port", name= "Casablanca")
cape_town = Node("Port", name="Cape Town")

graph.create(buenos_aires, new_york, casablanca, cape_town)

rel1 = Relationship(buenos_aires, "TRIP_TO", new_york, days=6)
rel2 = Relationship(buenos_aires, "TRIP_TO", casablanca, days=5)
rel3 = Relationship(buenos_aires, "TRIP_TO", cape_town, days=4)

graph.create(rel1, rel2, rel3)
scheung38 commented 8 years ago

Using:

python 2.7.10 py2neo 2.0.8

with the above settings, but running example as it is:

Traceback (most recent call last):
  File "/Users/seb/PycharmProjects/Neo4jSample/sample_py2neo.py", line 9, in <module>
    graph.create(buenos_aires, new_york, casablanca, cape_town)
  File "/Users/seb/anaconda/envs/pyenv2.7.10/lib/python2.7/site-packages/py2neo/core.py", line 706, in create
    statement = CreateStatement(self)
  File "/Users/seb/anaconda/envs/pyenv2.7.10/lib/python2.7/site-packages/py2neo/cypher/create.py", line 44, in __init__
    self.supports_node_labels = self.graph.supports_node_labels
  File "/Users/seb/anaconda/envs/pyenv2.7.10/lib/python2.7/site-packages/py2neo/core.py", line 1080, in supports_node_labels
    return self.neo4j_version >= (2, 0)
  File "/Users/seb/anaconda/envs/pyenv2.7.10/lib/python2.7/site-packages/py2neo/core.py", line 958, in neo4j_version
    return version_tuple(self.resource.metadata["neo4j_version"])
  File "/Users/seb/anaconda/envs/pyenv2.7.10/lib/python2.7/site-packages/py2neo/core.py", line 213, in metadata
    self.get()
  File "/Users/seb/anaconda/envs/pyenv2.7.10/lib/python2.7/site-packages/py2neo/core.py", line 261, in get
    raise Unauthorized(self.uri.string)
py2neo.error.Unauthorized: http://localhost:7474/db/data/
nicolewhite commented 8 years ago

You need to authenticate or turn off authentication.

scheung38 commented 8 years ago

But the conf already took care of that when I changed auth_enabled=false :

/usr/local/Cellar/neo4j/2.2.5/libexec/conf:
dbms.security.auth_enabled=false

or is it the case that this installation was done via brew install neo4j and this conf file was not read in?

nicolewhite commented 8 years ago

Can you try using a version of Neo4j that wasn't installed with brew?

scheung38 commented 8 years ago

Does it make a difference actually? Since neo4j is running in the background and localhost:7474 takes user to neo4j dashboard. it is the database that cannot be authorized for some reason. I see that in your models.py example

import os

url = os.environ.get('GRAPHENEDB_URL', 'http://localhost:7474')
username = os.environ.get('NEO4J_USERNAME')
password = os.environ.get('NEO4J_PASSWORD')

if username and password:
    authenticate(url.strip('http://'), username, password)

graph = Graph(url + '/db/data/')

But still same error.

nicolewhite commented 8 years ago

I think it must make a difference. If setting dbms.security.auth_enabled=false didn't work, I think you should try with a version of Neo4j that was installed normally.

scheung38 commented 8 years ago

Before doing reinstallation,

this conf in my case:

/usr/local/Cellar/neo4j/2.2.5/libexec/conf:
dbms.security.auth_enabled=false

But project is in

/Users/seb/PycharmProjects/SampleNeo4j/models.py

is there anyway to read in within python to check its status? It is a bit strange to have conf file in totally different path, relative to user application path...

nicolewhite commented 8 years ago

It shouldn't matter. Maybe try hardcoding in the username and password to make sure it's not just an environment variable problem:

url = "http://localhost:7474"
username = "your_username"
password = "your_password"
authenticate(url.strip('http://'), username, password)
graph = Graph(url + "/db/data/")
scheung38 commented 8 years ago
url = os.environ.get('GRAPHENEDB_URL', 'http://localhost:7474') -> url: 'http://localhost:7474'
username = os.environ.get('NEO4J_USERNAME') -> username: 'neo4j'
password = os.environ.get('NEO4J_PASSWORD') -> password: 'mypassword'
if username and password:
    authenticate(url.strip('http://'), username, password)

graph = Graph(url + '/db/data/') -> graph : <Graph uri= u'http://localhost:7474/db/data/'>

so it seems like it is passing through the if username and password comparison and the other values are read in from .bashrc where I set NEO4J_USERNAME and NEO4J_PASSWORD.

nicolewhite commented 8 years ago

That's why I suggested hardcoding the values to check if it was an environment variable problem. Does it work if you set the values in the code as I suggested?

scheung38 commented 8 years ago

So with the returned values above I can access dashboard

http://localhost:7474

But

http://localhost:7474/db/data/

still returns:

{
errors: [
{
    code: "Neo.ClientError.Security.AuthorizationFailed",
    message: "No authorization header supplied."
}
]
}

which means something is still incorrect as no data generated along with the above message?

nicolewhite commented 8 years ago

What is "the dashboard"?

scheung38 commented 8 years ago

Single stepping seem to be fine as each step is returning some value, until the last

graph.create(rel1, rel2, rel3, ..)

upon executing I get a

Frame is not available

Sorry when referring to the dashboard, I simply meant the localhost:7474/browser page from Neo4j

nicolewhite commented 8 years ago

That code snippet works fine for me. I'd check your syntax or run it all at once.

scheung38 commented 8 years ago

and what did you get when you to go http://localhost:7474/db/data/? thanks

scheung38 commented 8 years ago

This is what I have:

from py2neo import Graph, Node, Relationship, authenticate
import os

url = os.environ.get('GRAPHENEDB_URL', 'http://localhost:7474')
username = os.environ.get('NEO4J_USERNAME')
password = os.environ.get('NEO4J_PASSWORD')

if username and password:
    authenticate(url.strip('http://'), username, password)

graph = Graph(url + '/db/data/')

buenos_aires = Node("Port", name="Buenos Aires")
new_york = Node("Port", name="New York")
liverpool = Node("Port", name="Liverpool")
casablanca = Node("Port", name="Casablanca")
cape_town = Node("Port", name="Cape Town")

graph.create(buenos_aires, new_york, liverpool, casablanca, cape_town)

rel1 = Relationship(buenos_aires, "TRIP_TO", new_york, days=6)
rel2 = Relationship(buenos_aires, "TRIP_TO", casablanca, days=5)
rel3 = Relationship(buenos_aires, "TRIP_TO", cape_town, days=4)
rel4 = Relationship(new_york, "TRIP_TO", liverpool, days=4)
rel5 = Relationship(liverpool, "TRIP_TO", casablanca, days=3)
rel6 = Relationship(liverpool, "TRIP_TO", cape_town, days=6)
rel7 = Relationship(casablanca, "TRIP_TO", liverpool, days=3)
rel8 = Relationship(casablanca, "TRIP_TO", cape_town, days=6)
rel9 = Relationship(cape_town, "TRIP_TO", new_york, days=8)

graph.create(rel1, rel2, rel3, rel4, rel5, rel6, rel7, rel8, rel9)

print 'Hello'

screen shot 2015-10-09 at 21 47 45

but end result at http://localhost:7474/db/data/:

screen shot 2015-10-09 at 21 49 05

nicolewhite commented 8 years ago

Try doing these one at a time:

graph.create(Relationship(buenos_aires, "TRIP_TO", new_york, days=6))
graph.create(Relationship(buenos_aires, "TRIP_TO", casablanca, days=5))
graph.create(Relationship(buenos_aires, "TRIP_TO", cape_town, days=4))
...
scheung38 commented 8 years ago

same message at http://localhost:7474/db/data/

what is displayed at your end when you view http://localhost:7474/db/data/?

nicolewhite commented 8 years ago

This is not related to the browser. Please install Neo4j regularly (not through Homebrew) and try again.

scheung38 commented 8 years ago

OK with clean install now works!

uninstalled brew version

But not sure why browser shows the same graph 4x? screen shot 2015-10-09 at 23 09 56

scheung38 commented 8 years ago

Modified graph in python, but neo4j still showing previous old graph data even with browser page reload? How do we migrate or delete db? Normally Pycharm can delete dB within project, but not sure how to delete here as db is not stored in application directory? Other than knowing the url for database is

http://localhost:7474/db/data

rm -rf graph.db the way currently to delete, but chrome browser still rendering old graph?

scheung38 commented 8 years ago

From doc I can understand this syntax of finding movies that match a certain pattern and returning it

MATCH (m:Movie) WHERE m.title = "The Matrix" RETURN m

But more useful queries I like to learn how to perform would be like:

  1. Buenos Aires to New York to Liverpool would be?
  2. Shortest path from Buenos Aires to Liverpool?
  3. No of routes from Liverpool to Liverpool with limit?
  4. Buenos Aires to Liverpool with exactly 4 stops?
  5. Liverpool to Liverpool less than 25 days?

Not sure how to go about it as I may have to dug deep into doc for these queries. Does example also show unittesting? Any guidance would be appreciated.

nicolewhite commented 8 years ago

I was happy to help you get up and running with py2neo, but your questions are now beyond the scope of this project. You should consider posting them to StackOverflow.