How to work with CSV in AGE with python
Published: 2023-05-07 00:10:38

Python is one of the most popular programming languages, and it is widely used in various industries for data analysis and machine learning. One of the most important tools in the Python ecosystem is Apache Age, a new open-source relational database management system that uses PostgreSQL as a backend and provides a SQL interface to query JSON, YAML, and CSV files.

To use Apache Age in your Python projects, you need to download the Python driver from [here](https://github.com/apache/age/tree/master/drivers/python). This driver provides a Python API to interact with the Age database and perform CRUD (Create, Read, Update, Delete) operations.

Once you have downloaded the driver, you need to install its dependencies by running 
```
pip install -r requirements.txt
``` 
This will install all the required packages, including psycopg2, which is used to connect to the PostgreSQL backend.


After installing the dependencies, you can start using the Age Python drivers csv in your project.


Here's a step-by-step guide on how to use it in your project:

- First, you need to install pandas, which is a popular data manipulation library. You can do this by running the following command in your terminal:

```bash
pip install pandas
```

- Next, you need to download the age_csv file from [this GitHub repository](https://github.com/Munmud/age_csv/blob/main/age_csv.py.) You can save it in your project directory or in a separate utils directory.

- Now that you have pandas and the age_csv library installed, you can use the functions it provides to import data into Age. Here are two examples of how you can use the library:

## Save to CSV
The save_csv() function allows you to save data from a PostgreSQL database to a CSV file. Here's an example:
```py
import psycopg2
from age_csv import *

# set DB path and graph name
conn = psycopg2.connect(
    host="localhost",
    port="5430",
    dbname="postgresDB",
    user="postgresUser",
    password="postgresPW")

GRAPH_NAME = 'bitnine_global_inic'

save_csv(connection=conn,
         GRAPH_NAME=GRAPH_NAME)
```

In the above example, we are connecting to a PostgreSQL database and saving the data from the bitnine_global_inic graph to a CSV file.


## Load from CSV
The load_csv() function allows you to load data from CSV files into Age. Here's an example:
```py
import psycopg2
from age_csv import *
import os

# set DB path and graph name
conn = psycopg2.connect(
    host="localhost",
    port="5430",
    dbname="postgresDB",
    user="postgresUser",
    password="postgresPW")

GRAPH_NAME = 'bitnine_global_inic'

data = load_csv(connection=conn,
                GRAPH_NAME=GRAPH_NAME,
                # directory=os.path.join(os.path.dirname(__file__), 'data')
                directory="./bitnine_global_inic_data"
                )
```

In the above example, we are connecting to a PostgreSQL database and loading data from CSV files located in the bitnine_global_inic_data directory. You must follow the naming rules for generating nodes and edges in your CSV files. For example, if you want to create nodes with the label People, you should name your CSV file People.csv.

- Example:
    - Creating nodes with label People : `People.csv`
    - Creating edges with label Purchase : `edge_Purchase.csv`


In conclusion, age_csv is a helpful library that can simplify the process of importing data from CSV files into Age. By using the save_csv() and load_csv() functions, you can easily import and export data between Age and CSV files. To get started, download the age_csv library and install pandas. Then, use the library as shown in the examples above.