我想使用cypher查询在java中获取特定节点但我无法获取它。 并且在debbuging时间debbuger从fetchNodes()方法中的特定for循环返回。 我无法正确地解读它。
类:NewCypherQuery(bean)
public class NewCypherQuery {
private static final String DB_PATH = "/var/lib/neo4j/data/";
private static String resultString, columnsString, nodeResult, rows = "",
query;
private static ExecutionResult result;
private static ExecutionEngine engine;
private static GraphDatabaseService db;
private static Node amad = null,pari=null,sona=null;
private static Relationship rel;
public static void callAllMethods() {
clearDbPath();
setUp();
createNodes();
// fetchNodes();
matchData();
}
private static void setUp() {
db = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
engine = new ExecutionEngine(db);
}
private static void createNodes() {
try (Transaction tx = db.beginTx();) {
amad = db.createNode();
amad.setProperty("name", "Amad");
amad.setProperty("age", 24);
amad.setProperty("edu", "mscit");
pari = db.createNode();
pari.setProperty("name", "Pari");
pari.setProperty("age", 20);
pari.setProperty("edu", "mscit");
sona = db.createNode();
sona.setProperty("name", "Sona");
sona.setProperty("age", 21);
sona.setProperty("edu", "mscit");
rel=amad.createRelationshipTo(pari, RelTypes.KNOWS);
rel.setProperty("rel", "friend");
rel=pari.createRelationshipTo(sona, RelTypes.KNOWS);
rel.setProperty("rel", "friend");
/*query = "CREATE (amad:User1 { name: 'Amad1',age : 20,education : 'Mscit' }),(pari:User1 { name : 'Pari1' }),(sona:User1 { name : 'Sona1'}),(amad)-[:FRIEND]->(pari),(pari)-[:FRIEND]->(sona)";
result = engine.execute(query);*/
System.out.println("Nodes created.....");
tx.success();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void fetchNodes() {
try (Transaction ignored = db.beginTx();) {
result = engine
.execute("start n=node(*) where n.name = 'Rita' return n, n.name");
// END SNIPPET: execute
// START SNIPPET: items
Iterator<Node> n_column = result.columnAs("n");
for (Node node : IteratorUtil.asIterable(n_column)) {
// note: we're grabbing the name property from the node,
// not from the n.name in this case.
nodeResult = node + ": " + node.getProperty("name");
System.out.println(nodeResult.length());
}
List<String> columns = result.columns();
// END SNIPPET: columns
// the result is now empty, get a new one
result = engine
.execute("start n=node(*) where n.name = 'Rita' return n, n.name");
// START SNIPPET: rows
for (Map<String, Object> row : result) {
for (Entry<String, Object> column : row.entrySet()) {
rows += column.getKey() + ": " + column.getValue() + "; ";
}
rows += "\n";
// END SNIPPET: rows
resultString = engine
.execute(
"start n=node(*) where n.name = 'Rita' return n, n.name")
.dumpToString();
System.out.println(resultString);
columnsString = columns.toString();
db.shutdown();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void clearDbPath() {
try {
deleteRecursively(new File(DB_PATH));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
servlet:(InsertNodes)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
NewCypherQuery ncq=new NewCypherQuery();
ncq.callAllMethods();
}
的index.jsp
<form method="post" action="InsertNodes">
<input type="text" name="txtname" value="Hello World !!!!!"></input>
<input type="submit" value="Neo4j World"></input>
</form>
答案 0 :(得分:0)
我绝对不确定我是否理解你的问题。
您只能在ExecutionResult
上进行一次迭代。由于迭代器已经用完,后续迭代将不执行任何操作。这可能看起来很奇怪。当您想要在使用它之前记录查询结果时。内部Cypher尽可能保持内存保守,并试图避免在使用迭代器时构建有利于直接遍历图形的集合。
如果您想多次迭代,您需要填充一个集合,您可以使用例如IteratorUtil.asList
。这样做的结果就是能够以内存消耗为代价进行多次迭代。
答案 1 :(得分:0)
Chauhan保持,
你不应该得到任何结果。您创建了三个节点,名称为Amad,Pari和Sona。然后,您搜索名为Rita的节点。没有名为Rita的节点。您的编码也存在许多其他问题,但Neo4j正在按照您的要求进行操作。
恩典与和平,
吉姆