Created
September 27, 2012 09:29
-
-
Save dchomicz/3793112 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from bulbs.neo4jserver import Graph, Config, Neo4jClient | |
from bulbs.config import DEBUG | |
from bulbs.model import Node, Relationship | |
from bulbs.property import String, Integer, DateTime | |
from bulbs.utils import current_datetime | |
class Person(Node): | |
element_type = "person" | |
id = Integer(nullable=False) | |
email = String() | |
class Knows(Relationship): | |
label = "knows" | |
created = DateTime(default=current_datetime, nullable=False) | |
class Neo4jAPI(object): | |
def __init__(self, neo4jURL): | |
config = Config(neo4jURL) | |
self._graph = Graph(config) | |
self._graph.add_proxy('people', Person) | |
self._graph.add_proxy('knows', Knows) | |
self._client = Neo4jClient(config) | |
self._graph.config.set_logger(DEBUG) | |
def create_node(self, user): | |
return self._graph.people.get_or_create('id', user['id'], id=user['id'], email=user['email']) | |
def find_nodes_by_id(self, user_id): | |
return self._graph.people.index.lookup(id=user_id).next() | |
def create_relationship(self, nodeA, nodeB): | |
print nodeA.id,' ', nodeB.id | |
if nodeB not in nodeA.bothV(): | |
return self._graph.knows.create(nodeA, nodeB) | |
print 'exists' | |
return False | |
def get_friends_of_friends(self, user_id): | |
# user = self._graph.people.index.lookup(id=user_id).next() | |
# for friend in user.bothV(): | |
# for fof in friend.bothV(): | |
# print fof.id | |
self._graph.warm_cache() | |
query = """START user=node:person(id={_id}) | |
MATCH user-[:knows]-()-[:knows]-fof, user-[r?:knows]-fof | |
WHERE r is NULL AND fof.id <> user.id | |
RETURN fof, COUNT(*) | |
ORDER BY COUNT(*) DESC, fof.id""" | |
#query = """START user=node:person(id={_id}) RETURN user""" | |
params = dict(_id=user_id) | |
results = self._client.cypher(query, params).results | |
print results | |
for result in self._client.cypher(query, params).results: | |
print result.get_data() | |
if __name__ == '__main__': | |
from config import NEO4J_URI | |
api = Neo4jAPI(NEO4J_URI) | |
nodes = {} | |
# api._graph.clear() | |
# for i in xrange(1, 6): | |
# nodes[i] = api.create_node({'id': i, 'email': None}) | |
for i in xrange(1, 6): | |
nodes[i] = api.find_nodes_by_id(i) | |
# api.create_relationship(nodes[1], nodes[2]) | |
# api.create_relationship(nodes[1], nodes[3]) | |
# api.create_relationship(nodes[2], nodes[1]) | |
# api.create_relationship(nodes[2], nodes[4]) | |
# api.create_relationship(nodes[2], nodes[5]) | |
# api.create_relationship(nodes[3], nodes[5]) | |
# api.create_relationship(nodes[4], nodes[2]) | |
api.get_friends_of_friends(4) | |
#node = api.find_nodes_by_id(5) | |
#print node.bothV().bothV() | |
#print node.email |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DB_ENGINE = 'mysql://root:admin@localhost:3006/neo4j' | |
NEO4J_URI = 'http://localhost:7474/db/data/' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from sqlalchemy import create_engine | |
from config import DB_ENGINE | |
engine = create_engine(DB_ENGINE) | |
def fetch_users(): | |
connection = engine.connect() | |
users = connection.execute('select * from users;'); | |
for user in users: | |
yield user | |
connection.close() | |
def fetch_friends(): | |
connection = engine.connect() | |
friends = connection.execute('select * from friends'); | |
for friend in friends: | |
yield friend | |
connection.close() | |
if __name__ == '__main__': | |
from config import NEO4J_URI | |
from neo4jAPI import Neo4jAPI | |
api = Neo4jAPI(NEO4J_URI) | |
users = fetch_users() | |
for user in users: | |
api.create_node({'id': user[0], 'email':user[1]}) | |
friends = fetch_friends() | |
for friend in friends: | |
nodeA = api.find_nodes_by_id(friend[0]) | |
nodeB = api.find_nodes_by_id(friend[1]) | |
api.create_relationship(nodeA, nodeB) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment