图形数据库之---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. Usenode_auto_indexfor the automatic index.

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

Node patterns can contain labels. NoSTARTclause is required then.

MATCH (n)-->(m)

Any pattern can be used inMATCHexcept the ones containing property maps.

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

Assign a path top.

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

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

Either property does not exist or predicate isTRUE.

n.property = {value}

Non-existing property returnsNULL, which is only equal toNULL.

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

Returnstrueif the predicate isTRUEfor all elements of the collection.

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

Returnstrueif the predicate isTRUEfor at least one element of the collection.

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

ReturnsTRUEif the predicate isFALSEfor all elements of the collection.

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

ReturnsTRUEif the predicate isTRUEfor 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-NULLexpression.

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 ofsearchwithreplacement. All arguments are be expressions.

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

Get part of a string. Thesub_lengthargument 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

TheWITHsyntax is similar toRETURN. 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 useORDER BY,SKIP,LIMITwithWITH.

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

ReturnTHENvalue from the matchingWHENvalue. TheELSEvalue is optional, and substituted forNULLif missing.

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

ReturnTHENvalue from the firstWHENpredicate evaluating toTRUE. 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 isTRUE.

tail (coll)

All but the first element of the collection.

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

Create a range of numbers. Thestepargument 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-NULLvalues.

count ( DISTINCT identifier)

All aggregation functions also take theDISTINCTmodifier, 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, ignoresNULL.

percentile_disc (n.property, {percentile})

Discrete percentile. Thepercentileargument is from0.0to1.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. UseON CREATE nandON MATCH nfor 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 labelPersonto a node.

DELETE
DELETE n, r

Delete a node and a relationship.

REMOVE n:Person

Remove a label fromn.

REMOVE n.property

Remove a property.

INDEX
CREATE INDEX ON :Person(name)

Create an index on the labelPersonand propertyname.

DROP INDEX ON :Person(name)

Drop the index on the labelPersonand propertyname.

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

An index can be automatically used for the equality comparison. Note that for examplelower(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})

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

sin ({expr})

Trigonometric functions, alsocos,tan,cot,asin,acos,atan,atan2({y-expr}, x-expr}).

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

Converts radians into degrees, useradiansfor the reverse.pifor π.

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

Logarithm base 10, natural logarithm,eto the power of the parameter. Value ofe.

如有转载,请标明来自此出处http://blog.youkuaiyun.com/qxs965266509,必须注意!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值