gpt4 book ai didi

java - 使用java中嵌入的neo4j进行查询

转载 作者:行者123 更新时间:2023-12-02 06:04:55 26 4
gpt4 key购买 nike

我有 2 个节点:名称和城市。两者之间的关系是(name)[:LIVES_IN]->(city)。我正在尝试生成一个查询来找出居住在 X 城市的人(其中 X 将来自文本框)。

我正在尝试按照 Luanne 和 Micheal Hunger 的建议构造此查询:

import java.io.IOException;
import java.util.ArrayList;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.helpers.collection.IteratorUtil;
import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;

public class registrationFrame extends javax.swing.JFrame {

public static final String DB_PATH = "D://data";
public static GraphDatabaseService graphDb = null;
Node person;
Node password;
Node city;
String nodeResulta;

public registrationFrame() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//node and relationship creation code

try (Transaction tx = graphDb.beginTx();) {
person = graphDb.createNode();
person.setProperty("name", jTextField1.getText());
person.setProperty("password", jPasswordField1.getPassword());
graphDb.index().forNodes("name").add(person, "name", jTextField1.getText());


city = graphDb.createNode();
city.setProperty("city_name", jTextField2.getText());
graphDb.index().forNodes("city_name").add(city, "city_name", jTextField2.getText());

person.createRelationshipTo(city, RelTypes.LIVES_IN);

tx.success();
}

}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//query code
ExecutionEngine engine = new ExecutionEngine(graphDb);
ExecutionResult result;
String temp=jTextField2.getText();
Map<String,Object> params=new HashMap<>();
//result = engine.execute("START n=node(*) MATCH (x:city)<-[:LIVES_IN]-(y:person) where x.name='"+jTextField2.getText()+"' RETURN y.name;");
//List<String> columns = result.columns();
//Iterator<Node> n_column = result.columnAs( "person" );
try (Transaction ignored = graphDb.beginTx()) {
//result = engine.execute("START n=node(*) MATCH (x:city)<-[:LIVES_IN]-(y:person) where x.name='"+temp+"' RETURN y");
// END SNIPPET: execute
// START SNIPPET: items
//result = engine.execute("START n=node(*) MATCH (x:city) RETURN x");//this query also returns nothing

params.put("c_name",temp);
result=engine.execute("MATCH (city_name:city {city_name:{c_name}})<-[:LIVES_IN]-(person) RETURN person",params);
System.out.println(result);
Iterator<Node> n_column = result.columnAs("person");

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.
nodeResulta = node + ": " + node.getProperty("name") + '\n';
//nodeResult1.add(node.getProperty( "name" ).toString());
}
// END SNIPPET: items
}

jTextArea1.setText(nodeResulta);// output will show here

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new registrationFrame().setVisible(true);
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
registerShutdownHook(graphDb);
//System.out.println("Created Social Graph!!");
}
});
}
private static void registerShutdownHook(final GraphDatabaseService graphDb) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
graphDb.shutdown();
}
});
}

public static enum RelTypes implements RelationshipType {

LIVES_IN,
FRIEND,
CUISINE,
LIKES,
IN
}

但是这个查询没有给出任何结果以及任何异常。

我的查询格式正确吗?谁能告诉我如何解决这个问题?我应该更改我的 neo4j 版本吗,因为我正在遵循 Luanne 和 Miheal HUnger 要求的一切。

谢谢

最佳答案

您尚未引用城市值:

start n=node(*) MATCH n-[:LIVES_IN]->city where city.city=dhaka return n.name

应该是

start n=node(*) MATCH n-[:LIVES_IN]->city where city.city='dhaka' return n.name

另外,请使用参数:

start n=node(*) MATCH n-[:LIVES_IN]->city where city.city={city} return n.name

http://docs.neo4j.org/chunked/stable/tutorials-cypher-parameters-java.html

编辑由于您已根据迈克尔的评论修改了查询,请尝试

   Map<String,Object> params=new HashMap<String,Object>();
params.put("city_name","dhaka");
result=engine.execute("MATCH (city:City {city:{city_name})<-[:LIVES_IN]-(person) RETURN person",params);

Iterator<Node> n_column = result.columnAs( "person" );

在此之前创建 City 索引:在 :City(城市) 上创建索引(http://docs.neo4j.org/chunked/stable/query-schema-index.html)

另请阅读以下学习 Material :

在线培训类(class):http://www.neo4j.org/learn/online_course

手册:http://docs.neo4j.org/chunked/stable/

学习密码:http://www.neo4j.org/tracks/cypher_track_start

关于java - 使用java中嵌入的neo4j进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22372991/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com