這篇具有很好參考價(jià)值的文章主要介紹了neo4j查詢語(yǔ)言Cypher詳解(三)--函數(shù)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。
函數(shù)
Cypher中的函數(shù)如果輸入?yún)?shù)為null,則返回null。
以字符串作為輸入的函數(shù)都對(duì)Unicode字符進(jìn)行操作,而不是對(duì)標(biāo)準(zhǔn)字符進(jìn)行操作。例如,size()函數(shù)應(yīng)用于任何Unicode字符將返回1,即使該字符不適合一個(gè)字符的16位。
可以通過(guò) SHOW FUNCTIONS
查看函數(shù)定義。
函數(shù)簽名中參數(shù)格式:eg:
all(
variable :: VARIABLE //:: VARIABLE,說(shuō)明是個(gè)變量,可用于后面的 WHERE 部分
IN
list :: LIST OF ANY? //list,是個(gè)LIST泛型。
WHERE predicate :: ANY? // predicate,是任意的斷言
)
:: (BOOLEAN?) //函數(shù)返回值類(lèi)型為BOOLEAN
斷言函數(shù)
Function |
Signature |
Description |
all() |
all(variable :: VARIABLE IN list :: LIST OF ANY? WHERE predicate :: ANY?) :: (BOOLEAN?) |
Returns true if the predicate holds for all elements in the given list. |
any() |
any(variable :: VARIABLE IN list :: LIST OF ANY? WHERE predicate :: ANY?) :: (BOOLEAN?) |
Returns true if the predicate holds for at least one element in the given list. |
exists() |
exists(input :: ANY?) :: (BOOLEAN?) |
Returns true if a match for the pattern exists in the graph. |
isEmpty() |
isEmpty(input :: LIST? OF ANY?) :: (BOOLEAN?) |
Checks whether a list is empty. |
isEmpty() |
isEmpty(input :: MAP?) :: (BOOLEAN?) |
Checks whether a map is empty. |
isEmpty() |
isEmpty(input :: STRING?) :: (BOOLEAN?) |
Checks whether a string is empty. |
none() |
none(variable :: VARIABLE IN list :: LIST OF ANY? WHERE predicate :: ANY?) :: (BOOLEAN?) |
Returns true if the predicate holds for no element in the given list. |
single() |
single(variable :: VARIABLE IN list :: LIST OF ANY? WHERE predicate :: ANY?) :: (BOOLEAN?) |
Returns true if the predicate holds for exactly one of the elements in the given list. |
標(biāo)量函數(shù)
Function |
Signature |
Description |
coalesce() |
coalesce(input :: ANY?) :: (ANY?) |
返回第一個(gè)非null值。 |
endNode() |
endNode(input :: RELATIONSHIP?) :: (NODE?) |
Returns the end node of a relationship. |
head() |
head(list :: LIST? OF ANY?) :: (ANY?) |
Returns the first element in a list. |
id() |
id(input :: NODE?) :: (INTEGER?) |
Deprecated Returns the id of a node. Replaced by elementId()
|
|
id(input :: RELATIONSHIP?) :: (INTEGER?) |
Deprecated Returns the id of a relationship. Replaced by elementId() . |
last() |
last(list :: LIST? OF ANY?) :: (ANY?) |
|
length() |
length(input :: PATH?) :: (INTEGER?) |
Returns the length of a path. |
properties() |
properties(input :: MAP?) :: (MAP?) |
返回一個(gè)對(duì)象的所有屬性,作為一個(gè)map |
|
properties(input :: NODE?) :: (MAP?) |
返回一個(gè)節(jié)點(diǎn)的所有屬性,作為一個(gè)map |
|
properties(input :: RELATIONSHIP?) :: (MAP?) |
返回一個(gè)關(guān)系的所有屬性,作為一個(gè)map |
randomUUID() |
randomUUID() :: (STRING?) |
|
size() |
size(input :: LIST? OF ANY?) :: (INTEGER?) |
Returns the number of items in a list. |
|
size(input :: STRING?) :: (INTEGER?) |
Returns the number of Unicode characters in a string. |
startNode() |
startNode(input :: RELATIONSHIP?) :: (NODE?) |
|
toBoolean() |
toBoolean(input :: STRING?) :: (BOOLEAN?) |
|
|
toBoolean(input :: BOOLEAN?) :: (BOOLEAN?) |
|
|
toBoolean(input :: INTEGER?) :: (BOOLEAN?) |
|
toBooleanOrNull() |
toBooleanOrNull(input :: ANY?) :: (BOOLEAN?) |
轉(zhuǎn)換為boolean,不能轉(zhuǎn)換返回null。 |
toFloat() |
toFloat(input :: NUMBER?) :: (FLOAT?) |
|
|
toFloat(input :: STRING?) :: (FLOAT?) |
|
toFloatOrNull() |
toFloatOrNull(input :: ANY?) :: (FLOAT?) |
轉(zhuǎn)換為小數(shù),不能轉(zhuǎn)換返回null。 |
toInteger() |
toInteger(input :: NUMBER?) :: (INTEGER?) |
|
|
toInteger(input :: BOOLEAN?) :: (INTEGER?) |
|
|
toInteger(input :: STRING?) :: (INTEGER?) |
|
toIntegerOrNull() |
toIntegerOrNull(input :: ANY?) :: (INTEGER?) |
轉(zhuǎn)換為整形,不能轉(zhuǎn)換返回null。 |
type() |
type(input :: RELATIONSHIP?) :: (STRING?) |
Returns the string representation of the relationship type. |
示例
CREATE (p:Person {name: 'Stefan', city: 'Berlin'})
RETURN properties(p)
//OUT PUT:
//: {"city":"Berlin","name":"Stefan"}
聚合函數(shù)
Function |
Signature |
Description |
avg() |
avg(input :: DURATION?) :: (DURATION?) |
|
|
avg(input :: FLOAT?) :: (FLOAT?) |
|
|
avg(input :: INTEGER?) :: (INTEGER?) |
|
collect() |
collect(input :: ANY?) :: (LIST? OF ANY?) |
收集數(shù)據(jù)作為一個(gè)LIST。 |
count() |
count(input :: ANY?) :: (INTEGER?) |
|
max() |
max(input :: ANY?) :: (ANY?) |
|
min() |
min(input :: ANY?) :: (ANY?) |
|
percentileCont() |
percentileCont(input :: FLOAT?, percentile :: FLOAT?) :: (FLOAT?) |
|
percentileDisc() |
percentileDisc(input :: FLOAT?, percentile :: FLOAT?) :: (FLOAT?) |
|
|
`percentileDisc(input :: INTEGER?, percentile :: FLOAT?) :: (INTEGER?) |
|
stdev() |
stdev(input :: FLOAT?) :: (FLOAT?) |
|
stdevp() |
stdevp(input :: FLOAT?) :: (FLOAT?) |
|
sum() |
sum(input :: DURATION?) :: (DURATION?) |
|
|
`sum(input :: FLOAT?) :: (FLOAT?) |
|
|
`sum(input :: INTEGER?) :: (INTEGER?) |
|
集合函數(shù)
Function |
Signature |
Description |
keys() |
keys(input :: MAP?) :: (LIST? OF STRING?) |
返回MAP的所有KEY LIST |
|
keys(input :: NODE?) :: (LIST? OF STRING?) |
返回節(jié)點(diǎn)的所有屬性 LIST |
|
keys(input :: RELATIONSHIP?) :: (LIST? OF STRING?) |
返回關(guān)系的所有屬性 LIST |
labels() |
labels(input :: NODE?) :: (LIST? OF STRING?) |
Returns a list containing the string representations for all the labels of a node. |
nodes() |
nodes(input :: PATH?) :: (LIST? OF NODE?) |
Returns a list containing all the nodes in a path. |
range() |
range(start :: INTEGER?, end :: INTEGER?) :: (LIST? OF INTEGER?) |
Returns a list comprising all integer values within a specified range. |
|
range(start :: INTEGER?, end :: INTEGER?, step :: INTEGER?) :: (LIST? OF INTEGER?) |
Returns a list comprising all integer values within a specified range created with step length. |
reduce() |
`reduce(accumulator :: VARIABLE = initial :: ANY?, variable :: VARIABLE IN list :: LIST OF ANY? |
expression :: ANY) :: (ANY?)` |
relationships() |
relationships(input :: PATH?) :: (LIST? OF RELATIONSHIP?) |
Returns a list containing all the relationships in a path. |
reverse() |
reverse(input :: LIST? OF ANY?) :: (LIST? OF ANY?) |
Returns a list in which the order of all elements in the original list have been reversed. |
tail() |
tail(input :: LIST? OF ANY?) :: (LIST? OF ANY?) |
Returns all but the first element in a list. |
toBooleanList() |
toBooleanList(input :: LIST? OF ANY?) :: (LIST? OF BOOLEAN?) |
|
toFloatList() |
toFloatList(input :: LIST? OF ANY?) :: (LIST? OF FLOAT?) |
|
toIntegerList() |
toIntegerList(input :: LIST? OF ANY?) :: (LIST? OF INTEGER?) |
|
toStringList() |
toStringList(input :: LIST? OF ANY?) :: (LIST? OF STRING?) |
|
示例
toBooleanList()
:
RETURN toBooleanList(null) as noList,
toBooleanList([null, null]) as nullsInList,
toBooleanList(['a string', true, 'false', null, ['A','B']]) as mixedList
noList |
nullsInList |
mixedList |
<null> |
[<null>,<null>] |
[<null>,true,false,<null>,<null>] |
總結(jié):
- 參數(shù)不是個(gè)LIST,報(bào)錯(cuò)
- LIST 中的null,不轉(zhuǎn)換,保留
- LIST中的不可轉(zhuǎn)換元素,結(jié)果為null。
- BOOLEAN類(lèi)型的元素,保留原始值
toFloatList
,toIntegerList
,toStringList
與 toBooleanList
規(guī)則類(lèi)似。
數(shù)值函數(shù)
Function |
Signature |
Description |
abs() |
abs(input :: FLOAT?) :: (FLOAT?) |
|
|
abs(input :: INTEGER?) :: (INTEGER?) |
|
ceil() |
ceil(input :: FLOAT?) :: (FLOAT?) |
|
floor() |
floor(input :: FLOAT?) :: (FLOAT?) |
|
isNaN() |
isNaN(input :: FLOAT?) :: (BOOLEAN?) |
Returns true if the floating point number is NaN . |
|
isNaN(input :: INTEGER?) :: (BOOLEAN?) |
|
rand() |
rand() :: (FLOAT?) |
|
round() |
round(input :: FLOAT?) :: (FLOAT?) |
|
|
round(value :: FLOAT?, precision :: NUMBER?) :: (FLOAT?) |
|
|
round(value :: FLOAT?, precision :: NUMBER?, mode :: STRING?) :: (FLOAT?) |
|
sign() |
sign(input :: FLOAT?) :: (INTEGER?) |
|
|
sign(input :: INTEGER?) :: (INTEGER?) |
|
對(duì)數(shù)函數(shù)
Function |
Signature |
Description |
e() |
e() :: (FLOAT?) |
返回e。 |
exp() |
exp(input :: FLOAT?) :: (FLOAT?) |
返回e^n。 |
log() |
log(input :: FLOAT?) :: (FLOAT?) |
返回自然對(duì)數(shù),以e為底 |
log10() |
log10(input :: FLOAT?) :: (FLOAT?) |
返回以10位底的對(duì)數(shù) |
sqrt() |
sqrt(input :: FLOAT?) :: (FLOAT?) |
平方差 |
三角函數(shù)
Function |
Signature |
Description |
acos() |
acos(input :: FLOAT?) :: (FLOAT?) |
|
asin() |
`asin(input :: FLOAT?) :: (FLOAT?) |
|
atan() |
atan(input :: FLOAT?) :: (FLOAT?) |
|
atan2() |
atan2(y :: FLOAT?, x :: FLOAT?) :: (FLOAT?) |
|
cos() |
cos(input :: FLOAT?) :: (FLOAT?) |
|
cot() |
cot(input :: FLOAT?) :: (FLOAT?) |
|
degrees() |
degrees(input :: FLOAT?) :: (FLOAT?) |
將弧度轉(zhuǎn)換為角度 |
haversin() |
haversin(input :: FLOAT?) :: (FLOAT?) |
半正矢計(jì)算 |
pi() |
pi() :: (FLOAT?) |
|
radians() |
radians(input :: FLOAT?) :: (FLOAT?) |
將角度轉(zhuǎn)換為弧度 |
sin() |
sin(input :: FLOAT?) :: (FLOAT?) |
|
tan() |
tan(input :: FLOAT?) :: (FLOAT?) |
|
字符串函數(shù)
Function |
Signature |
Description |
left() |
left(original :: STRING?, length :: INTEGER?) :: (STRING?) |
|
ltrim() |
ltrim(input :: STRING?) :: (STRING?) |
|
replace() |
replace(original :: STRING?, search :: STRING?, replace :: STRING?) :: (STRING?) |
|
reverse() |
reverse(input :: STRING?) :: (STRING?) |
|
right() |
`right(original :: STRING?, length :: INTEGER?) :: (STRING?) |
|
rtrim() |
rtrim(input :: STRING?) :: (STRING?) |
|
split() |
split(original :: STRING?, splitDelimiter :: STRING?) :: (LIST? OF STRING?) |
|
|
split(original :: STRING?, splitDelimiters :: LIST? OF STRING?) :: (LIST? OF STRING?) |
|
substring() |
substring(original :: STRING?, start :: INTEGER?) :: (STRING?) |
從0開(kāi)始 |
|
substring(original :: STRING?, start :: INTEGER?, length :: INTEGER?) :: (STRING?) |
|
toLower() |
toLower(input :: STRING?) :: (STRING?) |
|
toString() |
toString(input :: ANY?) :: (STRING?) |
|
toStringOrNull() |
toStringOrNull(input :: ANY?) :: (STRING?) |
|
toUpper() |
toUpper(input :: STRING?) :: (STRING?) |
|
trim() |
trim(input :: STRING?) :: (STRING?) |
|
時(shí)間函數(shù)
Function |
Signature |
Description |
date() |
date(input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATE?) |
創(chuàng)建DATE |
date.realtime() |
date.realtime(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATE?) |
使用 realtime clock 創(chuàng)建當(dāng)前DATE |
date.statement() |
date.statement(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATE?) |
使用 statement clock 創(chuàng)建當(dāng)前DATE |
date.transaction() |
date.transaction(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATE?) |
使用 transaction clock 創(chuàng)建當(dāng)前DATE |
date.truncate() |
date.truncate(unit :: STRING?, input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?, fields = null :: MAP?) :: (DATE?) |
截?cái)郉ATE |
datetime() |
datetime(input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATETIME?) |
|
datetime.fromepoch() |
datetime.fromepoch(seconds :: NUMBER?, nanoseconds :: NUMBER?) :: (DATETIME?) |
|
datetime.fromepochmillis() |
datetime.fromepochmillis(milliseconds :: NUMBER?) :: (DATETIME?) |
|
datetime.realtime() |
datetime.realtime(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATETIME?) |
|
datetime.statement() |
datetime.statement(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATETIME?) |
|
datetime.transaction() |
datetime.transaction(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATETIME?) |
|
datetime.truncate() |
datetime.truncate(unit :: STRING?, input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?, fields = null :: MAP?) :: (DATETIME?) |
|
localdatetime() |
localdatetime(input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALDATETIME?) |
|
localdatetime.realtime() |
localdatetime.realtime(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALDATETIME?) |
|
localdatetime.statement() |
localdatetime.statement(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALDATETIME?) |
|
localdatetime.transaction() |
localdatetime.transaction(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALDATETIME?) |
|
localdatetime.truncate() |
localdatetime.truncate(unit :: STRING?, input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?, fields = null :: MAP?) :: (LOCALDATETIME?) |
|
localtime() |
localtime(input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALTIME?) |
|
localtime.realtime() |
localtime.realtime(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALTIME?) |
|
localtime.statement() |
localtime.statement(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALTIME?) |
|
localtime.transaction() |
localtime.transaction(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALTIME?) |
|
localtime.truncate() |
localtime.truncate(unit :: STRING?, input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?, fields = null :: MAP?) :: (LOCALTIME?) |
|
time() |
time(input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (TIME?) |
|
time.realtime() |
time.realtime(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (TIME?) |
|
time.statement() |
time.statement(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (TIME?) |
|
time.transaction() |
time.transaction(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (TIME?) |
|
time.truncate() |
time.truncate(unit :: STRING?, input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?, fields = null :: MAP?) :: (TIME?) |
|
時(shí)鐘控制
-
transaction
: 同一個(gè)事務(wù),時(shí)間返回相同值。
-
statement
: 同一個(gè)語(yǔ)句的同一次調(diào)用,時(shí)間返回相同值。
-
realtime
: 系統(tǒng)時(shí)間。
截?cái)鄦挝?/h4>
-
millennium
:千年
-
century
:世紀(jì)
-
decade
:十年
-
year
:
-
weekYear
:
-
quarter
:
-
month
:
-
week
:
-
day
:
-
hour
:
-
minute
:
-
second
:
-
millisecond
: 毫秒
-
microsecond
: 微秒
示例
date([{timezone}])
RETURN date() AS currentDate
RETURN date({timezone: 'America/Los Angeles'}) AS currentDateInLA
date({year [, month, day]})
UNWIND [
date({year: 1984, month: 10, day: 11}),
date({year: 1984, month: 10}),
date({year: 1984})
] AS theDate
RETURN theDate
date({year [, week, dayOfWeek]})
UNWIND [
date({year: 1984, week: 10, dayOfWeek: 3}),
date({year: 1984, week: 10}),
date({year: 1984})
] AS theDate
RETURN theDate
所有時(shí)間函數(shù),都支持通過(guò)一個(gè)Map對(duì)象構(gòu)造時(shí)間實(shí)例。
duration函數(shù)
Function |
Signature |
Description |
duration() |
duration(input :: ANY?) :: (DURATION?) |
|
duration.between() |
duration.between(from :: ANY?, to :: ANY?) :: (DURATION?) |
計(jì)算2個(gè)時(shí)間差 |
duration.inDays() |
duration.inDays(from :: ANY?, to :: ANY?) :: (DURATION?) |
|
duration.inMonths() |
duration.inMonths(from :: ANY?, to :: ANY?) :: (DURATION?) |
|
duration.inSeconds() |
duration.inSeconds(from :: ANY?, to :: ANY?) :: (DURATION?) |
|
示例
duration([ {years, quarters, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds} ])
duration(temporalAmount) // temporalAmount STRING類(lèi)型
duration標(biāo)準(zhǔn)字符串格式:P[nY][nM][nW][nD][T[nH][nM][nS]]
,具體見(jiàn):https://neo4j.com/docs/cypher-manual/current/values-and-types/temporal/#cypher-temporal-specifying-durations文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-633413.html
duration.between(instant1, instant2)
字段
years
quarters
months
weeks
days
hours
minutes
seconds
milliseconds
microseconds
nanoseconds
空間函數(shù)
Function |
Signature |
Description |
point.distance() |
`point.distance(from :: POINT?, to :: POINT?) :: (FLOAT?) |
|
point() - Cartesian 2D |
point(input :: MAP?) :: (POINT?) |
|
point() - Cartesian 3D |
point(input :: MAP?) :: (POINT?) |
|
point() - WGS 84 2D |
`point(input :: MAP?) :: (POINT?) |
|
point() - WGS 84 3D |
`point(input :: MAP?) :: (POINT?) |
|
point.withinBBox() |
point.withinBBox(point :: POINT?, lowerLeft :: POINT?, upperRight :: POINT?) :: (BOOLEAN?) |
|
加載 CSV 函數(shù)
Function |
Signature |
Description |
file() |
file() :: (STRING?) |
返回文件路徑 |
linenumber() |
linenumber() :: (INTEGER?) |
返回行數(shù) |
Graph functions
Function |
Signature |
Description |
graph.names() |
graph.names() :: (LIST? OF STRING?) |
|
graph.propertiesByName() |
graph.propertiesByName(name :: STRING?) :: (MAP?) |
|
graph.byName() |
USE graph.byName(name :: STRING?) |
|
用戶自定義函數(shù)
Type |
Description |
Usage |
Developing |
Scalar |
For each row the function takes parameters and returns a result. |
Using UDF |
Extending Neo4j (UDF) |
Aggregating |
Consumes many rows and produces an aggregated result. |
Using aggregating UDF |
Extending Neo4j (Aggregating UDF) |
附錄
參考
https://neo4j.com/docs/cypher-manual/current/functions/文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-633413.html
到了這里,關(guān)于neo4j查詢語(yǔ)言Cypher詳解(三)--函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!
本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!