未加星标

Starting with Graph Databases: A Quick look into Neo4j

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Let’s talk about Neo4j, a graph database that recently has attracted a significant number of fans. My goal is for you to have a brief (I promise to be quick) vision of how it works and to give you some examples to make it more tangible.

We are used to write data using tables, to relate them through primary keys and, by looking directly at the data, you only see IDs. Graph databases were designed mainly so that this doesn’t happen. Its purpose is for you to have a complete understanding when looking at the data.


Starting with Graph Databases: A Quick look into Neo4j

Example of a simple school database diagram.

First of all, to understand an advantage of graph databases, it’s very good to know a few concepts that reinforce the idea of software close to the business rules. For that, it is worth reading Domain-Driven Design , where the model should be as close as possible to the business.

In Neo4j, that is done with Cypher: a declarative SQL-inspired language for describing patterns in visual graphs using an ASCII syntax. It allows us to state what we want to select, insert, update or delete from our graph data without requiring us to describe exactly how to do it.

To be more practical, mind the example below, where the same “query” is implemented with SQL and Cypher.

SELECT f.* FROM students s INNER JOIN person p ON p.id = s.person_id INNER JOIN friend f ON f.friend_from_id == p.id WHERE s.id = 45 ORDER BY p.name MATCH (student :Student)-[FRIEND]->(person :Person) WHERE student.id = 45 RETURN person ORDER BY person.name

Above there’s a “query” to search for a specific student’s friends, where in the traditional mode (SQL) we need to better understand the database structure and know how they relate, causing a greater use of “JOINS”. In Cypher relations are more intuitive for reading.

Graph databases are a more natural way of storing the data. You don’t have to worry about tables and foreign keys and it keeps everything within two simple concepts: nodes and relations. Each node or relation can have its attributes and labels of identification, a way to categorize the data.

To make it easier to understand, let’s map a school in a simple way, where we have students, teachers and courses. How would a diagram represent this? Would you do it in a graph database? It is actually simpler than you think! Just draw it:


Starting with Graph Databases: A Quick look into Neo4j

Now, hands-on! You can install Neo4j on your computer or do it using Docker:

image: neo4j:3.4.5 ports: - "7474:7474" - "7473:7473" - "7687:7687"

Don’t forget to follow the rules and nomenclature recommendations .

Creating our first nodes:

CREATE (s)-[k:KNOWS]->(t), (t)-[ts:TEACHES]->(c), (s)-[e:ENROLLED]->(c) CREATE (user1:Person:Student { name: "Natam" }) CREATE (user2:Person:Teacher { name: "Natalia" }) CREATE (course:Course { title: “Math” })

Now let’s relate them:

MATCH (s:Student {name:"Natam"}), (t:Teacher {name:"Natalia"}), (c:Course {title:"Math"})

Now that we have our data, we need to consult it. Let’s do this?

MATCH (n) RETURN n

This command will return an overview of your graph. You can apply more filters and conditions to your “query”. Check this out: https://neo4j.com/docs/developer-manual/current/cypher/clauses/where/

Conclusion, but already?

Yes! The purpose of this post is for you to get to know this incredible technology and how basic is its structure. If you found it interesting and want to learn more about it, try modeling applications you already know using Neo4j. I’m sure you’ll be even more surprised!

I will leave a few links in case you want to know more concepts related to this technology.

REFERENCES

Semantic Web Wikipedia

The Semantic Web is an extension of the World Wide Web through standards by the World Wide Web Consortium (W3C). The… en.wikipedia.org

Ontology Wikipedia

本文数据库(综合)相关术语:系统安全软件

代码区博客精选文章
分页:12
转载请注明
本文标题:Starting with Graph Databases: A Quick look into Neo4j
本站链接:https://www.codesec.net/view/621286.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(48)