Graphic Database neo4j Development Combat

Source: Internet
Author: User
Tags data structures object model neo4j

NEO4J Introduction

Data storage is generally an integral part of application development. The data that is generated and required in the application run is persisted in a specific format. A common task in application development is to convert between the domain object model of the application itself and the data storage format. If the data storage format is similar to the domain object model, the mapping relationship required for the conversion is more natural and easier to implement. For a particular application, the domain object model is determined by the characteristics of the application itself and is generally modeled in the most natural and intuitive manner. So it is important to choose the data storage format appropriately. The most common data storage format currently available is a relational database. The relational database is modeled by the entity-relationship model (E-R model), which is modeled by the relationship between the table and the table. The number of relational databases that can be used in real development is very substantial, both open and commercial. relational databases are suitable for storing tabular data of type isomorphism for data entries. If the relationships between different objects in the domain object model are more complex, it is necessary to use cumbersome object-relational mapping techniques (object-relationship Mapping,orm) for conversion.

For many applications, the domain object model is not suitable to be stored as a relational database form. This is also the reason that the relational database (NoSQL) is popular. There are many kinds of NoSQL database, including key-value database, document-oriented database and graphic database. The neo4j described in this article is the most important graphic database. NEO4J uses the concept of graph in data structures to model. The two most basic concepts in neo4j are nodes and edges. A node represents an entity, while an edge represents a relationship between entities. Both the node and the edge can have their own properties. Different entities are connected by various relationships to form complex object graphs. NEO4J also provides the ability to find and traverse on an object graph.

For many applications, the domain object model itself is a graph structure. For such applications, it is most appropriate to use a graphics database such as NEO4J, because the cost of the model conversion is minimal. Take the application of social networking as an example, the user as the entity in the application, through different relationships linked together, such as family relations, friends and colleagues relationship. Different relationships have different attributes. For example, the attributes included in a coworker relationship include the name of the company, the start time, and the end time. For such applications, the use of neo4j for data storage, not only to achieve a simple, late maintenance costs are relatively low.

NEO4J uses the most general data structure of the "graph" to model the database so that NEO4J's data models are highly expressive. Data structures such as linked lists, trees, and hashes can be abstracted into graphs. NEO4J also has the basic characteristics of general database, including transaction support, high availability and high performance. NEO4J has been applied in many production environments. The popular cloud application development platform Heroku also provides neo4j as an optional extension.

After a brief introduction of NEO4J, the basic usage of neo4j is described below.

NEO4J Basic Use

Before using neo4j, you need to first understand the basic concepts in neo4j.

Nodes and relationships

The most basic concept in neo4j is node and relationship (relationship). Nodes represent entities and are represented by the Org.neo4j.graphdb.Node interface. There can be different relationships between two nodes. The relationship is represented by the Org.neo4j.graphdb.Relationship interface. Each relationship consists of three elements, such as the starting node, the terminating node, and the type. The presence of the starting and terminating nodes shows that the relationship has a direction, similar to an edge in a directed graph. In some cases, however, the direction of the relationship may not be meaningful and will be ignored during processing. All relationships are of a type and are used to differentiate between nodes of different meanings. When you create a relationship, you specify its type. The type of the relationship is represented by the Org.neo4j.graphdb.RelationshipType interface. Both nodes and relationships can have their own properties. Each property is a simple name-value pair. The name of the property is of type string, and the value of the property can only be a base type, a string type, and an array of base types and string types. A node or relationship can contain any number of attributes. The method for manipulating a property is declared in an interface Org.neo4j.graphdb.PropertyContainer. Both Node and relationship interfaces inherit from the Propertycontainer interface. Methods commonly used in Propertycontainer interfaces include obtaining and setting GetProperty and setProperty of property values. The following is a concrete example to illustrate the use of nodes and relationships.

This example is a simple song information management program for recording singers, songs and albums, and other related information. In this program, the entities include singers, songs, and albums, and relationships include the publishing relationship between the singer and the album, and the relationship between the album and the song. Listing 1 shows an example of using neo4j to manipulate entities and relationships in a program.

Listing 1. Examples of use of nodes and relationships

 private static enum Relationshiptypes implements RelationshipType {PUBLISH, contain} public void Usen 
   Odeandrelationship () {Graphdatabaseservice db = new Embeddedgraphdatabase ("Music"); 
   Transaction tx = Db.begintx (); 
       try {Node Node1 = Db.createnode (); 
       Node1.setproperty ("name", "singer 1"); 
       Node Node2 = Db.createnode (); 
       Node2.setproperty ("Name", "album 1"); 
       Node1.createrelationshipto (Node2, relationshiptypes.publish); 
       Node node3 = Db.createnode (); 
       Node3.setproperty ("Name", "Song 1"); 
       Node2.createrelationshipto (Node3, Relationshiptypes.contain); 
   Tx.success (); 
   finally {tx.finish (); } 
}

In Listing 1, you first define the two types of relationships. The general practice of defining a relationship type is to create an enumeration type that implements the RelationshipType interface. The PUBLISH and contain in relationshiptypes represent the publication and inclusion relationships, respectively. In a Java program, you can start the NEO4J database in an embedded way by simply creating an object for the Org.neo4j.kernel.EmbeddedGraphDatabase class and specifying the storage directory for the database file. When using the NEO4J database, the operations that are modified generally need to be included in a transaction for processing. A new node can be created by the CreateNode method of the Graphdatabaseservice interface. The Createrelationshipto method of the node interface can create a relationship between the current node and another node.

Another concept associated with nodes and relationships is the path. The path has a starting node, followed by several pairs of relationships and node objects. A path is the result of a query or traversal on an object graph. The Org.neo4j.graphdb.Path interface is used in neo4j to represent the path. The Path interface provides operations to handle the nodes and relationships contained therein, including the Startnode and Endnode methods to obtain the start and end nodes, as well as the nodes and relationships methods to obtain a solid of the Iterable interface that traverses all nodes and relationships. Is. A detailed description of the query and traversal on the diagram is given in the following section.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.