postgresqlcypherapache-age

Is it possible to create a graph in AGE using existing table in the database?


I have just started with Apache AGE extension. I am exploring the functionalities of graph database. Is there a way to create a graph from existing tables/schema such that the table becomes the label and the attributes become the properties for the vertex?

The create_graph('graph name') is used for creating graphs but I can only create a new graph using this function.


Solution

  • It's not as simple as that. For a start you have to understand this.

    When deriving a graph model from a relational model, keep in mind some general guidelines.

    1. A row is a node.

    2. A table name is a label name.

    3. A join or foreign key is a relationship.

    Using those relationships, you can model out the data. This is if you need to ensure no errors.

    Without an example here is the Dynamic way of creating Graph from Relational model.

    1st make a PostgreSQL function that takes in the arguments. Example, name and title of Person. It will create a node.

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

    2nd use the function like so,

    SELECT public.create_person(sql_person.name, sql_person.title) 
    FROM sql_schema.Person AS sql_person;
    

    You'll have created a node for every row in SQL_SCHEMA.Person