全部產品
Search
文件中心

:基於GraphCompute快速搭建好友推薦圖應用

更新時間:Jun 30, 2024

為了方便使用者能夠熟悉圖計算服務GraphCompute,我們提供一份完整的Demo資料、查詢Query和文檔指南,協助使用者快速搭建一個圖計算應用,並提供百萬層級資料量的多度好友關係查詢&分析能力。

圖計算是什嗎?

圖計算服務(GraphCompute)基於阿里巴巴自主研發的圖引擎,提供一站式圖計算解決方案,專註OLAP能力的擴充和支援,通過自研的高效能運算元,讓使用者快速接入資料和完成圖技術探索。結合阿里巴巴在電商、安全和社交等多個行業領域的積累,為全球企業及開發人員提供圖技術服務。

【好友推薦】為什麼要用圖計算

社交類情境的資料模型屬於典型的圖結構,圖計算可以為社交類業務提供天然的圖模型支援,更加完美表達各種關係的資料。使用圖計算,可以顯著提升社交類業務程式的開發效率和品質,減少資料關係的轉換和計算的額外損耗。

以社交關係為例,不僅是好友查詢,在“初始使用者推薦、好友推薦、聊天查詢、關聯主題”等多種情境中都能運用圖來建模。圖技術在好友查詢中僅需要幾毫秒的時間,它將好友定義成節點,將好友與好友之間的關係定義成邊,圖計算產品能夠以“點、邊”的查詢方式,更加高效的表達並分析這複雜圖結構下的資料關係和結果。

【好友推薦】商務邏輯怎麼構建

某款社交APP,是一款年輕人交友軟體,通過虛擬頭像,匿名社交,即時匹配、連麥派對、聲控接唱、興趣測試等功能,從相遇開始,拒絕尬聊。該業務提供了“心靈匹配”功能,主要技術訴求是基於圖計算能夠提供三度好友推薦給“心靈匹配”。

根據業務的特點進行抽象定義,最終的商務邏輯可以理解為:

  • 查詢的業務情境:A- B - C -D 三跳好友關係

  • 需要擷取到三跳後,對所有好友進行路徑權重加分,最終得到一個推薦好友的排序列表

image

【好友推薦】圖模型建立

根據圖查詢的業務情境:A- B - C -D 三跳好友關係,通過模型的抽取和總結,最終我們確定的圖模型結構是如下結構:User代表使用者節點,節點資訊中包括使用者屬性(生日、性別)、relation代表使用者之間的關係,代表他們的關係(關係權重-PS:關係權重基於離線計算或者分析可得)

image

Relation 關係表達,就是典型的U2U思路,找到與其興趣相似的活躍使用者,將後者的行為作為前者行為序列的補充,可以對於低活躍使用者相似偏好的推薦。

U2U擴充的方法也有好幾類:

方法

優點

缺點

1.user-based的協同過濾

推薦的發現性好。

難以引入side information,比如關注的人員、興趣點、學校或使用者的基礎資訊,年齡、購買力等;且資料稀疏對該類演算法的影響較大。

2. 基於user embedding,進行相似使用者的Top k檢索

在挖掘user embedding的過程中可以引入較多side information。

相似使用者的Top k檢索魯棒性較差,計算資源消耗大。

3. 引入使用者向量聚簇,變成相似使用者簇的Top k檢索

相似簇推薦相較於相似使用者的推薦,穩定性較高,計算開銷減少。

基於使用者單向量聚簇,容易忽略使用者行為橫跨多類目的特性,得到的商品列表較為單一。

如何建立圖應用

根據圖查詢的業務情境:A- B - C -D 三跳好友關係,通過模型的抽取和總結,最終我們確定的圖模型結構是如下結構:User代表使用者節點,節點資訊中包括使用者屬性(生日、性別)、relation代表使用者之間的關係,代表他們的關係(關係權重-PS:關係權重基於離線計算或者分析可得)

1、建立叢集

單擊【圖計算服務-購買建立】

image

2、定義圖模型、配置節點、關係表

定義圖模型

配置節點

配置關係表

image

樣本使用:

MaxCompute源表:【igraph_mock.vertex_user_demo】

節點表中包括123074個使用者

樣本使用:

MaxCompute源表:【igraph_mock.edge_relation_demo】

關係表中包括399879條關係邊

1)定義圖模型

1、建立【新增 - 圖】定義圖名和描述;

圖名:【user_relation_graph】 ,後續Gremlin查詢語句中需要指定訪問的圖配置名稱。

2、【圖編輯】頁面【新增點】建立User節點表

image.png

3、【圖編輯】頁面【User節點表】直接建立relation關係

image.png

image

2)配置節點屬性

image

3)配置關係屬性

image

警告

注意:如果後續需要基於關係資料執行圖演算法相關任務,請關閉【索引最佳化】的配置。

image.png

4)發布圖配置

image.png

3、發布索引

1)批量迴流

圖計算服務底層服務是通過分布式圖計算引擎來提供服務,在每次圖配置更新正式發布後,都需要進行一次資料迴流操作。我們提供了一鍵化的【批量迴流】功能,進行線上資料結構的快速變更。

image.png

2)特定表迴流

image.png

4、圖查詢

通過前面的步驟,圖計算應用及資料已經準備完成,下面就可以進行圖資料的查詢和分析;

可以通過【圖探索】-進行探索式互動或者控制台Gremlin語句查詢;

image

image

【好友推薦】測試Case

1、根據Userid查詢使用者詳情:

g("user_relation_graph").V("7949635553727122101").hasLabel("users")

查詢結果:

{
  "result": [
    {
      "data": [
        {
          "value": [
            {
              "label": "user",
              "gender": "1",
              "id": "7949635553727122101",
              "starsign": "Aries"
            }
          ],
          "labels": [
            []
          ]
        }
      ],
      "error_info": [],
      "trace_info": {}
    }
  ],
  "error_info": []
}

2、批量查詢一批Userid的使用者詳情

g("user_relation_graph").V("2443269531561029504;4315033251719520021;6045530619721418713;-2441936916298108531;-6187501937134616998;-7902352812594818920;8829494226614398819;-788398966410862160").hasLabel("user")

查詢結果:

{
  "data": [
    {
      "label": "user",
      "gender": "1",
      "id": "-2441936916298108531",
      "starsign": "Cancer"
    },
    {
      "label": "user",
      "gender": "1",
      "id": "-6187501937134616998",
      "starsign": "Gemini"
    },
    {
      "label": "user",
      "gender": "0",
      "id": "-788398966410862160",
      "starsign": "Capricorn"
    },
    {
      "label": "user",
      "gender": "0",
      "id": "-7902352812594818920",
      "starsign": "Aquarius"
    },
    {
      "label": "user",
      "gender": "0",
      "id": "2443269531561029504",
      "starsign": "Pisces"
    },
    {
      "label": "user",
      "gender": "1",
      "id": "4315033251719520021",
      "starsign": "Pisces"
    },
    {
      "label": "user",
      "gender": "1",
      "id": "6045530619721418713",
      "starsign": "Pisces"
    },
    {
      "label": "user",
      "gender": "1",
      "id": "8829494226614398819",
      "starsign": "Aquarius"
    }
  ],
  "error_info": [],
  "trace_info": {}
}

3、查詢使用者的一度關係並且排序計算

g("user_relation_graph").E("7949635553727122101").hasLabel("relation").order().by("score",decr).limit(10).values("to_id")

查詢結果:

{
  "result": [
    {
      "data": [
        "2557390182698651469",
        "-5910095803510830870",
        "-8777626058260080543",
        "-3326503472333052856",
        "-5628868613588358018",
        "5693972407819734988",
        "3169032466213709540",
        "-6273932137952248996",
        "85024782667881542",
        "2490097926641478897"
      ],
      "error_info": [],
      "trace_info": {}
    }
  ],
  "error_info": []
}

4、查詢使用者的三度好友中星座為白羊座的好友

g("user_relation_graph").E("-2441936916298108531").hasLabel("relation").outE().outE().inV().filter("starsign=\"Aries\"")

查詢結果:

image

5、查詢使用者7949635553727122101好友的星座人數分布

g("user_relation_graph").E("7949635553727122101").hasLabel("relation").inV().groupCount().by("starsign")

查詢結果:

{
  "result": [
    {
      "data": [
        {
          "\"Aquarius\"": "7",
          "\"Aries\"": "10",
          "\"Cancer\"": "1",
          "\"Capricorn\"": "3",
          "\"Gemini\"": "3",
          "\"Leo\"": "2",
          "\"Libra\"": "2",
          "\"Pisces\"": "9",
          "\"Sagittarius\"": "7",
          "\"Taurus\"": "4",
          "\"Virgo\"": "2"
        }
      ],
      "error_info": [],
      "trace_info": {}
    }
  ],
  "error_info": []
}

6、查詢使用者7949635553727122101好友的星座分數分布

g("user_relation_graph").withSack(supplier(normal,"0.0"),Splitter.identity,Operator.sum).E("7949635553727122101").hasLabel("relation").sack(Operator.sum).by("score").inV().group().by("starsign").by(sack().sum())

查詢結果:

{
  "result": [
    {
      "data": [
        {
          "\"Aquarius\"": "99.44964174192042",
          "\"Aries\"": "171.6614835163086",
          "\"Cancer\"": "15.8359302136816",
          "\"Capricorn\"": "42.44029297266677",
          "\"Gemini\"": "34.2056092615523",
          "\"Leo\"": "16.46936742222886",
          "\"Libra\"": "24.07061392479605",
          "\"Pisces\"": "129.85462775218915",
          "\"Sagittarius\"": "86.14746036242798",
          "\"Scorpio\"": "6.33437208547264",
          "\"Taurus\"": "41.80685576411946",
          "\"Virgo\"": "33.572172053005"
        }
      ],
      "error_info": [],
      "trace_info": {}
    }
  ],
  "error_info": []
}

7、查詢使用者-2441936916298108531三度好友中星座為白羊座的好友,計算路徑權重總分,按得分降序排列,並取前10名

g("user_relation_graph").withSack(supplier(normal,"0.0"),Splitter.identity,Operator.sum).E("-2441936916298108531").hasLabel("relation").sack(Operator.sum).by("score").outE().sack(Operator.sum).by("score").outE().sack(Operator.sum).by("score").inV().filter("starsign=\"Aries\"").values("id").barrier().dedup().order().by(sack(),decr).limit(10)

查詢結果:

{
  "result": [
    {
      "data": [
        "-4985651325249407669",
        "-966745601209007179",
        "300594519935616602",
        "8616477414455953382",
        "3410211067444088094",
        "7361520262922301828",
        "4419627442674893942",
        "1684980613157243612",
        "-3968869064747091877",
        "7376060565223003509"
      ],
      "error_info": [],
      "trace_info": {}
    }
  ],
  "error_info": []
}

高階能力 - 高效能全圖統計能力

業務功能:需要對使用者進行全域的統計和計算,圖計算服務在圖查詢能力的基礎之上,整合了倒排查詢。

1、使用者表從【KV索引】調整為【INDEX】倒排索引,提交表配置儲存;

image.png

2、執行圖配置的【儲存】和【發布】功能;

3、圖營運 - 索引構建

image.png

測試case:

1、查詢所有白羊座的使用者

g("user_relation_graph").V().hasLabel("user").indexQuery("{\"match\":{\"starsign\":\"Aries\"},\"config\":{\"seek_count_limit_per_shard\":100000,\"search_count_limit_per_shard\":100000}}")

查詢結果:

image

2、查詢所有男性使用者的星座分布(為了保證效能,進行了limit 統計截斷)

g("user_relation_graph").V().hasLabel("user").indexQuery("{\"match\":{\"gender\":\"1\"},\"config\":{\"seek_count_limit_per_shard\":100000,\"search_count_limit_per_shard\":100000}}").groupCount().by("starsign")

查詢結果:

{
    "result": [
        {
            "data": [
                {
                    "\"Aquarius\"": 825,
                    "\"Aries\"": 640,
                    "\"Cancer\"": 923,
                    "\"Capricorn\"": 848,
                    "\"Gemini\"": 773,
                    "\"Leo\"": 821,
                    "\"Libra\"": 522,
                    "\"Pisces\"": 2397,
                    "\"Sagittarius\"": 491,
                    "\"Scorpio\"": 501,
                    "\"Taurus\"": 739,
                    "\"Virgo\"": 520
                }
            ],
            "error_info": [               
            ],
            "trace_info": {              
            }
        }
    ],
    "error_info": [        
    ]

3、查詢好友最多的100名白羊座男性使用者

g("user_relation_graph").V().hasLabel("user").indexQuery("{\"and\":[{\"match\":{\"gender\":\"1\"}\},\{\"match\":{\"starsign\":\"Aries\"}}],\"config\":{\"seek_count_limit_per_shard\":100000,\"search_count_limit_per_shard\":100000}}").limit(10000).outE().groupCount().by("from_id").unfold().order().by(select(Column.values),decr).limit(100)

查詢結果:

{
  "result": [
    {
      "data": [
        {
          "-7032647615083234229": "50"
        },
        {
          "-5722782251601168066": "50"
        },
        {
          "-5335242748220558153": "50"
        },
        {
          "-8676408452255309391": "50"
        },
        {
          "-6047928004364318541": "50"
        },
        {
          "-5344466669668822162": "50"
        },
        {
          "-6190401221243138849": "50"
        },
        {
          "-4827852736929428415": "50"
        },
        {
          "-5307302829373746633": "50"
        },
        {
          "-5475401520922089187": "50"
        },
        {
          "-8017336865667734219": "50"
        },
        {
          "-8152630370117271740": "50"
        },
        {
          "-7004109459328652310": "50"
        },
        {
          "-7817593255334792111": "50"
        },
        {
          "-5272574182922494022": "50"
        },
        {
          "-8523896507246731100": "50"
        },
        {
          "-5938745235467206212": "50"
        },
        {
          "-8438867826624678384": "50"
        },
        {
          "-4405449005585220943": "50"
        },
        {
          "-9126301390881979643": "50"
        },
        {
          "-5382489933968271711": "50"
        },
        {
          "-8841373379965641987": "50"
        },
        {
          "-5302202017699941647": "50"
        },
        {
          "-6248244606214120159": "50"
        },
        {
          "-6711633791333564554": "50"
        },
        {
          "-7315782827574470472": "50"
        },
        {
          "-9031277985752489187": "50"
        },
        {
          "-9022869359443119815": "50"
        },
        {
          "-8369470302541518920": "50"
        },
        {
          "-6142766699127849771": "50"
        },
        {
          "-8655738446138261193": "50"
        },
        {
          "-8646149194767113790": "50"
        },
        {
          "-9097183937680782346": "50"
        },
        {
          "-4641376756135904334": "50"
        },
        {
          "-9081172527938898024": "50"
        },
        {
          "-7137344541799001227": "50"
        },
        {
          "-8491934123275310192": "50"
        },
        {
          "-5656245743850590165": "50"
        },
        {
          "-8448876639884702547": "50"
        },
        {
          "-8921032424157220292": "50"
        },
        {
          "-8886790874757451152": "50"
        },
        {
          "-4170067740591839020": "50"
        },
        {
          "-8412401447690411889": "50"
        },
        {
          "-6935600597565680532": "50"
        },
        {
          "-6465292696804515998": "50"
        },
        {
          "-4580178566813967734": "50"
        },
        {
          "-8337980039891383510": "50"
        },
        {
          "-6750094915823112945": "50"
        },
        {
          "-8271130662107074869": "50"
        },
        {
          "-4313989045148439251": "50"
        },
        {
          "-7072971882028192758": "50"
        },
        {
          "-6081673847669716322": "50"
        },
        {
          "-8215720337033905961": "50"
        },
        {
          "-4511660190549716707": "50"
        },
        {
          "-6954636645089290492": "50"
        },
        {
          "-5470987491723790775": "50"
        },
        {
          "-8693909718856724970": "50"
        },
        {
          "-9172299426900892564": "50"
        },
        {
          "-6142070315717702174": "50"
        },
        {
          "-8683222889153361552": "50"
        },
        {
          "-6061245317590715601": "50"
        },
        {
          "-8680938277196498242": "50"
        },
        {
          "-6382251776295701372": "50"
        },
        {
          "-7953204420754362673": "50"
        },
        {
          "-5390210842719212579": "50"
        },
        {
          "-7836046658486786908": "50"
        },
        {
          "-7151222483126499509": "50"
        },
        {
          "-7824880497332818548": "50"
        },
        {
          "-5914268273571897378": "50"
        },
        {
          "-6703760236487855527": "50"
        },
        {
          "-6251439297928817838": "50"
        },
        {
          "-7764254764054046817": "50"
        },
        {
          "-4147078275545366756": "50"
        },
        {
          "-8961736465346823903": "50"
        },
        {
          "-6966728454894457236": "50"
        },
        {
          "-7670177853018881196": "50"
        },
        {
          "-5753132289681546447": "50"
        },
        {
          "-7607424647216651656": "50"
        },
        {
          "-6860348792160569972": "50"
        },
        {
          "-7081692058360204084": "50"
        },
        {
          "-4284841470230159060": "50"
        },
        {
          "-7050130964435971895": "50"
        },
        {
          "-5460635829467019773": "50"
        },
        {
          "-7486065564917180528": "50"
        },
        {
          "-4775647028251006118": "50"
        },
        {
          "-7438712711008287507": "50"
        },
        {
          "-6655259811991911369": "50"
        },
        {
          "-8414061350601269280": "50"
        },
        {
          "-4138667890938143127": "50"
        },
        {
          "-7428739044579099070": "50"
        },
        {
          "-5783820100141303990": "50"
        },
        {
          "-7104520570549970103": "50"
        },
        {
          "-5351128558805225544": "50"
        },
        {
          "-3919387380195528267": "50"
        },
        {
          "-3924714660733547258": "50"
        },
        {
          "-4056394674602078332": "50"
        },
        {
          "-4062831157333201368": "50"
        },
        {
          "-4812848481660416581": "50"
        },
        {
          "-6513421419955124633": "50"
        },
        {
          "-7028635231904837853": "50"
        }
      ],
      "error_info": [],
      "trace_info": {}
    }
  ],
  "error_info": []
}