DEV Community

Moiz Ibrar
Moiz Ibrar

Posted on • Updated on

Initialization and creating a database for testing the installation of Postgres and Age part 4.

Querying and testing Postgres with cypher Queries with Apache age:-

  1. To create a graph, use the create_graph function located in the ag_catalog namespace.
SELECT create_graph('graph_name');

Enter fullscreen mode Exit fullscreen mode

This will create a new graph with the name my_graph in the database, which can be used to store and query graph data.

2.
To create a single vertex, use the CREATE clause.

SELECT * 
FROM cypher('graph_name', $$
    CREATE (n)
$$) as (v agtype);
Enter fullscreen mode Exit fullscreen mode

This will execute the Cypher query CREATE (n) on the my_graph graph, which will create a new node in the graph. The query result will be returned in the agtype format in the v column.

3.
To create a single vertex with the label, use the CREATE clause.

SELECT * 
FROM cypher('graph_name', $$
    CREATE (:label)
$$) as (v agtype);

Enter fullscreen mode Exit fullscreen mode

This will execute the Cypher query CREATE (:label) on the my_graph graph, which will create a new node in the graph with the label "label". The query result will be returned in the agtype format in the v column.

4.
To query the graph, you can use the MATCH clause.

SELECT * FROM cypher('graph_name', $$
MATCH (v)
RETURN v
$$) as (v agtype);

Enter fullscreen mode Exit fullscreen mode

This will execute the Cypher query MATCH (v) RETURN v on the my_graph graph, which will retrieve all nodes in the graph and return them as results. The query result will be returned in the agtype format in the v column.

5.
You can use the following to create an edge, for example, between two nodes.

SELECT * 
FROM cypher('graph_name', $$
    MATCH (a:label), (b:label)
    WHERE a.property = 'Node A' AND b.property = 'Node B'
    CREATE (a)-[e:RELTYPE]->(b)
    RETURN e
$$) as (e agtype);
Enter fullscreen mode Exit fullscreen mode

This will execute the Cypher query MATCH (a:label), (b:label) WHERE a.property = 'Node A' AND b.property = 'Node B' CREATE (a)-[e:RELTYPE]->(b) RETURN e on the my_graph graph, which will create a new directed edge of type "RELTYPE" between the nodes with the "label" label and properties "Node A" and "Node B" respectively. The query result will be returned in the agtype format in the e column.

6.
To create an edge and set properties.

SELECT * 
FROM cypher('graph_name', $$
    MATCH (a:label), (b:label)
    WHERE a.property = 'Node A' AND b.property = 'Node B'
    CREATE (a)-[e:RELTYPE {property:a.property + '<->' + b.property}]->(b)
    RETURN e
$$) as (e agtype);
Enter fullscreen mode Exit fullscreen mode

This will execute the Cypher query MATCH (a:label), (b:label) WHERE a.property = 'Node A' AND b.property = 'Node B' CREATE (a)-[e:RELTYPE {property:a.property + '<->' + b.property}]->(b) RETURN e on the my_graph graph, which will create a new directed edge of type "RELTYPE" between the nodes with the "label" label and properties "Node A" and "Node B" respectively. The edge will also have a property named "property", whose value will be a concatenation of the "a.property" and "b.property" values, separated by the "<->" string. The query result will be returned in the agtype format in the e column.

Example:-

SELECT * 
FROM cypher('graph_name', $$
    MATCH (a:Person), (b:Person)
    WHERE a.name = 'Node A' AND b.name = 'Node B'
    CREATE (a)-[e:RELTYPE {name:a.name + '<->' + b.name}]->(b)
    RETURN e
$$) as (e agtype);
Enter fullscreen mode Exit fullscreen mode

Apache-Age:-https://age.apache.org/
GitHub:-https://github.com/apache/age

Top comments (0)