[12屆 鐵人賽] [Day9] Neo4j 獨創 Cypher Query Language === ###### tags: `iT鐵人賽` `Neo4j` ## 前情提要 上一篇講了 Neo4j DB 的基礎結構 是時候要來講講如何製造這些結構了 SQL Server 有 SQL 語法 那 Neo4j 身為獨步全球的 Graph Database (?) 自然會有屬於 Neo4j 的 GQL 沒錯 ! 就是今天的主題 **Cypher** ## Cypher Cypher,起源於 2011 年 為 Neo4j 的工程師針對 Neo4j Graph Database 開發的查詢語言 並基於 SQL 語法所開發而成 所以如果你熟悉 SQL 語法的話,你之後會看到許多非常類似的東西 那我們先來看看基本結構吧 如果你要簡單表達一個關係 ( A Call B ),那你可以寫成這樣 ``` ( A )-[ Call ]->( B ) ``` ### Node 首先你要表達一個節點 `Node` 那你要寫成這樣 --> `()` 還記得上篇講過的 `Label` 嗎,加上 `Label` 吧 --> `(:Label)` 那 `Property` 呢 --> `(:Label{Property:"I am Property"})` 這樣一個完整的 `Cypher` 表示 `Node` 語法就完成了 :::info 這邊值得一提的是,你會發現 `Property` 的內容是用 `{}` 包起來的 實際上,裡面就是 **Json** 的結構 不過要記得上一篇說的,巢狀結構是不允許的 ::: ### Relationship 那 `Relationship` 呢 --> `[]` 一樣加上 `Type` 跟 `Property` 吧 --> `[:Label{Property:"I am Property"}]` 嗯 ... 這部份 Node 跟 Relationship 長的一樣啦 (゚∀゚) 那加上連接線吧 ``` (:Person{Name:"A"})-[:Call]->(:Person{Name:"B"}) ``` 這樣 A 連到 B 的基本結構就完成了 :::success 這樣是代表有指向的關係 ``` ()-[]->() ``` 當然有沒指向的關係 ``` ()-[]-() ``` 嗯 對 `>` 箭頭拿掉而已 ::: 基本結構講完了,下面這張官方的圖你是不是也看得懂了呢 ? >![](https://dist.neo4j.com/wp-content/uploads/20170731135122/Property-Graph-Cypher.svg) > Source : [Property-Graph-Cypher](https://neo4j.com/cypher-graph-query-language/) ## openCypher 繼續講故事 Cypher 發展自 2015 年,隨著圖形資料的技術漸漸變得重要 Neo4j 決定將 Cypher 標準化,並視下方四個關鍵項目 * Cypher reference documentation > 不負責任翻譯 : 完整 Cypher 使用文件 * Technology compatibility kit (TCK) > 不負責任翻譯 : 軟體技術版本支援 > 大概是說,所有用 Cypher 的軟體必須提供,軟體版本對應 Cypher 版本的內容 * Reference implementation > 不負責任翻譯 : ************ > ~~看得懂英文內容,中文翻譯不能@@~~ > > > Distributed under the Apache 2.0 license, the reference implementation is a fully functional implementation of key parts of the stack needed to support Cypher inside a data platform or tool. The first planned deliverable is a parser that will take a Cypher statement and parse it into an AST (abstract syntax tree) representation. The reference implementation complements the documentation and tests by providing working implementations of Cypher – which are permissively licensed – and can be used as examples or as a foundation for one’s own implementation. > > Source : [Meet openCypher: The SQL for Graphs ](https://neo4j.com/blog/open-cypher-sql-for-graphs/) * Cypher language specification > 不負責任翻譯 : Cypher 語法標準規範 將 Cypher 推廣為圖形資料的技術的主要圖形查詢語法 並開始了 [openCypher](https://www.opencypher.org/) 這個開源項目,持續堆動相關技術的發展 --- 總算是寫完了@@ 一直搞不定這篇文章的內容大綱跟順序 所以這篇文章一個一拖再拖 從鐵人賽 Day3 一路拖到 Day5 才寫完 我的積稿量阿QQ 明天 ***Cypher 語法簡介 Part1 : 基礎中的基礎 MATCH & RETURN*** 期待明日的降臨吧 Ciao! --- ## 參考資料 [Wikipedia - Cypher (query language)](https://en.wikipedia.org/wiki/Cypher_(query_language)) [Neo4j Developer - Cypher Query Language](https://neo4j.com/developer/cypher/) [Neo4j - Cypher-Graph-Query-Language](https://neo4j.com/cypher-graph-query-language/) [openCypher](https://www.opencypher.org/) [Meet openCypher: The SQL for Graphs ](https://neo4j.com/blog/open-cypher-sql-for-graphs/) ## 紀錄 撰寫日期:2020/09/?? & 2020/09/19 耗時:至少 2.5 小