图形数据库之---neo4j(非关系型数据库)

Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中。Neo4j也可以被看作是一个高性能的图引擎,该引擎具有成熟数据库的所有特性。程序员工作在一个面向对象的、灵活的网络结构下而不是严格、静态的表中——但是他们可以享受到具备完全的事务特性、企业级的数据库的所有好处。

Neo4j因其嵌入式、高性能、轻量级等优势,越来越受到关注。

图形数据结构

在一个图中包含两种基本的数据类型:Nodes(节点) 和 Relationships(关系)。Nodes 和 Relationships 包含key/value形式的属性。Nodes通过Relationships所定义的关系相连起来,形成关系型网络结构。


     neo4j是一种图数据库,同时它也是一种嵌入式数据库。它对图数据是以节点和边(关系)模式进行存储。每个节点可以包含一系列信息,通过Node类里面的setProperty()方法对节点信息进行存储,Node也可以使用createRelationshipTo()方法实现个节点和其他节点的联系,并且该方法返回的是一个Relationship对象,我们也可以对Relationship设置属性,也就是节点和节点之间的关系属性。什么叫关系属性?例如:person1àperson2,person1和person2的关系可以是朋友也可以是同学还可以是亲人,这里的朋友、同学、亲人就是这里的Relationship的属性。那么关系属性就是描叙两个节点之间的关系类型。这就方便在对节点进行查找的时候对节点进行过滤。



neo4j数据库中的节点类似关系型数据库中的表。neo4j节点之间的关系类似关系型数据库中的2表之间的第三张表,neo4j数据库的节点之间的关系上也可以保存一些2者之间关系的属性键值对。同样,可以为每种关系创建一个名字,把关系归类管理。图形数据库比关系型性能好很多,操作性很好,可以灵活的控制,根据neo4j中Node的不同,我们可以赋予不同的属性

http://docs.neo4j.org/refcard/2.0/

START
START n= node (*)

Start from all nodes.

START n= node ({ids})

Start from one or more nodes specified by id.

START n= node ({id1}), m= node ({id2})

Multiple starting points.

START n= node :nodeIndexName(key={value})

Query the index with an exact query. Use node_auto_indexfor the automatic index.

MATCH
MATCH (n:Person)-[:KNOWS]->(m:Person)
WHERE n.name= "Alice"

Node patterns can contain labels. No START clause is required then.

MATCH (n)-->(m)

Any pattern can be used in MATCH except the ones containing property maps.

MATCH p = (n)-->(m)

Assign a path to p.

WHERE
WHERE n.property <> {value}

Use a predicate to filter.

Predicates
n.property <> {value}

Use comparison operators.

has (n.property)

Use functions.

n.number >= 1 AND n.number <= 10

Use boolean operators to combine predicates.

n:Person

Check for node labels.

identifier IS NULL

Check if something is NULL.

NOT has (n.property) OR n.property = {value}

Either property does not exist or predicate is TRUE.

n.property = {value}

Non-existing property returns NULL, which is only equal to NULL.

n.property =~ "Tob.*"

Regular expression.

(n)-[:KNOWS]->(m)

Make sure the pattern has at least one match.

NOT (n)-[:KNOWS]->(m)

Exclude matches to (n)-[:KNOWS]->(m) from the result.

n.property IN [{value1}, {value2}]

Check if an element exists in a collection.

Predicate Functions
all (x IN collection WHERE has (x.property))

Returns true if the predicate is TRUE for all elements of the collection.

any (x IN collection WHERE has (x.property))

Returns true if the predicate is TRUE for at least one element of the collection.

none (x IN collection WHERE has (x.property))

Returns TRUE if the predicate is FALSE for all elements of the collection.

single (x IN collection WHERE has (x.property))

Returns TRUE if the predicate is TRUE for exactly one element in the collection.

Scalar Functions
length (collection)

Length of the collection.

type (a_relationship)

String representation of the relationship type.

startNode (a_relationship)

Start node of the relationship.

endNode (a_relationship)

End node of the relationship.

coalesce (n.property, {defaultValue})

The first non-NULL expression.

head (collection)

The first element of the collection.

last (collection)

The last element of the collection.

timestamp ()

Milliseconds since midnight, January 1, 1970 UTC.

id (node_or_relationship)

The internal id of the relationship or node.

String Functions
str ({expression})

String representation of the expression.

replace ({original}, {search}, {replacement})

Replace all occurrences of search with replacement. All arguments are be expressions.

substring ({original}, {begin}, {sub_length})

Get part of a string. The sub_length argument is optional.

left ({original}, {sub_length}),
   right ({original}, {sub_length})

The first part of a string. The last part of the string.

trim ({original}), ltrim ({original}),
   rtrim ({original})

Trim all whitespace, or on left or right side.

upper ({original}), lower ({original})

UPPERCASE and lowercase.



RETURN
RETURN *

Return the value of all identifiers.

RETURN n AS columnName

Use alias for result column name.

RETURN DISTINCT n

Return unique rows.

ORDER BY n.property

Sort the result.

ORDER BY n.property DESC

Sort the result in descending order.

SKIP {skip_number}

Skip a number of results.

LIMIT {limit_number}

Limit the number of results.

SKIP {skip_number} LIMIT {limit_number}

Skip results at the top and limit the number of results.

WITH
MATCH (user)-[:FRIEND]-(friend)
WHERE user.name = {name}
WITH user, count (friend) AS friends
WHERE friends > 10
RETURN user

The WITH syntax is similar to RETURN. It separates query parts explicitly, allowing you to declare which identifiers to carry over to the next part.

MATCH (user)-[:FRIEND]-(friend)
WITH user, count (friend) AS friends
ORDER BY friends DESC
SKIP 1 LIMIT 3
RETURN user

You can also use ORDER BYSKIPLIMIT with WITH.

CASE
CASE n.eyes
  WHEN 'blue' THEN 1
  WHEN 'brown' THEN 2
  ELSE 3
END

Return THEN value from the matching WHEN value. TheELSE value is optional, and substituted for NULL if missing.

CASE
  WHEN n.eyes = 'blue' THEN 1
  WHEN n.age < 40 THEN 2
  ELSE 3
END

Return THEN value from the first WHEN predicate evaluating to TRUE. Predicates are evaluated in order.

Collection Functions
labels (n)

The labels of the node.

nodes (path)

The nodes in the path.

relationships (path)

The relationships in the path.

extract (x IN coll | x.prop)

A collection of the value of the expression for each element in the collection.

filter (x IN coll WHERE x.prop <> {value})

A collection of the elements where the predicate is TRUE.

tail (coll)

All but the first element of the collection.

range ({first_num}, {last_num}, {step})

Create a range of numbers. The step argument is optional.

reduce (s = "" , n IN coll | s + n.prop)

Evaluate expression for each element in the collection, accumulate the results.

FOREACH (n IN coll | SET n.marked = TRUE )

Execute a mutating operation for each element in a collection.

Aggregation
count (*)

The number of matching rows.

count (identifier)

The number of non-NULL values.

count ( DISTINCT identifier)

All aggregation functions also take the DISTINCTmodifier, which removes duplicates from the values.

sum (n.property)

Sum numerical values.

avg (n.property)

Calculates the average.

max (n.property)

Maximum numerical value.

min (n.property)

Minimum numerical value.

collect (n.property)

Collection from the values, ignores NULL.

percentile_disc (n.property, {percentile})

Discrete percentile. The percentile argument is from 0.0to 1.0.

percentile_cont (n.property, {percentile})

Continuous percentile.

stdev (n.property)

Standard deviation for a sample of a population.

stdevp (n.property)

Standard deviation for an entire population.


Write-Only Query Structure
( CREATE [ UNIQUE ] | MERGE )*
[ SET | DELETE | FOREACH ]*
[ RETURN [ ORDER BY ] [ SKIP ] [ LIMIT ]]
Read-Write Query Structure
[ START ]
[ MATCH ]
[ WHERE ]
[ WITH [ ORDER BY ] [ SKIP ] [ LIMIT ]]
[ CREATE [ UNIQUE ]| MERGE ]*
[ SET | DELETE | FOREACH ]*
[ RETURN [ ORDER BY ] [ SKIP ] [ LIMIT ]]
CREATE
CREATE (n {name: {value}})

Create a node with the given properties.

CREATE (n {map})

Create a node with the given properties.

CREATE (n {collectionOfMaps})

Create nodes with the given properties.

CREATE (n)-[r:KNOWS]->(m)

Create a relationship with the given type and direction; bind an identifier to it.

CREATE (n)-[:LOVES {since: {value}}]->(m)

Create a relationship with the given type, direction, and properties.

CREATE UNIQUE
CREATE UNIQUE
     (n)-[:KNOWS]->(m {property: {value}})

Match pattern or create it if it does not exist. The pattern can not include any optional parts.

MERGE
MERGE (n:Person {property: {value}})
ON CREATE n SET n.created = timestamp ()
ON MATCH  n SET n.access  = n.access+1

Match pattern or create it if it does not exist. Use ON CREATE n and ON MATCH n for conditional updates.

SET
SET n.property={value}

Update or create a property.

SET n={map}

Set all properties. This will remove any existing properties.

SET n:Person

Adds a label Person to a node.

DELETE
DELETE n, r

Delete a node and a relationship.

REMOVE n:Person

Remove a label from n.

REMOVE n.property

Remove a property.

INDEX
CREATE INDEX ON :Person(name)

Create an index on the label Person and property name.

DROP INDEX ON :Person(name)

Drop the index on the label Person and property name.

MATCH (n:Person) WHERE n.name = {value}

An index can be automatically used for the equality comparison. Note that for example lower(n.name) = {value} will not use an index.

Mathematical Functions
abs ({expr})

The absolute value.

rand ()

A random value. Returns a new value for each call. Also useful for selecting subset or random ordering.

round ({expr})

Round to the nearest integer.

floor ({expr})

The greatest integer less than or equal to a decimal number.

ceil ({expr})

The smallest integer greater than or equal to a decimal number.

sqrt ({expr})

The square root.

sign ({expr})

0 if zero, -1 if negative, 1 if positive.

sin ({expr})

Trigonometric functions, also costancotasinacos,atanatan2({y-expr}, x-expr}).

degrees ({expr}), radians ({expr}), pi ()

Converts radians into degrees, use radians for the reverse. pi for π.

log10 ({expr}), log ({expr}), exp ({expr}), e ()

Logarithm base 10, natural logarithm, e to the power of the parameter. Value of e.

相关文章
相关标签/搜索