unknown
unknown

Reputation: 343

send a query in neo4j [JAVA]

I am new to neo4j and graph database, and I have to send a query to get some values.

I have food and category nodes, and the relationship type between the two is specified by another node categorized_as.

What I need to fetch is the pair of food_name and its category_name.

Thanks for your help in advance.

Upvotes: 0

Views: 640

Answers (1)

FrobberOfBits
FrobberOfBits

Reputation: 18022

Here's the documentation on how to run cypher queries from java. Adapted for your example, it would look like this:

// Create a new graph DB at path DB_PATH
GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );

// Create a new execution engine for running queries.
ExecutionEngine engine = new ExecutionEngine( db );

ExecutionResult result;

// Queries need to be run inside of transactions...
try ( Transaction ignored = db.beginTx() )
{
    String query = "MATCH (f:food)-[:categorized_as]->(c:category) RETURN f.food_name as foodName, c.category_name as categoryName";

    // Run that query we just defined.
    result = engine.execute(query);

    // Pull out the "foodNames" column from the result indicated by the query.
    Iterator<String> foodNames = result.columnAs( "foodName" );
    // Iterate through foodNames...
}

Upvotes: 1

Related Questions