- 浏览: 2486406 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
neo4j(1)Introduction to neo4j and sample project
1. Introduction to Graph Database
Node ------relationship -------> property
name=wheel number=4 name=car,color=red,
A Graph --- records data in ---->Nodes -----which have ----> Properties
Nodes ------are organized by -----> Relationships ---- which also have -----> Properties
A Traversal ---- navigates ----> a Graph; it ----identifies ---> Paths ---- which order -----> Nodes
An Index ----maps from ----> Properties ---to either ----> Nodes or Relationships
A Graph Database ---- manages a ----> Graph and ----also manages related ---> Indexs
2. The domain Layer
The User Object
@NodeEntity
public class Userneo4j {
@GraphId
private Long id;
private String firstName;
private String lastName;
@Indexed
private String username;
private String password;
@Fetch @RelatedTo(type = "HAS_ROLENEO4J")
private Roleneo4j roleneo4j;
public Userneo4j() {}
public Userneo4j(String username) {
this.username = username;
}
public Userneo4j(String username, String firstName, String lastName, Roleneo4j roleneo4j) {
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
this.roleneo4j = roleneo4j;
}
public Userneo4j(String username, String password, String firstName, String lastName, Roleneo4j roleneo4j) {
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.roleneo4j = roleneo4j;
}
...snip...
The Role domain:
@NodeEntity
public class Roleneo4j {
@GraphId
private Long id;
private Userneo4j userneo4j;
private Integer role;
public Roleneo4j() {
}
public Roleneo4j(Integer role) {
this.role = role;
}
...snip...
The relationship domain object:
@RelationshipEntity(type = "HAS_ROLENEO4J")
public class Userneo4jRoleneo4jRelationship {
private String description;
@StartNode
private Userneo4j userneo4j;
@EndNode
private Roleneo4j roleneo4j;
...snip...
@NodeEntity annotation is used to turn a POJO class into an entity backed by a node in the graph database. Fields on the entity are by default mapped to properties of the node.
@GraphId
For the simple mapping this is a required field which must be of type Long. It is used by Spring Data Neo4j to store the node or relationship-id to re-connect the entity to the graph.
The @Indexed annotation can be declared on fields that are intended to be indexed by the Neo4j indexing facilities.
@Fetch
To have the collections of relationships being read eagerly ... we have to annotate it with the @Fetch annotation.
@RelatedTo: Connecting node entities
Every field of a node entity that references one or more other node entities is backed by relationships in the graph.
@RelationshipEntity, making them relationship entities. Just as node entities represent nodes in the graph, relationship entities represent relationships.
3. The Service Layer
Init Service to add the init data:
package com.sillycat.easynosql.dao.neo4j.init;
import org.springframework.beans.factory.annotation.Autowired;
import com.sillycat.easynosql.dao.neo4j.model.Roleneo4j;
import com.sillycat.easynosql.dao.neo4j.model.Userneo4j;
import com.sillycat.easynosql.dao.neo4j.repository.Userneo4jRepository;
public class InitNeo4jService {
@Autowired
private Userneo4jRepository userneo4jRepository;
public void init() {
if (userneo4jRepository.findByUsername("john") != null) {
userneo4jRepository.delete(userneo4jRepository
.findByUsername("john"));
}
if (userneo4jRepository.findByUsername("jane") != null) {
userneo4jRepository.delete(userneo4jRepository
.findByUsername("jane"));
}
// Create new records
Userneo4j john = new Userneo4j();
john.setFirstName("John");
john.setLastName("Smith");
john.setPassword("111111");
john.setRoleneo4j(new Roleneo4j(1));
john.setUsername("john");
Userneo4j jane = new Userneo4j();
jane.setFirstName("Jane");
jane.setLastName("Adams");
jane.setPassword("111111");
jane.setRoleneo4j(new Roleneo4j(2));
jane.setUsername("jane");
john.getRoleneo4j().setUserneo4j(john);
jane.getRoleneo4j().setUserneo4j(jane);
userneo4jRepository.save(john);
userneo4jRepository.save(jane);
userneo4jRepository.findByUsername("john").getRoleneo4j().getRole();
}
}
The Service will access the data via repository classes:
package com.sillycat.easynosql.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.conversion.EndResult;
import com.sillycat.easynosql.dao.neo4j.model.Userneo4j;
import com.sillycat.easynosql.dao.neo4j.repository.Roleneo4jRepository;
import com.sillycat.easynosql.dao.neo4j.repository.Userneo4jRepository;
import com.sillycat.easynosql.model.User;
import com.sillycat.easynosql.model.convert.UserConvert;
import com.sillycat.easynosql.service.UserService;
public class UserServiceNeo4jImpl implements UserService{
@Autowired
private Userneo4jRepository userneo4jRepository;
@Autowired
private Roleneo4jRepository roleneo4jRepository;
public User create(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
if (existingUser != null) {
throw new RuntimeException("Record already exists!");
}
Userneo4j saveUser = UserConvert.convertUser2Userneo4j(user);
saveUser.getRoleneo4j().setUserneo4j(saveUser);
userneo4jRepository.save(saveUser);
return UserConvert.convertUserneo4j2User(saveUser);
}
public User read(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
user = UserConvert.convertUserneo4j2User(existingUser);
return user;
}
public List<User> readAll() {
List<User> users = new ArrayList<User>();
EndResult<Userneo4j> results = userneo4jRepository.findAll();
for (Userneo4j r : results) {
users.add(UserConvert.convertUserneo4j2User(r));
}
return users;
}
public User update(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
if (existingUser == null) {
return null;
}
existingUser.setFirstName(user.getFirstName());
existingUser.setLastName(user.getLastName());
existingUser.getRoleneo4j().setRole(user.getRole().getRole());
roleneo4jRepository.save(existingUser.getRoleneo4j());
userneo4jRepository.save(existingUser);
return UserConvert.convertUserneo4j2User(existingUser);
}
public Boolean delete(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
if (existingUser == null) {
return false;
}
userneo4jRepository.delete(existingUser);
return true;
}
}
references:
http://www.infoq.com/cn/articles/graph-nosql-neo4j
http://krams915.blogspot.com/2012/03/spring-mvc-31-implement-crud-with_3045.html
https://github.com/krams915/spring-neo4j-tutorial.git
http://neo4j.org/
http://www.iteye.com/topic/978371
http://agapple.iteye.com/blog/1128400
http://blog.csdn.net/jdream314/article/details/6633655
http://hi.baidu.com/luohuazju/blog/item/085eff062ac71475020881d3.html
1. Introduction to Graph Database
Node ------relationship -------> property
name=wheel number=4 name=car,color=red,
A Graph --- records data in ---->Nodes -----which have ----> Properties
Nodes ------are organized by -----> Relationships ---- which also have -----> Properties
A Traversal ---- navigates ----> a Graph; it ----identifies ---> Paths ---- which order -----> Nodes
An Index ----maps from ----> Properties ---to either ----> Nodes or Relationships
A Graph Database ---- manages a ----> Graph and ----also manages related ---> Indexs
2. The domain Layer
The User Object
@NodeEntity
public class Userneo4j {
@GraphId
private Long id;
private String firstName;
private String lastName;
@Indexed
private String username;
private String password;
@Fetch @RelatedTo(type = "HAS_ROLENEO4J")
private Roleneo4j roleneo4j;
public Userneo4j() {}
public Userneo4j(String username) {
this.username = username;
}
public Userneo4j(String username, String firstName, String lastName, Roleneo4j roleneo4j) {
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
this.roleneo4j = roleneo4j;
}
public Userneo4j(String username, String password, String firstName, String lastName, Roleneo4j roleneo4j) {
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.roleneo4j = roleneo4j;
}
...snip...
The Role domain:
@NodeEntity
public class Roleneo4j {
@GraphId
private Long id;
private Userneo4j userneo4j;
private Integer role;
public Roleneo4j() {
}
public Roleneo4j(Integer role) {
this.role = role;
}
...snip...
The relationship domain object:
@RelationshipEntity(type = "HAS_ROLENEO4J")
public class Userneo4jRoleneo4jRelationship {
private String description;
@StartNode
private Userneo4j userneo4j;
@EndNode
private Roleneo4j roleneo4j;
...snip...
@NodeEntity annotation is used to turn a POJO class into an entity backed by a node in the graph database. Fields on the entity are by default mapped to properties of the node.
@GraphId
For the simple mapping this is a required field which must be of type Long. It is used by Spring Data Neo4j to store the node or relationship-id to re-connect the entity to the graph.
The @Indexed annotation can be declared on fields that are intended to be indexed by the Neo4j indexing facilities.
@Fetch
To have the collections of relationships being read eagerly ... we have to annotate it with the @Fetch annotation.
@RelatedTo: Connecting node entities
Every field of a node entity that references one or more other node entities is backed by relationships in the graph.
@RelationshipEntity, making them relationship entities. Just as node entities represent nodes in the graph, relationship entities represent relationships.
3. The Service Layer
Init Service to add the init data:
package com.sillycat.easynosql.dao.neo4j.init;
import org.springframework.beans.factory.annotation.Autowired;
import com.sillycat.easynosql.dao.neo4j.model.Roleneo4j;
import com.sillycat.easynosql.dao.neo4j.model.Userneo4j;
import com.sillycat.easynosql.dao.neo4j.repository.Userneo4jRepository;
public class InitNeo4jService {
@Autowired
private Userneo4jRepository userneo4jRepository;
public void init() {
if (userneo4jRepository.findByUsername("john") != null) {
userneo4jRepository.delete(userneo4jRepository
.findByUsername("john"));
}
if (userneo4jRepository.findByUsername("jane") != null) {
userneo4jRepository.delete(userneo4jRepository
.findByUsername("jane"));
}
// Create new records
Userneo4j john = new Userneo4j();
john.setFirstName("John");
john.setLastName("Smith");
john.setPassword("111111");
john.setRoleneo4j(new Roleneo4j(1));
john.setUsername("john");
Userneo4j jane = new Userneo4j();
jane.setFirstName("Jane");
jane.setLastName("Adams");
jane.setPassword("111111");
jane.setRoleneo4j(new Roleneo4j(2));
jane.setUsername("jane");
john.getRoleneo4j().setUserneo4j(john);
jane.getRoleneo4j().setUserneo4j(jane);
userneo4jRepository.save(john);
userneo4jRepository.save(jane);
userneo4jRepository.findByUsername("john").getRoleneo4j().getRole();
}
}
The Service will access the data via repository classes:
package com.sillycat.easynosql.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.conversion.EndResult;
import com.sillycat.easynosql.dao.neo4j.model.Userneo4j;
import com.sillycat.easynosql.dao.neo4j.repository.Roleneo4jRepository;
import com.sillycat.easynosql.dao.neo4j.repository.Userneo4jRepository;
import com.sillycat.easynosql.model.User;
import com.sillycat.easynosql.model.convert.UserConvert;
import com.sillycat.easynosql.service.UserService;
public class UserServiceNeo4jImpl implements UserService{
@Autowired
private Userneo4jRepository userneo4jRepository;
@Autowired
private Roleneo4jRepository roleneo4jRepository;
public User create(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
if (existingUser != null) {
throw new RuntimeException("Record already exists!");
}
Userneo4j saveUser = UserConvert.convertUser2Userneo4j(user);
saveUser.getRoleneo4j().setUserneo4j(saveUser);
userneo4jRepository.save(saveUser);
return UserConvert.convertUserneo4j2User(saveUser);
}
public User read(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
user = UserConvert.convertUserneo4j2User(existingUser);
return user;
}
public List<User> readAll() {
List<User> users = new ArrayList<User>();
EndResult<Userneo4j> results = userneo4jRepository.findAll();
for (Userneo4j r : results) {
users.add(UserConvert.convertUserneo4j2User(r));
}
return users;
}
public User update(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
if (existingUser == null) {
return null;
}
existingUser.setFirstName(user.getFirstName());
existingUser.setLastName(user.getLastName());
existingUser.getRoleneo4j().setRole(user.getRole().getRole());
roleneo4jRepository.save(existingUser.getRoleneo4j());
userneo4jRepository.save(existingUser);
return UserConvert.convertUserneo4j2User(existingUser);
}
public Boolean delete(User user) {
Userneo4j existingUser = userneo4jRepository.findByUsername(user
.getUsername());
if (existingUser == null) {
return false;
}
userneo4jRepository.delete(existingUser);
return true;
}
}
references:
http://www.infoq.com/cn/articles/graph-nosql-neo4j
http://krams915.blogspot.com/2012/03/spring-mvc-31-implement-crud-with_3045.html
https://github.com/krams915/spring-neo4j-tutorial.git
http://neo4j.org/
http://www.iteye.com/topic/978371
http://agapple.iteye.com/blog/1128400
http://blog.csdn.net/jdream314/article/details/6633655
http://hi.baidu.com/luohuazju/blog/item/085eff062ac71475020881d3.html
发表评论
-
Stop Update Here
2020-04-28 09:00 260I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 430NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 310Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 321Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 291Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 378Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 373Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 325Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 397VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 334Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 415NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 359Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 291Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 207GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 390GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 274GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 263Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 259Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 250Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 234Serverless with NodeJS and Tenc ...
相关推荐
neo4j project
Beginning Neo4j is your introduction in the world of graph databases, and the benefits they can bring to your applications. Neo4j is the most established graph database on the market, and it's always ...
Starting with a practical and vital introduction to Neo4j and various aspects of Neo4j installation, you will learn how to connect and access Neo4j servers from programming languages such as Java, ...
This book is your introduction in the world of graph databases, and the benefits they can bring to your applications. Neo4j is the most established graph database on the market, and it's always ...
With a sample demonstration and outline of community developed tools, this book will help you develop cutting-edge, high performance, and secure applications for complex data using the Neo4j graph ...
Py2neo is a simple and pragmatic Python library that provides access to the popular graph database Neo4j via its RESTful web service interface. This brings with it a heavily refactored core, a cleaner...
Chapter 1, Graph Theory and Databases, explains the fundamental theoretical and historical underpinnings of graph database technology. Additionally, this chapter positions graph databases in an ever-...
• Query tuning — Learn to analyze queries and tune them for performance. • Execution plans — Cypher execution plans and operators. • Deprecations, additions and compatibility — An overview...
neo4j社区版 用户名 neo4j 密码neo4j
This practical book walks you through hands-on examples of how to use graph algorithms in Apache Spark and Neo4j—two of the most common choices for graph analytics. Also included: sample code and ...
Neo4j Java Reference 3.0
Neo4j图数据库_1
基于neo4j搭建金融风控图谱.rar基于neo4j搭建金融风控图谱.rar基于neo4j搭建金融风控图谱.rar基于neo4j搭建金融风控图谱.rar基于neo4j搭建金融风控图谱.rar基于neo4j搭建金融风控图谱.rar基于neo4j搭建金融风控图谱....
vue+neo4j+(neovis.js / neo4j-driver)纯前端实现知识图谱的集成 一、Neovis.js 不用获取数据直接连接数据库绘图 二、 neo4j-driver 能够直接通过前端获取数据。 三、vis.js 绘图 四、 echarts绘图 neo4j是什么? ...
Neo4j文档 包括中英文文档 共两份 欢迎大家下载..
1. Neo4j的亮点 2. 图数据库概要 3. Neo4j图数据库 II. 教程 4. 在Java应用中使用Neo4j 5. Neo4j远程客户端库 6. 遍历查询框架 7. 数据模型范例 8. 多语言支持 9. 在Python应用中使用Neo4j 10. ...
完整项目导入数据到Neo4j,通过jdbc查询Neo4j数据库,用SSM框架展示到前台,项目部署后可在前台批量导入数据,增量导入数据,添加节点,删除节点,修改节点,查询节点 有了它再也不用趟Neo4j的坑 解压密码是 neo4j
Following on from that, you will be introduced to Neo4j and you will be shown how to install Neo4j on various operating systems. You will then be shown how you can model and import your data into ...
neo4j api neo4j学习资料 neo4j教程 │ neo4j官方API(官方各种API的文档整理).7z │ neo4j数据迁移--初探(一).htm │ neo4j笔记.docx │ neo4j错误码状态码.html │ └─01.neo4j学习博客汇总 │ index.html └...