Load a graph

更新时间:
复制 MD 格式

Use load_labels_from_table and load_edges_from_table to import vertices and edges from existing relational tables into a graph.

Prerequisites

Before you begin, make sure you have:

  • A privileged account with permission to create extensions

  • The ganos_graph extension installed. ganos_graph depends on AGE. Create the AGE extension before installing ganos_graph.

    CREATE EXTENSION IF NOT EXISTS ganos_graph;

    If you see ERROR: invalid extension name: "ganos_graph", contact us.

  • A graph and the required labels created

  • Source relational tables prepared with vertex and edge data

Important

Load each set of vertices only once. Loading the same vertices multiple times causes id conflicts.

Functions

Run load_labels_from_table first to load vertices, then run load_edges_from_table to load edges. Edge loading depends on vertex IDs already being present in the graph.

load_labels_from_table

Loads vertices from a relational table into the specified graph and label.

load_labels_from_table('<graph_name>',
                       '<label_name>',
                       '<table_name>',
                       '<node_id>' default 'id')

Parameters

ParameterDescription
graph_nameName of the target graph
label_nameVertex label to assign to all loaded vertices
table_nameSource relational table
node_idColumn used as the unique vertex identifier. Defaults to id.

All columns except node_id are imported as vertex properties. The node_id column is not stored as a property — it is used only to establish vertex identity.

load_edges_from_table

Loads edges from a relational table, connecting existing vertices by their IDs.

load_edges_from_table('<graph_name>',
                      '<label_name>',
                      '<table_name>',
                      '<start_node_label_name>',
                      '<end_node_label_name>',
                      '<start_node_id>' default 'from_id',
                      '<end_node_id>' default 'to_id')

Parameters

ParameterDescription
graph_nameName of the target graph
label_nameEdge label to assign to all loaded edges
table_nameSource relational table
start_node_label_nameVertex label of the edge's start node
end_node_label_nameVertex label of the edge's end node
start_node_idColumn containing the start vertex ID. Defaults to from_id. Must match node_id values in the corresponding vertex table.
end_node_idColumn containing the end vertex ID. Defaults to to_id. Must match node_id values in the corresponding vertex table.

All columns except start_node_id and end_node_id are imported as edge properties.

Note

Numeric SQL column types (such as integer and double precision) are preserved as numeric graph property values, as shown in the example output below.

Example

The following example loads a graph named toys that models users owning products.

Step 1: Prepare source tables

Create the vertex and edge tables and insert test data.

-- Vertex table: users
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');

-- Vertex table: products
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');

-- Edge table: ownership relationships
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');

Step 2: Create the graph and labels

SELECT create_graph('toys');
SELECT create_vlabel('toys','v_user');
SELECT create_vlabel('toys','v_product');
SELECT create_elabel('toys','e_own');

Step 3: Load vertices

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

Verify that all 6 vertices loaded correctly:

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

Expected output (6 rows):

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)

Step 4: Load edges

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

Verify that all 4 edges loaded correctly:

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

Expected output (4 rows):

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)