`
sillycat
  • 浏览: 2486406 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

neo4j(1)Introduction to neo4j and sample project

 
阅读更多
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

分享到:
评论

相关推荐

    neo4j project

    neo4j project

    Beginning.Neo4j.1484212

    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 ...

    Neo4j Cookbook(PACKT,2015)

    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, ...

    Beginning Neo4j(Apress,2016)

    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 ...

    Neo4j High Performance(PACKT,2015)

    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 ...

    Building Web Applications with Python and Neo4j(PACKT,2015)

    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...

    Learning Neo4j 3.x, 2nd Edition-Packt Publishing(2017).epub

    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-...

    neo4j cypher manual docment

    • 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社区版

    neo4j社区版 用户名 neo4j 密码neo4j

    Graph Algorithms: Practical Examples in Apache Spark and 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 Java Reference 3.0

    Neo4j图数据库 1

    Neo4j图数据库_1

    基于neo4j搭建金融风控图谱.rar

    基于neo4j搭建金融风控图谱.rar基于neo4j搭建金融风控图谱.rar基于neo4j搭建金融风控图谱.rar基于neo4j搭建金融风控图谱.rar基于neo4j搭建金融风控图谱.rar基于neo4j搭建金融风控图谱.rar基于neo4j搭建金融风控图谱....

    vue+neo4j +纯前端(neovis.js / neo4j-driver) 实现 知识图谱的集成 大干货

    vue+neo4j+(neovis.js / neo4j-driver)纯前端实现知识图谱的集成 一、Neovis.js 不用获取数据直接连接数据库绘图 二、 neo4j-driver 能够直接通过前端获取数据。 三、vis.js 绘图 四、 echarts绘图 neo4j是什么? ...

    Neo4j学习-Neo4j入门-Neo4j文档

    Neo4j文档 包括中英文文档 共两份 欢迎大家下载..

    Neo4j中文手册.zip

    1. Neo4j的亮点 2. 图数据库概要 3. Neo4j图数据库 II. 教程 4. 在Java应用中使用Neo4j 5. Neo4j远程客户端库 6. 遍历查询框架 7. 数据模型范例 8. 多语言支持 9. 在Python应用中使用Neo4j 10. ...

    SSM+Neo4j+Echarts完整版

    完整项目导入数据到Neo4j,通过jdbc查询Neo4j数据库,用SSM框架展示到前台,项目部署后可在前台批量导入数据,增量导入数据,添加节点,删除节点,修改节点,查询节点 有了它再也不用趟Neo4j的坑 解压密码是 neo4j

    Learning Neo4j(PACKT,2014)

    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学习资料汇总(各种优质博文和neo4j教程整理)

    neo4j api neo4j学习资料 neo4j教程 │ neo4j官方API(官方各种API的文档整理).7z │ neo4j数据迁移--初探(一).htm │ neo4j笔记.docx │ neo4j错误码状态码.html │ └─01.neo4j学习博客汇总 │ index.html └...

Global site tag (gtag.js) - Google Analytics