全部產品
Search
文件中心

PolarDB:載入圖

更新時間:Jun 04, 2025

您可以通過從關係表中載入頂點和邊實現載入圖。

前提條件

  • 使用高許可權帳號建立ganos_graph外掛程式。

    說明
    • 該外掛程式依賴AGE相關功能,請先安裝並啟用AGE外掛程式後安裝當前外掛程式。

    • 如在安裝外掛程式時遇到ERROR: invalid extension name: "ganos_graph"類似錯誤,請聯絡我們

    CREATE EXTENSION IF NOT EXISTS ganos_graph;
  • 在載入資料之前需提前建立圖和標籤。

  • 頂點資料只能載入一次,多次載入可能出現id衝突。

載入圖

從對應關係表中載入圖包括載入頂點和載入邊兩個步驟,對此為您提供以下兩個函數。

load_labels_from_table

用於從頂點關係表載入頂點。

文法

load_labels_from_table('<圖名稱>',
                      '<標籤名稱>',
                      '<表名稱>',
                      '<節點唯一id欄位名稱>' default 'id')

其中,頂點關係表中除唯一id欄位外,其他欄位均載入為頂點屬性。

load_edges_from_table

用於從邊關係表載入邊。

文法

load_edges_from_table('<圖名稱>',
                    '<標籤名稱>',
                    '<表名稱>',
                    '<起始節點標籤名稱>',
                    '<終止節點標籤名稱>',
                    '<起始id欄位名稱>' default 'from_id',
                    '<終止id欄位名稱>' default 'to_id');

其中,請確保邊關係表中包含起始id終止id說明邊的串連關係。且邊關係表中的起始id終止id與頂點關係表中的<節點唯一id欄位名稱>保持一致。邊關係表中除起始id欄位名稱終止id欄位名稱外,其他欄位均載入為頂點屬性。

樣本

以下為您提供一個簡單的SQL樣本,用於從關係表中載入一張簡單的圖。

  1. 基礎資料準備。

    1. 建立對應關係表並插入測試資料。

      CREATE TABLE public.v_user_raw(id integer, type text, uid text, name text, age integer);
      INSERT INTO v_user_raw VALUES
      (1, 'A','U1', 'Alice', '33'),
      (2, 'B','U1', 'Bob', '21');
      
      CREATE TABLE public.v_product_raw(id integer, product_id text, price double precision);
      INSERT INTO v_product_raw VALUES
      (1, 'INAKLIDAS', '50'),
      (2, 'ENKUCLKSD', '80'),
      (3, 'IIUIHAKLS', '320'),
      (4, 'SDVDSUHEE', '340');
      
      CREATE TABLE public.e_own_raw(from_id integer, to_id integer, product_id text, buy_price text);
      INSERT INTO e_own_raw VALUES
      (1, 1, 'INAKLIDAS', '45'),
      (2, 1, 'ENKUCLKSD', '70'),
      (2, 3, 'INAKLIDAS', '50'),
      (1, 4, 'SDVDSUHEE', '330');
    2. 建立圖和標籤。

      SELECT create_graph('toys');
      SELECT create_vlabel('toys','v_user');
      SELECT create_vlabel('toys','v_product');
      SELECT create_elabel('toys','e_own');
  2. 從關係表中載入圖。

    1. 載入頂點。

      SELECT load_labels_from_table('toys', 'v_user', 'v_user_raw', 'id');
      SELECT load_labels_from_table('toys', 'v_product', 'v_product_raw', 'id');

      可通過以下語句查看圖中所有頂點:

      SELECT * FROM cypher('toys', $$
        MATCH (n)
        RETURN n
      $$) AS (n agtype);

      返回結果如下:

                                                                   n                                                              
      ----------------------------------------------------------------------------------------------------------------------------
       {"id": 844424930131969, "label": ["v_user"], "properties": {"age": 33, "uid": "U1", "name": "Alice", "type": "A"}}::vertex
       {"id": 844424930131970, "label": ["v_user"], "properties": {"age": 21, "uid": "U1", "name": "Bob", "type": "B"}}::vertex
       {"id": 1125899906842625, "label": ["v_product"], "properties": {"price": 50, "product_id": "INAKLIDAS"}}::vertex
       {"id": 1125899906842626, "label": ["v_product"], "properties": {"price": 80, "product_id": "ENKUCLKSD"}}::vertex
       {"id": 1125899906842627, "label": ["v_product"], "properties": {"price": 320, "product_id": "IIUIHAKLS"}}::vertex
       {"id": 1125899906842628, "label": ["v_product"], "properties": {"price": 340, "product_id": "SDVDSUHEE"}}::vertex
      (6 rows)
    2. 載入邊。

      SELECT load_edges_from_table('toys', 'e_own', 'e_own_raw', 'v_user', 'v_product');

      可通過以下語句查看圖中所有邊:

      SELECT * FROM cypher('toys', $$
        MATCH ()-[e:e_own]->()
        RETURN e
      $$) AS (e agtype);

      返回結果如下:

                                                                                          e                                                                                     
      --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       {"id": 1407374883553281, "label": "e_own", "end_id": 1125899906842625, "start_id": 844424930131969, "properties": {"buy_price": "45", "product_id": "INAKLIDAS"}}::edge
       {"id": 1407374883553282, "label": "e_own", "end_id": 1125899906842625, "start_id": 844424930131970, "properties": {"buy_price": "70", "product_id": "ENKUCLKSD"}}::edge
       {"id": 1407374883553283, "label": "e_own", "end_id": 1125899906842627, "start_id": 844424930131970, "properties": {"buy_price": "50", "product_id": "INAKLIDAS"}}::edge
       {"id": 1407374883553284, "label": "e_own", "end_id": 1125899906842628, "start_id": 844424930131969, "properties": {"buy_price": "330", "product_id": "SDVDSUHEE"}}::edge
      (4 rows)