0

I have data in structured from Tables (Rows and Columns). The tables have relations as well. I want to use that structured Data that is stored in PostgreSQL to convert it to Graph Data (Edges and Vertices). Is there any way to convert that data directly to vertices and edges?

I was searching for a way to convert that data directly to vertices and edges.

  • [This](https://github.com/apache/age/issues/50#issuecomment-819722171) answer by Josh Innis gives one possible way to import relational data to AGE – Ahmar Feb 19 '23 at 20:43

3 Answers3

0

To do this, first you'll have to create a graph model using apache-age extension in postgreSQL. Detailed conversion steps is well documented in this documentation provided by neo4j.

Sarthak
  • 380
  • 7
0

Consider the example as follow :

SQL command for creating table and INSERT into table

create table person (
    person_uid SERIAL NOT NULL PRIMARY KEY,
    person_name VARCHAR(50) NOT NULL
);


-- INSERT INTO person
insert into person ( person_name ) values ('Fernanda');
insert into person ( person_name ) values ('Moontasir');
insert into person ( person_name ) values ('Sanjida');
insert into person ( person_name ) values ('Fahmida');

Load AGE

load 'age';
SET search_path TO ag_catalog;

Prepare statement

See the doc for more info about prepare statement

PREPARE create_person AS
SELECT *
FROM cypher('graph_name', $$
CREATE (:Person {name: $name})
$$, $1) as (n agtype);

Dynamic SQL in a pl/pgSQL Function

You could make a pl/pgsql function that takes in your arguments of name and other property and creates a node, such as

CREATE OR REPLACE FUNCTION public.create_person(name text)
RETURNS void
LANGUAGE plpgsql
VOLATILE
AS $BODY$
BEGIN
    EXECUTE format('SELECT * FROM cypher(''graph_name'', $$CREATE (:Person {name: %s})$$) AS (a agtype);', quote_ident(name));
END
$BODY$;

Now you can call the function

SELECT public.create_person(sql_person.person_name) 
FROM person AS sql_person;

This would create a vertex for every row in person.

Alternatively

As the graph relation I want can vary in different situation you can use the python or any other driver to implement your own logical graph database.

0

First you have to create graph model, You can use the cypher command to import data from your relational database into the graph. You can then use the MATCH clause to create vertices and edges bases on your table data.

Here is the Example:

CREATE (:Person {person_id: row.person_id, name: row.name, age: row.age})

You can create edges between vertices based on relationships between rows in different tables. Here is the Example:

MATCH (p1.Person {person_id: row.person_id_1}), (p2:Person {person_id: row.person_id_2})
CREATE (p1)-[:FRIENDS_WITH]->(p2)