Categoría: Artículos

Autor: Martín Jurado

Head of advanced Analytics, Big Data and Digital Transformation en GRUPO INFRA MX.

19 diciembre, 2023

. 6 minutos de lectura.

In today’s digital fabric, where information flows like a complex network, graph databases have emerged as the master architect of connection. In a world where data is more than just points, where each entity is intertwined with another in a sophisticated dance of relationships, graph databases have taken center stageBut what happens when information becomes a vast, complex puzzle, dispersed across cyberspace? How can we transform disconnected data into relevant and meaningful knowledge? This is where graph database technology comes into play to create “Knowledge Graphs” but this term will be the subject of another article.

But what are Graph Databases?

Graph databases are a type of database management system designed to store and query data in graph form. A graph consists of nodes that represent entities and relationships between those nodes that represent the connections or interactions between the entities.

Key elements:

  • Nodes: They represent entities (An entity is a “thing”, “object”, “individual” or “concept” of the real world with independent existence, that is, it differs only from another object or thing, even if it is of the same type or the same entity).
  • Relationships (or Edges): They connect nodes and describe the interactions between them, usually they are “actions” of said entities. Each relationship has a type that defines the nature of the connection. In a more technical context, relationships can have additional properties, such as weight or direction, and can be directed or undirected.
  • Properties: Attributes or information associated with nodes and relationships. For example, a node that represents a person could have properties such as name, age, and gender. Relationships can also have properties, such as the date the connection was established.

Graph databases are ideal for scenarios where the relationships and connections between data are as important as the data itself. Widely used in applications such as social networking, fraud analysis, recommendations, knowledge representation, and complex network modeling, graph databases provide an intuitive and powerful data structure for modeling and querying complex relationships, making them valuable in a variety of modern applications.

Main Features:

1. Model Flexibility:

  • Natural Representation of Relationships: Graph databases allow relationships to be modeled in a natural way. Each entity (node) in the graph can have direct relationships with other entities through connections (edges). This makes it easier to represent complex relationships and data hierarchies, such as social networks, product relationships in recommender systems, and organizational structures.
  • Dynamic Schema: Unlike some relational database models, where a rigid schema is required, graphs allow a dynamic schema. You can add new relationships and properties to nodes without first modifying the database structure. This is especially useful in environments where the data structure changes over time.

2. Efficiency in Relationship Consultations:

  • Fast Retrieval of Connected Information: The graph structure is especially efficient for queries that involve relationships. By following edges, queries can quickly and directly retrieve connected information. This is beneficial in scenarios such as social media browsing, where it is common to retrieve friends of friends, or in recommendation applications that look for similar behavior patterns.
  • Efficient Trajectory Algorithms: Graph databases often implement efficient algorithms to find trajectories and patterns in the network. This is essential for solving problems such as finding the shortest path between two nodes or identifying communities within a network.

3. Scalability:

  • Efficient Handling of Large Data Sets: Graph databases are designed to be highly scalable. They can efficiently handle large volumes of data and remain effective as the complexity and size of the graph grow. This is crucial in scenarios where the number of relationships and nodes can be massive, such as in large-scale social networks or in analysis of business interconnections.
  • Distribution and Parallelism: Many graph database implementations offer distribution and parallelism capabilities, allowing the workload to be distributed across multiple nodes or servers. This significantly contributes to the ability to scale out to handle large distributed data sets.

Use cases:

  • Social Networks: The graph could represent users as nodes and friendship connections as edges between them.
  • Recommendation Systems: Here, nodes can be users and products, while edges reflect preferences or past purchases, allowing for more precise recommendations.
  • Analysis of Logistics Routes and Networks: Nodes can be locations and edges represent physical connections between them, helping to optimize routes and logistics flows.
  • Knowledge Management: In this case, the nodes could be concepts or entities, and the edges reflect logical relationships between them, facilitating the organization and search for information.

Examples of Graph Databases:

  • Neo4j: A popular graph database with an intuitive query syntax called Cypher.
  • Amazon Neptune: A fully managed graph database service in the cloud. It uses SPARQL and Gremlin as query languages. SPARQL is a standard query language for retrieving information and data from RDF graphs, while Gremlin is a graph query language that follows the Apache TinkerPop initiative standard.
  • ArangoDB: A multi-model database that supports graphs along with other data models. It uses AQL (ArangoDB Query Language) for queries. AQL is a domain-specific language designed to work with graph and document data.

But how are these systems used in practice? (e.g. Neo4j)

In Neo4j, queries are performed in the graph property query language, also known as Cypher. Here are some basic examples of introductory queries in Neo4j:

  1. Create Account (Nodo):

CREATE (source:Account {number: 1, owner: 'John Doe', balance: 1000})
CREATE (destination:Account {number: 2, owner: 'John Pie', balance: 5000})

2.  View all the nodes:

MATCH (a:Account)
RETURN a

3. Perform a Transaction (Relationship):

MATCH (source:Account {number: 1}), (destination:Account {number: 2})
CREATE (source)-[t:TRANSFER {amount: 200, timestamp: timestamp()}]->(destination)
SET source.balance = source.balance - t.amount,
destination.balance = destination.balance + t.amount

This query matches the source and destination accounts and creates a TRANSFER relationship between them with a specified amount. It also updates the balances of the source and destination accounts accordingly.

4. Retrieve All Transactions:

MATCH (a)-[t:TRANSFER]->(b)
RETURN a, t, b

This query retrieves all nodes connected by TRANSFER relationships, representing transactions between accounts.

5. Update an Account’s Balance:

MATCH (a:Account {number: 1})
SET a.balance = 800

This query updates the balance of the account with account number 1 to 800.

.                   .                 .

Cypher, the query language used in Neo4j, has proven to be a powerful tool for interacting with graph databases. Its intuitive and expressive design makes it easy to manipulate and retrieve data in graph structures efficiently. It is not only an essential tool for interacting with graph databases, but it also helps make the modeling and manipulation of graph data accessible and effective for developers and users alike. Its adoption has been fundamental in the popularity and usefulness of Neo4j and other graph databases.

In short, graph databases have become an indispensable tool for those seeking to extract meaningful insights from interconnected data. By adopting this technology, organizations can unlock new possibilities to discover patterns, identify trends, and make informed decisions in today’s increasingly interconnected world. If you want a Neo4j article, please let me know.