ModernWeb
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Graph Database應用思考模式 - 范建銘 {%hackmd j86FNbMoRoeN4Xn0pg5ofw %} > 請從這裡開始 這場是 backend 的主題 ## 什麼是GraphDB * 不是放圖片的資料庫! * 跟 GraphQL 沒有關係 ## NoSQL _GraphDB 是 NoSQL的分支_ * Key-Value * redis * Document-based * MongoDB * Column-based * BigTable * hbase * Graph-based * [Neo4j](https://neo4j.com/) [圖資料庫](https://zh.wikipedia.org/zh-tw/%E5%9B%BE%E6%95%B0%E6%8D%AE%E5%BA%93) > 圖資料庫也可稱為面向/基於圖的資料庫,對應的英文是Graph database。圖資料庫的基本含義是以「圖」這種資料結構存儲和查詢數據,不是存儲圖片的資料庫。圖資料庫的基本存儲單元為:節點、關係、屬性。 1. 節點 2. 關係 * 關係內可以定義 "屬性" 3. 屬性 Facebook 應用 Graph Database 用最多 資料的關聯越複雜,越適合用 GraphDB 來存取資料。 1. 節點 * 人、粉絲團、地標 2. 關係 * 在節點與節點之間建立關係 * 加好友、同意、拒絕 3. 屬性 * 可針對`節點`做屬性關聯 ![](https://i.imgur.com/AL8638J.jpg) ![](https://www.flickr.com/photos/editor/4537962343) ## 為何想用GraphDB 1. 資料超多 * 上億筆資料量且要隨時更新 * 要可快速更新資料 2. 找資料速度要快 * 網頁呈現不能慢 3. 資料超級複雜 * 舊架構不能一口氣全翻掉 * NoSQL - Graph 4. 最好不要花錢買軟體 * _技術債_ 新舊並存 > 其他人說為啥不用ES > [講者] 你會嗎? > 大家都不會就聽我的 ![](https://cambridge-intelligence.com/wp-content/uploads/2017/03/visualizing-arangodb-graph-database-landscape.png)(https://cambridge-intelligence.com/visualize-arangodb/) * 第一名的 GraphDB - [Neo4j](https://neo4j.com/) ([有書](https://www.tenlong.com.tw/search?keyword=neo4j&lang=zh_tw)) * 第二名的 GraphDB - [ArangoDB](https://www.arangodb.com) ## ArangoDB 1. 多模型資料庫: Graph、Document、key-value 2. 內含API: [JS framework Foxx](https://www.foxx-framework.com/) 3. AQL(ArangoDB Query Language) 查詢語法 * 每學一套 GraphDB 就要學一套新的查詢語法 4. 社群版 (免費) 即可用 cluster * 電商講求高可用性 HA * neo4j 的 cluster 要收費 雖然文件較多且較完整 中文文件也較多 5. 官方文件完整(英文) 6. 社群討論活躍 * 選用 open source 專案時,網路熱絡度很重要,儘量選擇還有在持續維護的軟體 ArangoDB 免費版夠用了 --- ## 設計一套Graph系統吧! ### 電商角度 客戶資料表 --> 領用資料表 <--> 折價券資料表 * 只給某人的獨一折價券 * 共同持有一張折價券 * 100張大家搶 ### 客人角度 * 客人有幾張 * 客人看折價卷內容價值 * 客人參加活動搶到一張限量折價券 ### 關連資料庫 JOIN 模式 客戶資料表 --> 領用資料表 --> 折價券資料表 * 查找 A 客戶的折價卷張數:客戶資料表+領用資料表 * 查找 A 客戶折價卷:客戶資料表 + 領用資料表 + 折價卷資料表 * 預先發 100 張:折價卷 + 塞 100 筆資料到領用資料表 > 從關聯式資料庫到Graph的思考:名詞轉換成節點,動詞轉換關係。 ![](https://i.imgur.com/XTjcK3x.jpg) ![](https://i.imgur.com/WIdpzFB.jpg) ### 圖形資料庫模式 客戶 ----------> 折價券    領用與使用 * 發行折價卷:塞折價卷及領用(領用時再牽線) * 查找 A 客戶的折價卷張數:客戶有幾條領用線 * 查找 A 客戶的折價卷內容 ### Arango DB Demo * Document * 客戶 * 折價券 * Edge > mongoDB從關聯式資料庫轉換需要作反正規化 > 關聯式資料庫轉成圖形式的門檻比較沒這麼大 ### 定義 Graph 關係的方向性 From A node TO B node. AQL ``` For v.e.p IN 1..1 OUTBOUND concat('A_cust/A123456789') GRAPH 'Coupon' ``` v: vertex e: edge p: path 1..1 min..max ``` FILTER p.vertices[1].START <= DATE_NOW() AND p.vertices[1].END >= DATE_NOW() NAD p.edges[0].GET == '1' COLLECT WITH COUNT INTO COUPON_COUNT RETURN COUPON_COUNT ``` 過濾條件 ### 方法優化 * Graph 要從 vertex出發,但資料不一定要放在 vertex * 將客戶編號放在 edge * 節點不需要放關聯key 少一層過濾 * 過濾條件要放在edge上 * 多模型資料庫,一開始以為 document json就是 graph * 一開始不知道可以用 edge,以為建立 document 就是 graph db * document 真的要從頭閱讀到最後,才能學到精髓 * 之後用了 edge 建立拓墣圖後, graph db 速度更快 * DB 引擎: * RocksDB * 從記憶體做存取,速度較快 * MMFiles * 從硬碟做存取 --- ## 維運的技巧與困難 * 高流量 2ms/次 * cluster * 優點 * server 可分角色 * 有的負責API * 分片 * 缺點 * 同步資料耗資源 * 不明原因自行重啟 * Master slave * 缺點 * 不明原因自行重啟 * 最後架構 * 三台single獨立 利用loadbalance去分散流量 * 資料同步寫入三台機器 ## big data 分析 * 銀行詐騙偵測 * 多帳戶共用身份 * 帳戶之間的往來關係 * 帳號作為節點 * 推薦引擎 * 共同喜好牽成關係圖 * 網站應用 * 網頁地圖 * 電商館架 * 社群網站 除了關聯式資料庫還有其他選擇 [Amazon Neptune](https://aws.amazon.com/tw/neptune/) ###### tags: `MW18`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully