Cypher descriptive image Query Language

Source: Internet
Author: User
1/** 2 * use cypherparser language 3 * @ author 4 * @ since 2014-8-4 3:22:25 5 */6 Public void usercypherparser () {7 // execute a cypher query 8 graphdatabaseservice graphdb = new graphdatabasefactory (). newembeddeddatabase ("DB/music. DB "); 9 try (transaction Tx = graphdb. begintx () {10/* API Org. neo4j. cypher. javacompat. executionengine: To run a cypher query, use this class. 11 executionengine (graphdatabase Service Database) creates an execution engine around the give graph database 12 method: 13 executionresult execute (string query) 14 executes a query and returns an iterable that contains the result set 15 */16 executionengine engine = new executionengine (graphdb ); 17 // use the cypher language for query ----- A descriptive graph query language 18 // you need to name the mode in the reference section. The different naming sections defined are called identifiers. 19 // All nodes can be obtained using the asterisk (*), which is also applicable to links. 20 system. out. println ("------------------------------ nodes --------------------------"); 21 executionresult result = engine.exe cute ("START n = node (*) return N "); 22 // obtain the result set of the specified column 23 iterator <node> nodeita = result. columnas ("N"); 24 node = NULL; 25 while (nodeita. hasnext () {26 node = nodeita. next (); 27 Info (node + "-; node name =" + node. getproperty ("name"); 28} 29 30 // link data 31 system. out. println ("----- ----------------------- Relationship ---------------------------- "); 32 // you can use the asterisk (*) to obtain all nodes, which is also applicable to links. 33 result = engine.exe cute ("START n = relationship (*) return N"); 34 // obtain the result set of the specified column 35 iterator <relationship> relationshipita = result. columnas ("N"); 36 relationship = NULL; 37 while (relationshipita. hasnext () {38 relationship = relationshipita. next (); 39 Info (Relationship + "-; Start node:" + relationship. getstartnode (). getproperty ("name"); 40} 41 42 // get node 43 through index query/* If the Start Node can be obtained through index query, write as follows: 44 node: indexname (Key = "value "). 45 query: Start n = node: nodes (name = "A") return n the node named a in the index will be returned. 46 */47 system. Out. println ("---------------------------- index query node --------------------------"); 48 // a node index named nodes exists in this column. 49 result = engine.exe cute ("START n = node: nodes (name = 'artist 1's album 1') return N "); 50 // obtain the result set of the specified column 51 nodeita = result. columnas ("N"); 52 while (nodeita. hasnext () {53 node = nodeita. next (); 54 Info (node + "-; node name =" + node. getproperty ("name"); 55} 56 57 // obtain the relation 58 system through the index. out. println ("------------------------------ index query relationship --------------------------"); 59 // you can obtain all nodes by using an asterisk (*), which is also applicable to links. 60 result = engine.exe cute ("START n = relationship: Relationships (publishtime = '2017-10-10 ') return N "); 61 // obtain the result set of the specified column 62 relationshipita = result. columnas ("N"); 63 while (relationshipita. hasnext () {64 relationship = relationshipita. next (); 65 Info (Relationship + "-; Start node:" + relationship. getstartnode (). getproperty ("name"); 66} 67 68 // match -- http://www.uml.org.cn/sjjm/201203063.asp 69/* match Statement 7 0. Declare the image (pattern) in the match part of a query ). The Declaration of the mode causes one or more paths separated by commas ). The 71-node identifier can be used or is not in parentheses. Use parentheses to be exactly the same as do not use parentheses, for example, 72 match (a) --> (B) matches exactly the same pattern as match a --> B. All parts of the 73 mode are directly or indirectly bound to the start point. 74 the optional relationship is an optional description mode method, but it may not match in the real graph (when the node may not exist or there is no such relationship), it will be evaluated as null. 75. Similar to the external join in SQL, if Cypher finds one or more matches, all will be returned. If no match exists, Cypher returns NULL. 76 */77 system. Out. println ("---------------------------- match query and node -- related node ----------------------------"); 78 // There is a node index named nodes in this column. 79 // The symbol-Indicates relevance and does not need to care about the direction and type. 80 // when you are interested in the direction of the link, you can use the --> or <-- symbol, such as: 81 result = engine.exe cute ("START n = node (0) match (N) --> (x) return X "); 82 // obtain the result set of the specified column 83 nodeita = result. columnas ("X"); 84 while (nodeita. hasnext () {85 node = nodeita. next (); 86 Info (node + "-; node name =" + node. getproperty ("name"); 87} 88 89 // targeting link and identifier 90/* If the link identifier is required, in order to filter the link attribute or to return the link, the following example uses an identifier. 91 query: 92 start n = node (3) match (N)-[R]-> () return R 93 all the links connected from node A will be returned. 94 */95 system. out. println ("---------------------------- match query and node -- Relationship between all click nodes of this node --------------------------"); 96 // you can obtain all nodes by asterisk (*), which is also applicable to links. 97 result = engine.exe cute ("START n = node (0) match (N)-[R]-> () return r "); 98 // obtain the result set 99 relationshipita = Result of the specified column. columnas ("R"); 100 while (relationshipita. hasnext () {101 relationship = relationshipita. next (); 102 Info (Relationship + "-; Start node:" + relationship. getstartnode (). getproperty ("name"); 103} 104 105 // All nodes that match a link sent from this node through the link type 106 system. out. println ("---------------------------- match query all nodes of a link sent from this node through the link type --------------------------"); 107 result = engine.exe cute ("START n = node (0) match (N)-[: publish]-> (x) return X "); 108 // obtain the result set of the specified column 109 nodeita = result. columnas ("X"); 110 while (nodeita. hasnext () {111 node = nodeita. next (); 112 Info (node + "-; node name =" + node. getproperty ("name"); 113} 114 115 // uses the link type to match and use the identifier 116 system. out. println ("---------------------------- match matches by link type and uses the identifier ----------------------------"); 117 result = engine.exe cute ("START n = node (0) match (N)-[R: publish]-> () return r "); 118 // obtain the result set of the specified column 119 relationshipita = result. columnas ("R"); 120 while (relationshipita. hasnext () {121 relationship = relationshipita. next (); 122 Info (Relationship + "-; Start node:" + relationship. getstartnode (). getproperty ("name"); 123} 124 125 // multi-relationship --- this solves the problem of the information of all the songs of the singer 126 system. out. println ("------------------------------ match multiple relationships ------------------------"); 127 result = engine.exe cute ("START n = node (0) match (N)-[: publish]-> () -[: contain]-> (x) return X "); 128 // obtain the result set of the specified column 129 nodeita = result. columnas ("X"); 130 while (nodeita. hasnext () {131 node = nodeita. next (); 132 Info (node + "-; node name =" + node. getproperty ("name"); 133} 134 135} finally {136 graphdb. shutdown (); 137} 138}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.