
{"id":208,"date":"2016-03-13T11:54:14","date_gmt":"2016-03-13T03:54:14","guid":{"rendered":"http:\/\/www.jiandaima.com\/blog\/?p=208"},"modified":"2016-12-05T15:00:51","modified_gmt":"2016-12-05T07:00:51","slug":"neo4j-3-0-%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/www.jiandaima.com\/blog\/archives\/208.html","title":{"rendered":"Neo4j 3.0 \u5b58\u50a8\u8fc7\u7a0b"},"content":{"rendered":"<p>Neo4j\u00a03.0\u00a0\u63d0\u4f9b\u4e00\u4e2a\u65b0\u7684\u529f\u80fd\u201c\u5b58\u50a8\u8fc7\u7a0b\u201d\uff0c\u8be5\u529f\u80fd\u5e76\u4e0d\u662fNeo4j-Server\u7684\u6269\u5c55\uff0c\u800c\u662f\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u7684\u3002<\/p>\n<p>\u5728\u5199\u8fd9\u7bc7\u6587\u7ae0\u7684\u65f6\u5019\uff0c\u53ea\u80fd\u901a\u8fc7\u9884\u5907\u597d\u7684\u8bed\u53e5\u53bb\u6267\u884c<\/p>\n<p><code>CALL package.procedure(params)<\/code><\/p>\n<p>\u4f46\u662f\u63a5\u4e0b\u6765\u4ed6\u4f1a\u88ab\u5b8c\u5168\u96c6\u6210\u5230Cypher\u8bed\u53e5\u5f53\u4e2d\u3002\u8fd9\u6837\u6211\u68a6\u5c31\u53ef\u4ee5\u901a\u8fc7CALL\u8fd9\u4e2a\u8bed\u53e5\u6216\u8005\u5c06\u5b83\u8f6c\u6362\u6210\u65b9\u6cd5\u8868\u8fbe\u5f0f\uff08\u8fd9\u53ea\u662f\u4e2a\u4eba\u559c\u597d\uff09\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u201c\u5b58\u50a8\u8fc7\u7a0b\u201d\u53ea\u80fd\u4f7f\u7528Java\uff08\u6216\u8005\u5176\u4ed6JVM\u8bed\u8a00\uff09\u3002\u4f60\u53ef\u80fd\u83b7\u8bf4\uff0c\u201cWTF\u00a0\u00a0\u3002\u3002\u3002java\u201d, \u5176\u5b9ejava\u4e5f\u6ca1\u6709\u90a3\u4e48\u4e00\u65e0\u662f\u5904\u3002<\/p>\n<p><a href=\"https:\/\/www.jiandaima.com\/blog\/wp-content\/uploads\/2016\/03\/1128.jpg\" class=\"highslide-image\" onclick=\"return hs.expand(this);\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/03\/1128-300x113.jpg\" alt=\"Neo4j 3.0 \u5b58\u50a8\u8fc7\u7a0b\" width=\"300\" height=\"113\" class=\"aligncenter size-medium wp-image-910\" \/><\/a><\/p>\n<p>\u4e00\u5f00\u59cb\uff0c\u5728\u5efa\u7acb\uff0c\u7f16\u7801\uff0c\u7f16\u8bd1\u4e00\u4e2a\u5b58\u50a8\u8fc7\u7a0b\u5e76\u4e0d\u4f1a\u82b1\u8d39\u592a\u591a\u7684\u7cbe\u529b\uff0c\u53ea\u9700\u8981\u4e0b\u8f7dNeo4j 3.0,\u00a0<a href=\"http:\/\/neo4j.com\/download\/other-releases\" rel=\"nofollow\" target=\"_blank\">3.0.0-M04 milestone<\/a>\u6216\u8005\u6700\u65b0\u7684\u7248\u672c\uff0c\u540c\u65f6JDK\uff0cGradle\u6216\u8005Maven\u4e5f\u9700\u8981\u5b89\u88c5\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528Jake Hansson\u5199\u597d\u7684\u4e00\u4e2a\u6a21\u677f<a href=\"https:\/\/github.com\/neo4j-examples\/neo4j-procedure-template\" rel=\"nofollow\" target=\"_blank\">neo4j-examples<\/a>\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u5feb\u7684\u5f00\u59cb\u6211\u4eec\u7684\u7b2c\u4e00\u4e2a\u7ec3\u4e60\u3002<\/p>\n<p>\u6211\u4eec\u4e3e\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff08<a href=\"https:\/\/github.com\/jexp\/neo4j-procedures-demo\" rel=\"nofollow\" target=\"_blank\">GitHub Repository<\/a>\uff09\uff0c\u4f60\u9700\u8981\u8bf4\u660e\u4e00\u4e0b<code>org.neo4j:neo4j:3.0.0[-M04]\u53ef\u4ee5\u5f97\u5230\u7684\u53d6\u503c\u8303\u56f4\uff0c<\/code>\u6765\u83b7\u5f97\u5fc5\u9700\u7684\u6ce8\u91ca\uff0c\u7136\u540eNeo4j API\u624d\u80fd\u8ba8\u8bba\u6570\u636e\u5e93\u3002<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;dependency&gt;\r\n&lt;groupId&gt;org.neo4j&lt;\/groupId&gt;\r\n&lt;artifactId&gt;neo4j&lt;\/artifactId&gt;\r\n&lt;version&gt;${neo4j.version}&lt;\/version&gt;\r\n&lt;scope&gt;provided&lt;\/scope&gt;\r\n&lt;\/dependency&gt;\r\n<\/pre>\n<p>Gradle \u5982\u4e0b<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nproject.ext {\r\nneo4j_version = \u201c\u201d\r\n}\r\ndependencies {\r\ncompile group: \u201corg.neo4j\u201d, name:\u201dneo4j\u201d, version:project.neo4j_version\r\ntestCompile group: \u201corg.neo4j\u201d, name:\u201dneo4j-kernel\u201d, version:project.neo4j_version, classifier:\u201dtests\u201d\r\ntestCompile group: \u201corg.neo4j\u201d, name:\u201dneo4j-io\u201d, version:project.neo4j_version, classifier:\u201dtests\u201d\r\ntestCompile group: \u201cjunit\u201d, name:\u201djunit\u201d, version:4.12\r\n}\r\n<\/pre>\n<p>\u5982\u679c\u4f60\u60f3\u5199\u4e00\u4e9b\u5176\u4ed6\u7684\u7a0b\u5e8f\uff0c\u4f60\u53ea\u9700\u8981\u91cd\u65b0\u5efa\u7acb\u4e00\u4e2a\u6587\u4ef6\u5939\u548c\u65b0\u7684\u7c7b\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\u5305\u548c\u65b9\u6cd5\u7684\u540d\u5b57\u53d8\u6210\u4e86\u5b58\u50a8\u8fc7\u7a0b\u7684\u540d\u5b57\uff08\u5e76\u4e0d\u662f\u7c7b\u7684\u540d\u5b57\uff09\u3002<\/p>\n<p>\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u5b58\u50a8\u8fc7\u7a0b\u6765\u8ba1\u7b97\u67d0\u4e2a\u6807\u7b7e\u7684\u6700\u5927\u548c\u6700\u5c0f\u5ea6\u6570\u3002<\/p>\n<p>\u901a\u8fc7@Context\u6807\u6ce8\u53ef\u4ee5\u8bb2Neo4j\u7684\u00a0GraphDatabaseService\u00a0\u5b9e\u4f8b\u6ce8\u5165\u5230\u4f60\u7684\u7c7b\u4e2d\u3002\u56e0\u4e3a\u5b58\u50a8\u8fc7\u7a0b\u662f\u65e0\u72b6\u6001\u7684\uff0c\u6240\u4ee5\u58f0\u660e\u975e\u6ce8\u5165\u975e\u9759\u6001\u7684\u5b57\u6bb5\u662f\u4e0d\u5141\u8bb8\u7684\u3002<\/p>\n<p>\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b58\u50a8\u8fc7\u7a0b\u5c06\u547d\u540d\u4e3astats.degree\uff0c\u4ee5CALL stats.degree('User')\u7684\u5f62\u5f0f\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage stats;\r\npublic class GraphStatistics {\r\n@Context private GraphDatabaseService db;\r\n\/\/ Result class\r\npublic static class Degree {\r\npublic String label;\r\n\/\/ note, that \u201cint\u201d values are not supported\r\npublic long count, max, min = Long.MAX_VALUE;\r\n\/\/ method to consume a degree and compute min, max, count\r\nprivate void add(long degree) {\r\nif (degree &lt; min) min = degree;\r\nif (degree &gt; max) max = degree;\r\ncount ++;\r\n}\r\n}\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Procedure\r\npublic Stream&lt;Degree&gt; degree(String label) {\r\n\/\/ create holder class for results\r\nDegree degree = new Degree(label);\r\n\/\/ iterate over all nodes with label\r\ntry (ResourceIterator it = db.findNodes(Label.label(label))) {\r\nwhile (it.hasNext()) {\r\n\/\/ submit degree to holder for consumption (i.e. max, min, count)\r\nd.add(it.next().getDegree());\r\n}\r\n}\r\n\/\/ we only return a \u201cStream\u201d of a single element in this case.\r\nreturn Stream.of(degree);\r\n}\r\n}\r\n<\/pre>\n<p>\u5982\u679c\u4f60\u8981\u5feb\u901f\u7684\u6d4b\u8bd5\u8fd9\u4e2a\u5b58\u50a8\u8fc7\u7a0b\uff0c\u53c8\u4e0d\u60f3\u542f\u52a8\u4e00\u4e2a\u8fdb\u7a0b\u5185\u670d\u52a1\u7aef\u518d\u8fde\u63a5\u4e0a\uff08\u6bd4\u5982\uff0c<a href=\"https:\/\/github.com\/neo4j-examples\/neo4j-procedure-template\" rel=\"nofollow\" target=\"_blank\">\u5b58\u50a8\u8fc7\u7a0b\u6a21\u677f<\/a>\u4e2d\u6f14\u793a\u7684\u901a\u8fc7\u65b0\u7684\u4e8c\u8fdb\u5236\u7684\u901a\u4fe1\u534f\u8bae\uff09\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Neo4j\u6d4b\u8bd5\u5de5\u5177\u96c6\u63d0\u4f9b\u7684Java API\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5199\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u6765\u6d4b\u8bd5\u4e0b\u6211\u4eec\u521a\u5199\u7684\u9177\u70ab\u7684\u5b58\u50a8\u8fc7\u7a0b\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage stats;\r\nclass GraphStatisticsTest {\r\n@Test public void testDegree() {\r\n\/\/ given Alice knowing Bob and Charlie and Dan knowing no-one\r\ndb.execute(\u201cCREATE (alice:User)-&#x5B;:KNOWS]-&gt;(bob:User),(alice)-&#x5B;:KNOWS]-&gt;(charlie:User),(dan:User)\u201d).close();\r\n\/\/ when retrieving the degree of the User label\r\nResult res = db.execute(\u201cCALL stats.degree(\u2018User\u2019)\u201d);\r\n\/\/ then we expect one result-row with min-degree 0 and max-degree 2\r\nassertTrue(res.hasNext());\r\nMap&lt;String,Object&gt; row = res.next();\r\nassertEquals(\u201cUser\u201d, row.get(\u201clabel\u201d));\r\n\/\/ Dan has no friends\r\nassertEquals(0, row.get(\u201cmin\u201d));\r\n\/\/ Alice knows 2 people\r\nassertEquals(2, row.get(\u201cmax\u201d));\r\n\/\/ We have 4 nodes in our graph\r\nassertEquals(4, row.get(\u201ccount\u201d));\r\n\/\/ only one result record was produced\r\nassertFalse(res.hasNext());\r\n}\r\n}\r\n\r\n<\/pre>\n<p>\u5f53\u7136\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b58\u50a8\u8fc7\u7a0b\u6765\u751f\u6210\u66f4\u591a\u7684\u5b58\u50a8\u8fc7\u7a0b\uff0c\u6bd4\u5982\u5176\u4ed6\u539f\u751f\u652f\u6301JVM\u7684\u8bed\u8a00\uff0c\u6bd4\u5982\u00a0<\/p>\n<p>JavaScript via Nashorn\uff0c Clojure\uff0cGroovy\uff0c Scala\uff0c Frege (Haskell)\uff0c (J)Ruby or (J\/P)ython\u3002<\/p>\n<p>\u6211\u7528JavaScript\u5199\u4e86\u4e00\u4e2a\u751f\u6210\u548c\u8fd0\u884c\u5b58\u50a8\u8fc7\u7a0b\u7684\u7a0b\u5e8f\u3002<\/p>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u7528\u5b58\u50a8\u8fc7\u7a0b\u505a\u5f88\u591a\u5176\u4ed6\u5f88\u9177\u7684\u4e8b\u60c5\uff0c\u6bd4\u5982\u4e0b\u9762\u5217\u7684\u8d44\u6e90\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6709\u5199\u4e00\u4e9b\u81ea\u5df1\u7684\u5b58\u50a8\u8fc7\u7a0b\u7684\u60f3\u6cd5\uff0c\u8bf7<a href=\"http:\/\/www.oschina.net\/translate\/michael@neo4j.com\" rel=\"nofollow\" target=\"_blank\">\u8054\u7cfb\u6211\u4eec<\/a>\u3002<\/p>\n<p>\u539f\u6587\u5730\u5740\uff1ahttp:\/\/www.oschina.net\/translate\/neo4j-30-stored-procedures?from=20160313<\/p>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>Neo4j 3.0 \u63d0\u4f9b\u4e00\u4e2a\u65b0\u7684\u529f\u80fd\u201c\u5b58\u50a8\u8fc7\u7a0b\u201d\uff0c\u8be5\u529f\u80fd\u5e76\u4e0d\u662fNeo4j-Server\u7684\u6269\u5c55\uff0c\u800c\u662f\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u7684\u3002<\/p>\n","protected":false},"author":1,"featured_media":910,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[298,299,48],"class_list":["post-208","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-the-development-of-communication-technology","tag-neo4j","tag-299","tag-48"],"post_thumbnail_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/03\/1128.jpg?imageView2\/1\/w\/300\/h\/168#","content_first_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/03\/1128.jpg","post_medium_image_300":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/03\/1128.jpg","post_thumbnail_image_624":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/03\/1128.jpg","post_frist_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/03\/1128.jpg","post_medium_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/03\/1128.jpg","post_large_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/03\/1128.jpg","post_full_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/03\/1128.jpg","post_all_images":[{"imagesurl":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2016\/03\/1128-300x113.jpg","id":"image0"}],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":5,"total_comments":0,"category_name":"\u5f00\u53d1\u6280\u672f\u4ea4\u6d41","post_date":"2016-03-13","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2020\/02\/img_5e44112722964.png","avatarurls":[],"related_posts":[],"pageviews":530,"next_post_id":214,"next_post_title":"\u4e2d\u56fd\u8681\u5251\uff1a\u4e00\u6b3e\u8de8\u5e73\u53f0\u7684\u5f00\u6e90\u7f51\u7ad9\u7ba1\u7406\u5de5\u5177","previous_post_id":195,"previous_post_title":"\u65b0\u4eba\u7ad9\u957f\u5fc5\u5b66\uff1a\u5982\u4f55\u7533\u8bf7\u8c37\u6b4cadsense\u53ef\u4ee5100%\u901a\u8fc7","_links":{"self":[{"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/posts\/208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/comments?post=208"}],"version-history":[{"count":0,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/posts\/208\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/media\/910"}],"wp:attachment":[{"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/media?parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/categories?post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/tags?post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}