在neo4j发送查询[JAVA]

时间:2014-09-15 14:04:12

标签: java neo4j

我是neo4j和graph数据库的新手,我必须发送一个查询来获取一些值。

我有食物类别节点,两者之间的关系类型由另一个节点 categorized_as 指定。

我需要提取的是一对 food_name 及其 category_name

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

Here's the documentation on how to run cypher queries from java。根据您的示例进行调整,它看起来像这样:

// 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...
}