DEV Community

Abdullah Bin Omer Zia
Abdullah Bin Omer Zia

Posted on

Using Apache AGE with Python

Apache Age is a distributed graph database that can handle large-scale graph data sets. It is built on top of Apache Arrow and Apache Spark, which allows it to take advantage of the distributed computing capabilities of Spark and the columnar storage format of Arrow. In this post, we'll explore how to use Apache Age with Python.

Getting Started with Apache Age

Before we can use Apache Age with Python, we need to install it on our system. The easiest way to do this is to use pip, the Python package installer. To install Apache Age using pip, open a terminal or command prompt and type:

pip install apache-age-python
Enter fullscreen mode Exit fullscreen mode

This will download and install the latest version of Apache Age, along with its Python client library.

Connecting to an Apache Age Database

Once Apache Age is installed, we can use its Python client library to connect to an Apache Age database. To connect to a database, we need to create a Graph object and pass in the configuration settings for the database. For example, if we have an Apache Age database running on the localhost, with the default settings, we can connect to it with the following code:

from age.core import Graph

graph = Graph("localhost", 6000, "my_database")
Enter fullscreen mode Exit fullscreen mode

This creates a Graph object that is connected to the Apache Age database running on the localhost, on port 6000, with the database name "my_database". We can now use this graph object to query the database and perform other operations.

Performing Queries with Apache Age and Python

Apache Age supports both the Gremlin and Cypher query languages, which allows us to use a variety of query styles when working with the database. To perform a query with Apache Age and Python, we can use the query_gremlin() or query_cypher() method on the Graph object. For example, to perform a Gremlin query that retrieves all the vertices in the database, we can use the following code:

result = graph.query_gremlin("g.V()")
Enter fullscreen mode Exit fullscreen mode

This sends the Gremlin query "g.V()" to the database and returns the results as a list of dictionaries. Each dictionary represents a vertex in the graph and includes its properties and other metadata.

Similarly, to perform a Cypher query that retrieves all the vertices in the database, we can use the following code:

result = graph.query_cypher("MATCH (n) RETURN n")
Enter fullscreen mode Exit fullscreen mode

This sends the Cypher query "MATCH (n) RETURN n" to the database and returns the results as a list of dictionaries, with each dictionary representing a node in the graph.

Modifying Data with Apache Age and Python

In addition to querying the database, we can also use Apache Age and Python to modify the data in the graph. To add a vertex to the graph, we can use the add_vertex() method on the Graph object. For example, to add a vertex with the label "person" and the property "name" set to "John Doe", we can use the following code:

vertex = graph.add_vertex("person", name="John Doe")
Enter fullscreen mode Exit fullscreen mode

This creates a new vertex in the graph with the label "person" and the property "name" set to "John Doe". We can also add edges between vertices using the add_edge() method.

Conclusion

In this post, we've explored how to use Apache Age with Python. We've seen how to connect to a database, perform queries using both Gremlin and Cypher, and modify the data in the graph. Apache Age is a powerful graph database that can handle large-scale graph data sets, and its Python client library makes it easy to work with the database using Python. With its distributed computing capabilities, Apache Arrow columnar storage format, and support for both Gremlin and Cypher query languages, Apache Age is a versatile tool for managing graph data. If you're working with large-scale graph data sets and looking for a scalable and flexible graph database solution, give Apache Age a try and see how it can work for you.

Top comments (1)

Collapse
 
dixon1e profile image
Dixon Dick • Edited

The line:

import age.core

fails with "No module named age.core", using these pinned requirements:

antlr4-python3-runtime==4.11.1
apache-age-python==0.0.7
psycopg2==2.9.9
psycopg2-binary==2.9.9

The python version is 3.11.

EDIT: Here is the latest driver sample code. Click on any of the ipynb examples to see a Python Notebook.