簡體   English   中英

使用java中嵌入的neo4j進行查詢

[英]Query using neo4j embedded in java

我有2個節點:名稱和城市。 這兩者之間的關系是(姓名)[:LIVES_IN] - >(城市)。 我正在嘗試生成一個查詢,以找出生活在城市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上創建索引:CREATE INDEX ON:城市(城市)( http://docs.neo4j.org/chunked/stable/query-schema-index.html

另請閱讀以下學習資料:

在線培訓課程: http//www.neo4j.org/learn/online_course

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

學習Cypher: http//www.neo4j.org/tracks/cypher_track_start

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM