-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use SQLbackend, how to connect to existing SQL DB file and read the graph into memory #19
Comments
If you have already created a graph on disk with sqlite, you can read it like this (assuming your database file is called import grand
from grand.backends import SQLBackend
G = grand.Graph(
backend=SQLBackend(db_url="sqlite:///example-graph.sqlite", directed=True)
) You can now interact with the graph through whatever dialect you like. If you want to use NetworkX-style APIs, you can list edges like this: G.nx.edges() Unfortunately, there isn't currently a great way to export and import graphs between Backends yet, as this can be a super expensive operation. If your graph is relatively small, you can export nodes and edges individually into, say, a NetworkX object — like this: H = nx.DiGraph()
for node, attrs in G.nx.nodes(data=True):
H.add_node(node, **attrs)
for u, v, attrs in G.nx.edges(data=True):
H.add_edge(u, v, **attrs) Then you'll have access to all the same nodes in an in-memory NetworkX object: H.nodes(data=True) Does this answer your question? To get you started if you don't have a sqlite graph file yet, you can create the G = grand.Graph(backend=SQLBackend(db_url="sqlite:///example-graph.sqlite", directed=True))
G.nx.add_edge("X", "Y") |
Hey @yangyxt — does this answer your question? |
I have a similar question I'm thinking through. Would I be able to use grand with grand-cypher for that ? Or will it be inefficient due to the way RDBMS queries work |
Hi @AbdealiJK! That's an interesting use-case, and I think that Grand should work for you, though I'm not sure we can make any efficiency guarantees since every "edge" in your query will be converted to its own query. One issue you'll likely encounter is that Grand currently assumes ONE table for nodes and ONE table for edges, but we might be able to get around that if you have a more-or-less consistent schema between the "nodelike" tables and we can form a virtual nodes table out of a join. By way of excessive explanation: The reason we don't currently support this is that not all Backends support the idea of multiple node "types," and practically no Dialects do either. So there's nothing blocking this technically, it's just not the original use-case! :) If you're interested in playing around with it, I'd be happy to help debug with you! |
Yeah. I'm planning on trying it out over the weekend. I guess the 2 things I'm thinkint about in this regard are:
My usecases are query-only. I.e. I will never modify my graph through networkx/other dialects. But, I really like this idea. I was looking for a sqlalchemy for graphs. And grand (while still young) seems to be moving towards that direction. Will play around with it more and contribute if possible |
Yes, thank you! And sorry for the late notice and response! Regarding exporting graphs, I chose to converting the database to text-based file objects to store the graph. |
Awesome @yangyxt — hope you're able to use Grand in the future :) @AbdealiJK: Great, let me know what you discover! I haven't really kicked the tires extensively on the SQL Backend in conjunction with Grand-Cypher, but in a few benchmarks it seemed pretty performant even on relatively large networks. (Not comparable with a "true" neo4j cypher query, but... not embarrassing, at least :) ) When I say "for each edge," that's each edge in the query, so if your Cypher query looks like MATCH (A)-[]->(B)
RETURN A, B This only runs one single table query. Still not ideal, but with some indexing probably pretty snappy. I've also just published one of my backlog-todo's to add a caching layer to the Backends (#20); I think that would probably dramatically increase the speed of query responses if you can make assumptions about a static dataset. I'd be super grateful and excited to see your contributions and evaluation! There's a lot of nuance to a generalized graph toolkit like Grand but I'm hopeful we're converging on something Good :) |
Hey @AbdealiJK — curious to hear how your experiments went! Would be interested in hearing if you had to make any major changes to get things working. |
Hi @j6k4m8 apologies I missed replying to your previous question. Differences here are:
|
Aha, that's super interesting @AbdealiJK — thank you for the update! If you think there are any good lessons-learned that we could integrate into the package, I'd love to help! |
I think the main one for me was that a single query which creates my entire graph was much faster than I expected. I would also suggest having some hooks in grand so I can customize the SQL structure I have in an existing DB. |
As showed in the title.
The text was updated successfully, but these errors were encountered: