Let's continue our interview tour of the NoSQL family this week, this time with our interviewee, Mr. Neo4J.
Why have Neo4J?
All kinds of databases are in fact describing the connection between data in the real world. A typical scenario in the real world is where relational databases are incapable, that is, social networks, such re-connected data. As shown below:
A and B, C, D, and E are all good friends. This is a typical many-to-many relationship in relational databases. For many-to-many relationships, the general relational strategy is to create an intermediate table to describe the many-to-many relationship. many-to-many relationship. However, a more common scenario for describing connections in relational databases is to describe the modeling relationship between different data, such as the relationship between students and curriculum, and the selection table stores other necessary information such as student ID, curriculum ID, and the object of student and curriculum description. are different, the data described in the above figure is the same, and they may all be people in the social circle. How do we store this connection, a self-connected table? Store contact information with him? Like the following:
Then if my current requirement is to query the friends of A's friends, then a simple and mindless operation is that I want to see my friends' friends. This is just one layer, and the corresponding SQL is actually very easy to write:
# 请原谅我还是从Student还是
SELECT * FROM Student WHERE id in (
SELECT studentReleationId FROM student_releation WHERE studentId in
(SELECT studentReleationId FROM student_releation WHERE studentId = '1')
# 转成join的话,先找出我朋友的朋友的Id,然后再做join,或者子查询
SELECT s2.studentReleationId FROM student_releation s1 INNER JOIN student_releation s2 on s1.studentReleationId = s2.studentId
WHERE s1.studentId = '1'
But in fact, join is not recommended. Suppose a person has 100 social friends, and some will actually be higher. If I store personal information for only 30,000 people, then the table for storing contacts is 3 million pieces of data. This is actually Not even cross-acquaintance. For relational databases, the join of large tables is a disaster, but for social software, 30,000 users is a relatively small amount of data. For mature social products, 3 million, 30 million Both are possible. Another example is the movie relationship diagram, which is not impossible for a relational database to check the actors who participated in a director's movie, but after the number increases, our query speed is constantly decreasing, because the relationship between tables and tables is not impossible. The relationship basically relies on joins, and the more joins, the slower the speed. From this, we lead to the application scenario of graph database, many-to-many modeling, and data access. Graph databases generally carry out targeted optimization for relational queries, such as storage model, data structure, query algorithm, etc., to prevent the query of local data from triggering the reading of all data.
Relational databases have different implementations. Like MySQL, Oracle, and SQL Server, graph databases also have different implementations: Neo4j, JanusGraph, HugeGraph. The query language of relational database is SQL, and the query language of graph database is: Gremlin, Cypher. This time we introduce Neo4j.
use it
I first came to Neo4j's official website hoping to have a free trial, because I didn't want to install it myself. Then I saw the free trial:
Then I stopped installing it and started using it directly. Let's first introduce the basic syntax:
create
create node
CREATE (Person { name: "Emil", from: "Sweden", klout: 99 })
Person is the label of this node, and {} is the property of this label.
Create a connection
MATCH (ee:Person) WHERE ee.name = "Emil" # 先查询一次是为了后面建立联系 CREATE (js:Person { name: "Johan", from: "Sweden", learn: "surfing" }), # 创建一个结点,并将js指向该结点 (ir:Person { name: "Ian", from: "England", title: "author" }),# 创建一个结点,并将ir指向 这个结点 (rvb:Person { name: "Rik", from: "Belgium", pet: "Orval" }), (ally:Person { name: "Allison", from: "California", hobby: "surfing" }), (ee)-[:KNOWS {since: 2001}]->(js),(ee)-[:KNOWS {rating: 5}]->(ir), # (变量)-[关系描述]->(变量) 这两个结点建立联系 [中是对这个边的关系描述] KNOWS可以理解为了解,KNOWS{SINCE:2001}表示ee和js认识是在2001年 (js)-[:KNOWS]->(ir),(js)-[:KNOWS]->(rvb), (ir)-[:KNOWS]->(js),(ir)-[:KNOWS]->(ally), (rvb)-[:KNOWS]->(ally)
delete
remove attribute of node
MATCH (a:Person {name:'Emil'}) REMOVE a.name
delete node
MATCH (a:Person {name:'Ian'}) DELETE a
- change
MATCH(ee:Person) where name = 'Johan'
SET ee.name = '张三' RETURN ee;
# 将name = Johan 的name改为张三
check
- Find a single node
MATCH (ee:Person) WHERE ee.name = "Johan" RETURN ee; # 查出结点中name叫Johan的,然后返回
- Find all nodes with Knows relationship
MATCH (n)-[:KNOWS]-() RETURN n
- Find friends of someone's friends, here we consider KNOWS as friends
MATCH (a:Person {name:'Ian'})-[r1:KNOWS]-()-[r2:KNOWS]-(friend_of_a_friend) RETURN friend_of_a_friend.name AS fofName # friend_of_a_friend 是返回的变量
Java Manipulate Neo4J
Java requires a driver to manipulate any database, and the free trial version of Neo4j has corresponding language examples.
Neo4J's driver is similar to JDBC, and we spell SQL and submit it natively. rely:
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>4.1.0</version>
</dependency>
Example:
public class DriverIntroduction implements AutoCloseable{
private final Driver driver;
public DriverIntroduction(String uri, String user, String password, Config config) {
// The driver is a long living object and should be opened during the start of your application
driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password), config);
}
@Override
public void close() throws Exception {
driver.close();
}
public void createFriendship(final String person1Name, final String person2Name) {
// 创建结点 然后建立联系,并返回结点
String createFriendshipQuery = "CREATE (p1:Person { name: $person1_name })\n" +
"CREATE (p2:Person { name: $person2_name })\n" +
"CREATE (p1)-[:KNOWS]->(p2)\n" +
"RETURN p1, p2";
Map<String, Object> params = new HashMap<>();
params.put("person1_name", person1Name);
params.put("person2_name", person2Name);
try (Session session = driver.session()) {
Record record = session.writeTransaction(tx -> {
Result result = tx.run(createFriendshipQuery, params);
return result.single();
});
} catch (Neo4jException ex) {
throw ex;
}
}
}
in conclusion
This article briefly introduces the origin of Neo4J, as well as the basic addition, deletion, modification and search, and Java manipulation of the database. Neo4j has only one database by default. At present, the community version does not seem to support the syntax to create a database, and the configuration file needs to be modified. Neo4J also does not have the concept of a relational database corresponding table. Below the database is the node data. Neo4j also has Chinese documentation:
http://neo4j.com.cn/public/docs/index.html
References
- Will graph databases like Neo4j emerge in China? Why? https://www.zhihu.com/question/19999933/answer/550019788
- Teach you a quick start knowledge map - Neo4J tutorial https://zhuanlan.zhihu.com/p/88745411?utm_source=wechat_session
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。