未加星标

User Defined Functions in Neo4j 3.1.0-M10

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

If you used or wrote procedures in the past, you most probably came across instances where it felt quite unwieldy to call a procedure just to compute something, convert a value or provide a boolean decision.

For example:

CREATE (v:Value {id:{id}, data:{data}}) WITH v CALL apoc.date.formatDefault(timestamp(), "ms") YIELD value as created SET v.created = created

You’d rather write it as a function:

CREATE (v:Value {id:{id}, data:{data}, created: apoc.date.format(timestamp()) })

Now in 3.1 that’s possible, and you can also leave off the "ms" and use a single function name, because the unit and format parameters have a default value.

Functions are more limited than procedures: they can’t execute writes or schema operations and are expected to return a single value, not a stream of values.

But this makes it also easier to write and use them.

By having information about their types, the Cypher Compiler can also check for applicability.

The signature of the procedure above changed from:

@Procedure("apoc.date.format") public Stream<StringResult> formatDefault(@Name("time") long time, @Name("unit") String unit) { return Stream.of(format(time, unit, DEFAULT_FORMAT)); }

to the much simpler function signature (ignoring the parameter name and value annotations):

@UserFunction("apoc.date.format") public String format(@Name("time") long time, @Name(value="unit", defaultValue="ms") String unit, @Name(value="format", defaultValue=DEFAULT_FORMAT) String format) { return getFormatter().format(time, unit, format); }

This can then be called in the manner outlined above.

In our APOC procedure library we already converted about 50 procedures into functions from the following areas:

package # of functions example function

date & time conversion

3

apoc.date.parse(&quot;time&quot;,[&quot;unit&quot;],[&quot;format&quot;])

number conversion

3

apoc.number.parse(&quot;number&quot;,[&quot;format&quot;])

general type conversion

8

apoc.convert.toMap(value)

type information and checking

4

apoc.meta.type(value)

collection and map functions

25

apoc.map.fromList([&quot;k1&quot;,v1,&quot;k2&quot;,v2,&quot;k3&quot;,v3])

JSON conversion

4

apoc.convert.toJson(value)

string functions

7

apoc.text.join([&quot;s1&quot;,&quot;s2&quot;,&quot;s3&quot;],&quot;delim&quot;)

hash functions

2

apoc.util.md5(value)

You can list user defined functions with call dbms.functions()


User Defined Functions in Neo4j 3.1.0-M10

We also started to add default values for parameters and deprecate procedures that required alternative names because of a wider parameter set.

We also moved the Description annotation from APOC’s own to the one now provided within Neo4j, so you’ll see the descriptions for all functions and procedures in dbms.procedures() .

All of this is available now, if you run Neo4j 3.1.0-M10 and use this latest 3.1.0.1 APOC release .

You can follow the development of APOC for Neo4j 3.1 in the 3.1 branch .

本文数据库(综合)相关术语:系统安全软件

主题: AU
分页:12
转载请注明
本文标题:User Defined Functions in Neo4j 3.1.0-M10
本站链接:http://www.codesec.net/view/480266.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(61)