0

When loading edges or vertex from a CSV file in Apache-age We have to create Elabels and Vlabels manually and then mention those in queries for loading data.

So the query becomes

SELECT create_vlabel('agload_test_graph', 'Country'); //to create labels
SELECT load_labels_from_file('agload_test_graph',
                             'Country',
                             '/home/kamleshk/age_installation/age/regress/age_load/data/countries.csv'); // use labels

Is there any other way to assign the elabels or vlabels directly to the name of the CSV file like we do in AgCloud express for Agensql?

cybersam
  • 63,203
  • 6
  • 53
  • 76
Kamlesh Kumar
  • 351
  • 1
  • 7

5 Answers5

0

There isn't a direct way for you to do that. You can, however, make a function that will automate this process and achieve what you are trying to. The code may be:

from age import Graph
graph = Graph()
graph.open('localhost', 5432, 'db_name', 'username', 'password')
def automation_function(graph, label, csv_file):
     graph.execute(f"SELECT create_vlabel('agload_graph', '{label}');")
     graph.execute(f"SELECT load_label('agload_graph', '{label}', '{csv_file}');")
automation_function(graph, 'Country', 'path_to_file')
graph.close()

Let me know if it helps!

0

You can try writing a script in python which might help you achieve your desired task. You can use psycopg2 library to connect with the PostgresDB and further use the script below:

import psycopg2
import os

def load_csv_as_label(dbname, user, password, host, port, graph_name, csv_path):
    # Extract filename (without extension) as label from the csv_path
    label_name = os.path.splitext(os.path.basename(csv_path))[0]

    try:
        # Establish a connection to the PostgreSQL database
        conn = psycopg2.connect(
            dbname=dbname,
            user=user,
            password=password,
            host=host,
            port=port,
        )

        # Create a new database session and return a new instance of the connection class
        cur = conn.cursor()

        # Create a new vertex label with the extracted filename
        cur.execute(f"SELECT create_vlabel('{graph_name}', '{label_name}')")
        
        # Load data from the csv file into the new vertex label
        cur.execute(f"SELECT load_labels_from_file('{graph_name}', '{label_name}', '{csv_path}')")

        # Commit the transaction
        conn.commit()

        print(f"Data loaded successfully from {csv_path} to {label_name} label")
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()

you can call this function using:

load_csv_as_label('my_database', 'my_user', 'my_password', 'localhost', 5432, 'agload_test_graph', '/home/kamleshk/age_installation/age/regress/age_load/data/countries.csv')

Humza Tareen
  • 146
  • 6
0

Currently, there is no direct way to assign elabels or vlabels based on the name of the CSV file as you would do in AgCloud Express for Agensql.

In Apache-age, you need to explicitly create labels and then associate them with the CSV file using the load_labels_from_file function. This approach allows for more flexibility in defining the labels and their properties before loading the data.

Here's an example of how you can create labels and load data from a CSV file:

Create the labels:

SELECT create_vlabel('agload_test_graph', 'Country');

Load data from the CSV file using the labels:

SELECT load_labels_from_file('agload_test_graph',
                             'Country',
                           '/home/kamleshk/age_installation/age/regress/age_load/data/countries.csv');
tokamak32
  • 27
  • 7
0

Currently, there is no other way to assign the elabels or vlabels directly to the name of the CSV file. The process of assigning labels to CSV is manual for now. Writing your own script for this purpose can be one option. Hope it helps!

0

You need to create Elabels and Vlabels manually and then mention those for loading data When loading edges or Vertices from CSV file in Apache AGE and the Query is following:

SELECT create_vlabel ('graph_name', 'vertex_label');
SELECT load_labels_from_file ('graph_name', 'vertex_label', '/path/to/file.csv')

There is no direct way to assign Elabels or Vlabels bases on the name of the CSV file as you would do in AgCloud Express for Agesql. You can write a script in Python that uses the psycopg2 library to connect with PostgreDB and automate this process.

import psycopg2

# Connect to the PostgreSQL database
conn = psycopg2.connect(
dbname="my_database",
user="my_user",
password="my_password",
host="localhost",
port=5432,
)

# Create a cursor object
cur = conn.cursor()

# Execute a SQL query
cur.execute("SELECT * FROM my_table")

# Fetch the results
results = cur.fetchall()

# Print the results
for row in results:
print(row)

# Close the cursor and connection
cur.close()
conn.close()