Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store graph data in a database?

I am new to graphs and its very interesting.This question may be noob one but please site some good materials.

I am trying to make a small social Network where each user is a node and has undirected connection with his friend.

Its working fine but now I want to store it in a database.

How can I store the data?How to store all the connected nodes(pointer) of a node.

Is it better to delete the memory after the user log out and read it from database when he logs in or should logging in and logging out shouldnot have any impact on the node?

I know its theoretical. Any references will be really helpful.

like image 231
user2511713 Avatar asked Jul 10 '13 14:07

user2511713


People also ask

How is graph data stored in database?

Graph databases store data like object-oriented languages. Each object can maintain a collection of other objects it is related to. These references are usually pointers to objects in-memory, and we do not have to store them explicitly. Nor do we have to find the object in memory with some foreign key attribute.

How would you persist a graph data structure in a relational database?

You have to store Nodes (Vertices) in one table, and Edges referencing a FromNode and a ToNode to convert a graph data structure to a relational data structure. And you are also right, that this ends up in a large number of lookups, because you are not able to partition it into subgraphs, that might be queried at once.


2 Answers

Use an actual graph database to store your data.

http://www.neo4j.org/

You can store key/value pairs in a node and you can also store edges that connect nodes.

Then you can use something like Gremlin to query/traverse the graph -https://github.com/tinkerpop/gremlin. See their documentation to download examples and run sample queries: https://github.com/tinkerpop/gremlin/wiki/Getting-Started

An idea of the syntax:

gremlin> // lets only take 'knows' labeled edges
gremlin> v.out('knows')
==>v[2]
==>v[4]
gremlin> // lets do a traversal from the '1' marko vertex to its outgoing edges.
gremlin> // in the property graph world, edges are first class citizens that can be traversed to.
gremlin> v.outE
==>e[7][1-knows->2]
==>e[9][1-created->3]
==>e[8][1-knows->4]
like image 132
ryan1234 Avatar answered Sep 19 '22 04:09

ryan1234


I start at the bottom.

Is it better to delete the memory after the user log out and read it from database when he logs in or should logging in and logging out should not have any impact on the node?

You will need some sort of permanent storage, or your lose all the data you acquired on your first crash/restart that might upset your users a bit.

How can I store the data? Well without knowing more about this it is difficult however assuming that you have a list of users and each user can have 0 or more friends then i would go with 2 tables.

  • Users - stores all your user information such as username and password
  • UsersFriends *- store all the relationships in a UserID -> UserID fashion *

Example

Users Table

UserID  Username
1       user2511713
2       abstracthchaos
3       anotheruser

UsersFriends

UserID    FriendUserID
1           3
2           3
1           2

Means user2511713 is friends with anotheruser & abstracthchaos and abstracthchaos friends with anotheruser, dependant on your business logic it may also be useful to imply the other way around such that 3 1 is the same as 1 3

like image 35
AbstractChaos Avatar answered Sep 18 '22 04:09

AbstractChaos