{"id":1291,"date":"2024-08-25T10:45:33","date_gmt":"2024-08-25T02:45:33","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=1291"},"modified":"2024-08-25T22:41:39","modified_gmt":"2024-08-25T14:41:39","slug":"%e7%94%a8-cypher-%e6%9d%a5%e6%9f%a5%e8%af%a2%e5%9b%be%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=1291","title":{"rendered":"\u7528 Cypher \u6765\u67e5\u8be2\u56fe\u6570\u636e\u5e93"},"content":{"rendered":"<h1>Cyper<\/h1>\n<p>\u4f5c\u4e3a\u58f0\u660e\u5f0f\u67e5\u8be2\u8bed\u8a00,  SQL \u5728\u8ba1\u7b97\u673a\u884c\u4e1a\u65e0\u4eba\u4e0d\u6653, \u65e0\u4eba\u4e0d\u77e5.  \u800c <strong>Cypher<\/strong> \u5c31\u662f Graph Database \u56fe\u6570\u636e\u5e93\u7684 SQL.<\/p>\n<blockquote>\n<p>Cypher is unique because it provides a visual way of matching patterns and relationships.<\/p>\n<p>Cypher was inspired by an ASCII-art type of syntax where <code>(nodes)-[:ARE_CONNECTED_TO]-&gt;(otherNodes)<\/code> using rounded brackets for circular <code>(nodes)<\/code>, and <code>-[:ARROWS]-&gt;<\/code> for relationships. <\/p>\n<\/blockquote>\n<p>Cypher \u7528&quot;\u5706\u62ec\u53f7&quot;\u6765\u8868\u793a\u8282\u70b9, \u7528&quot;\u65b9\u62ec\u53f7,\u8fde\u63a5\u7ebf\u53ca\u7bad\u5934&quot;\u8868\u793a\u5173\u7cfb<\/p>\n<blockquote>\n<p>When you write a query, you draw a graph pattern through your data.<\/p>\n<\/blockquote>\n<p>\u8fd9\u6837\u4e00\u53e5\u8bdd - &quot;Sally likes Graphs. Sally is friends with John. Sally works for Neo4j.&quot;  \u8868\u793a\u4e3a\u56fe\u6570\u636e\u5e93\u4e2d\u7684\u8282\u70b9\u548c\u5173\u7cfb<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.fanyamin.com\/wordpress\/wp-content\/uploads\/2024\/08\/image-1724553438413.png\" alt=\"file\" \/><\/p>\n<h2>\u5982\u4f55\u8868\u793a\u8282\u70b9<\/h2>\n<p>\u5706\u62ec\u53f7\u8868\u793a\u8282\u70b9, \u5176\u4e2d\u8282\u70b9\u7684\u6807\u7b7e label \u53ef\u4ee5\u7528 &quot;node:label&quot; \u6765\u8868\u793a<br \/>\n\u4f8b\u5982:<\/p>\n<pre><code>()                  \/\/anonymous node (no label or variable) can refer to any node in the database\n(p:Person)          \/\/using variable p and label Person\n(:Technology)       \/\/no variable, label Technology\n(work:Company)      \/\/using variable work and label Company<\/code><\/pre>\n<h2>\u5982\u4f55\u8868\u793a\u5173\u7cfb<\/h2>\n<p>\u6709\u5411\u56fe\u4e2d\u7684\u5173\u7cfb\u5c31\u662f\u7528\u7bad\u5934\u6765\u8868\u793a\u7684, Cypher \u4f7f\u7528\u7bad\u5934  <code>--&gt;<\/code> \u6216  <code>&lt;--<\/code> \u6765\u8fde\u63a5\u4e24\u4e2a\u8282\u70b9.<br \/>\n\u800c\u6ca1\u6709\u7bad\u5934\u7684\u8fde\u63a5\u7ebf <code>--<\/code> \u8868\u793a\u8282\u70b9\u4e4b\u95f4\u7684\u5173\u7cfb\u662f\u53cc\u5411\u7684<\/p>\n<p>\u521b\u5efa\u548c\u67e5\u8be2 &quot;Person&quot; \u4e0e &quot;Technology&quot; \u4e4b\u95f4\u5173\u7cfb\u7684\u8bed\u53e5\u5982\u4e0b<\/p>\n<pre><code>\/\/data stored with this direction \u521b\u5efa\u5173\u7cfb\nCREATE (p:Person)-[:LIKES]-&gt;(t:Technology)\n\n\/\/query relationship backwards will not return results \u67e5\u8be2\u5173\u7cfb\nMATCH (p:Person)&lt;-[:LIKES]-(t:Technology)\n\n\/\/better to query with undirected relationship unless sure of direction\nMATCH (p:Person)-[:LIKES]-(t:Technology)<\/code><\/pre>\n<h2>Relationship types<\/h2>\n<p>\u5173\u7cfb\u7c7b\u578b\u53ef\u4ee5\u81ea\u5df1\u5b9a\u4e49, \u63a8\u8350\u4f7f\u7528\u52a8\u8bcd (verbs and actions)<br \/>\n\u4f8b\u5982\u4ee5\u4e0b\u7684\u5173\u7cfb\u7c7b\u578b<\/p>\n<pre><code># \u838e\u8389\u559c\u6b22\u56fe\n[:LIKES] - makes sense when we put nodes on either side of the relationship (Sally LIKES Graphs)  \n\n#  \u838e\u8389\u4e3a neo4j \u5de5\u4f5c\n[:IS_FRIENDS_WITH] - makes sense when we put nodes with it (Sally IS_FRIENDS_WITH John)  \n\n# \u838e\u8389\u4e3a neo4j \u5de5\u4f5c\n[:WORKS_FOR] - makes sense with nodes (Sally WORKS_FOR Neo4j)\n<\/code><\/pre>\n<h2>Relationship variables<\/h2>\n<p>\u4e3a\u67e5\u8be2\u65b9\u4fbf, \u53ef\u4ee5\u7ed9\u5173\u7cfb\u547d\u540d\u4e00\u4e2a\u53d8\u91cf, \u5f62\u5982 <code>[r]<\/code> \u6216 <code>[rel]<\/code><\/p>\n<h2>Node or relationship properties<\/h2>\n<p>\u8282\u70b9\u548c\u5173\u7cfb\u7684\u5c5e\u6027\u90fd\u53ef\u5728\u8282\u70b9\u7684\u62ec\u53f7\u6216\u5173\u7cfb\u7684\u62ec\u53f7\u5185\u4f7f\u7528\u82b1\u62ec\u53f7\u3002\u7136\u540e\uff0c\u5c5e\u6027\u7684\u540d\u79f0\u548c\u503c\u653e\u5728\u82b1\u62ec\u53f7\u5185\u3002<\/p>\n<p>\u4f8b\u5982<\/p>\n<pre><code># \u8282\u70b9\u5c5e\u6027: p \u662f\u8282\u70b9\u540d, Person \u662f\u6807\u7b7e, \u5c5e\u6027\u540d\u662f name, \u5c5e\u6027\u503c\u662f Sally\nNode property: (p:Person {name: &#039;Sally&#039;})\n\n# \u5173\u7cfb\u5c5e\u6027: rel \u662f\u5c5e\u6027\u540d, IS_FRIENDS_WITH \u662f\u6807\u7b7e, \u5c5e\u6027\u540d\u662f since, \u5c5e\u6027\u503c\u662f 2018\nRelationship property: -[rel:IS_FRIENDS_WITH {since: 2018}]-&gt;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.fanyamin.com\/wordpress\/wp-content\/uploads\/2024\/08\/image-1724555587718.png\" alt=\"file\" \/><\/p>\n<h2>Patterns in Cypher<\/h2>\n<p>\u5728 Cypher \u4e2d\u7684\u6a21\u5f0f\u53ef\u80fd\u901a\u8fc7\u4ee5\u4e0a\u7684\u8282\u70b9, \u5173\u7cfb\u548c\u5c5e\u6027\u653e\u5728\u4e00\u8d77\u6765\u8868\u793a, \u4ee5\u9017\u53f7\u5206\u9694.<\/p>\n<p>\u4f8b\u5982\u6211\u4eec\u8981\u67e5\u8be2\u6a21\u5f0f &quot;Sally likes Graph&quot;, \u53ef\u4ee5\u8fd9\u6837\u8868\u793a<\/p>\n<pre><code>(p:Person {name: &quot;Sally&quot;})-[rel:LIKES]-&gt;(g:Technology {type: &quot;Graphs&quot;})<\/code><\/pre>\n<h2>Practice<\/h2>\n<ul>\n<li>\u7528 docker-compose \u542f\u52a8\u5982\u4e0b\u7684 memgraph<\/li>\n<\/ul>\n<pre><code>services:\n  memgraph:\n    image: memgraph\/memgraph-mage:latest\n    container_name: memgraph-mage\n    ports:\n      - &quot;7687:7687&quot;\n      - &quot;7444:7444&quot;\n    command: [&quot;--log-level=TRACE&quot;]\n\n  lab:\n    image: memgraph\/lab:latest\n    container_name: memgraph-lab\n    ports:\n      - &quot;3000:3000&quot;\n    depends_on:\n      - memgraph\n    environment:\n      - QUICK_CONNECT_MG_HOST=memgraph\n      - QUICK_CONNECT_MG_PORT=7687<\/code><\/pre>\n<p>\u901a\u8fc7 <code>docker-compose up -d<\/code> \u542f\u52a8 memgraph mage \u548c lab<\/p>\n<ul>\n<li>\n<p>memgraph\/memgraph-mage - includes Memgraph database, command-line interface mgconsole and MAGE graph algorithms library. If tagged with cuGraph, it also includes NVIDIA cuGraph GPU-powered graph algorithms.<\/p>\n<\/li>\n<li>\n<p>memgraph\/lab - includes a web interface Memgraph Lab that helps you explore the data stored in Memgraph.<\/p>\n<\/li>\n<\/ul>\n<p>\u6253\u5f00  <a href=\"http:\/\/localhost:3000\/\">http:\/\/localhost:3000\/<\/a> \u7528\u5982\u4e0b cypher \u521b\u5efa\u8282\u70b9\u548c\u5173\u7cfb<\/p>\n<pre><code>CREATE (:Country {name: &#039;Germany&#039;, language: &#039;German&#039;, continent: &#039;Europe&#039;});\nCREATE (:Country {name: &#039;France&#039;, language: &#039;French&#039;, continent: &#039;Europe&#039;});\n\nMATCH (c1),(c2) WHERE c1.name= &#039;Germany&#039; AND c2.name = &#039;France&#039;\nCREATE (c2)&lt;-[:WORKING_IN {date_of_start: 2014}]-(p:Person {name: &#039;John&#039;})-\n[:LIVING_IN {date_of_start: 2014}]-&gt;(c1);\n\nMATCH (c1),(c2) WHERE c1.name= &#039;Germany&#039; AND c2.name = &#039;France&#039;\nCREATE (c1)&lt;-[:WORKING_IN {date_of_start: 2014}]-(p:Person {name: &#039;Harry&#039;})-[:LIVING_IN {date_of_start: 2013}]-&gt;(c2);\n\nMATCH (n)-[r]-&gt;(m) RETURN n,r,m;<\/code><\/pre>\n<h2>Reference<\/h2>\n<ul>\n<li><a href=\"https:\/\/neo4j.com\/docs\/getting-started\/cypher-intro\/\">Cypher \u7b80\u4ecb<\/a><\/li>\n<li><a href=\"https:\/\/neo4j.com\/docs\/cypher-manual\/current\/introduction\/\">Neo4j Cypher Manual<\/a><\/li>\n<li><a href=\"https:\/\/neo4j.com\/docs\/cypher-cheat-sheet\/5\/\">Cypher Cheat Sheet<\/a><\/li>\n<li><a href=\"https:\/\/memgraph.com\/docs\/getting-started\/install-memgraph\/docker-compose\">Install memgraph by docker<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Cyper \u4f5c\u4e3a\u58f0\u660e\u5f0f\u67e5\u8be2\u8bed\u8a00, SQL \u5728\u8ba1\u7b97\u673a\u884c\u4e1a\u65e0\u4eba\u4e0d\u6653, \u65e0\u4eba\u4e0d\u77e5. \u800c Cypher \u5c31\u662f Graph Database \u56fe\u6570\u636e\u5e93\u7684 SQL. Cypher is unique because it provides a visual way of matching patterns and relationships. Cypher was inspired by an ASCII-art type of syntax where (nodes)-[:ARE_CONNECTED_TO]-&gt;(otherNodes) using rounded brackets for circular (nodes), and -[:ARROWS]-&gt; for relationships. Cypher \u7528&quot;\u5706\u62ec\u53f7&quot;\u6765\u8868\u793a\u8282\u70b9, \u7528&quot;\u65b9\u62ec\u53f7,\u8fde\u63a5\u7ebf\u53ca\u7bad\u5934&quot;\u8868\u793a\u5173\u7cfb When you write a query, [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=1291\" title=\"Permanent Link to: \u7528 Cypher \u6765\u67e5\u8be2\u56fe\u6570\u636e\u5e93\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1291","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1291"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1291"}],"version-history":[{"count":7,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1291\/revisions"}],"predecessor-version":[{"id":1301,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1291\/revisions\/1301"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}