【问题标题】:how can i see the index and its nodes in Neo4j web admin i created in java application?我如何在 Java 应用程序中创建的 Neo4j Web 管理员中查看索引及其节点?
【发布时间】:2013-09-01 17:50:43
【问题描述】:

我正在尝试访问并将数据保存到我的 neo4j 服务器,但是尽管我的代码和 server-properties.file 都显示了相同的数据库位置,但我在 webadmin 界面中看不到结果。

这是我写的代码(因为我之前创建用户,所以不包括用户创建代码):

package graph;

import java.util.Iterator;
import java.util.Random;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.index.Index;

public class GraphTest {

    static GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( "data/graph.db" );
    static Index<Node> nodeIndex = graphDb.index().forNodes("nodes");

    private static enum RelTypes implements RelationshipType
    {
        CALLED
    }

    public static void addRelationships(){
        int caller;
        int temp=0;
        int callee;
        Random rand = new Random();

        for(int i = 0 ;i<1000;i++){
            caller=rand.nextInt(100);
            temp=rand.nextInt(100);
            while(temp==caller)
                temp=rand.nextInt(100);
            callee=temp;


            String callerName = idToUserName( caller );
            Node foundCaller= nodeIndex.get( "uname", callerName ).getSingle();
            System.out.println( "The username of user " + caller + " is "
            + foundCaller.getProperty( "uname" ) );

            String calleeName = idToUserName( callee);
            Node foundCallee= nodeIndex.get( "uname", calleeName ).getSingle();
            System.out.println( "The username of user " + callee + " is "
            + foundCallee.getProperty( "uname" ) );


            System.out.println(callerName+" called " + calleeName);

            foundCaller.createRelationshipTo(foundCallee, RelTypes.CALLED);
            Iterable<Relationship> rels = foundCaller.getRelationships(RelTypes.CALLED, Direction.OUTGOING);
            Iterator<Relationship> x = rels.iterator();
            while(x.hasNext()){
                Relationship r = x.next();
                System.out.println(r.toString());
            }

        }

    }

    private static String idToUserName( final int id )
    {
        return "user" + String.valueOf(id) ;
    }

    private static Node createAndIndexUser( final String username )
    {
        Node node = graphDb.createNode();
        node.setProperty( "uname", username );
        nodeIndex.add( node,"uname", username );
        return node;
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Node firstNode;
        Node secondNode;
        Relationship relationship;

        System.out.println(nodeIndex.getName()+"  " +
nodeIndex.getGraphDatabase().getNodeById(45).getProperty("uname") );
        registerShutdownHook( graphDb );

        Transaction tx = graphDb.beginTx();
        try
        {
        // Updating operations go here
            // Create some users and index their names with the IndexService

            int idToFind = 67;
            String userName = idToUserName( idToFind );
            Node foundUser = nodeIndex.get( "uname", userName ).getSingle();
            System.out.println( "The username of user " + idToFind + " is "
            + foundUser.getProperty( "uname" ) );


            addRelationships();





            tx.success();

        }
        finally
        {
            tx.finish();

        }

        graphDb.shutdown();

    }

    private static void registerShutdownHook(final GraphDatabaseService graphDb) {
        // TODO Auto-generated method stub
        // Registers a shutdown hook for the Neo4j instance so that it
        // shuts down nicely when the VM exits (even if you "Ctrl-C" the
        // running application).
        Runtime.getRuntime().addShutdownHook( new Thread()
        {
        @Override
        public void run()
        {
        graphDb.shutdown();
        }
        } );

    }

}

这是 conf/neo4j-server.properties 文件的内容:

# location of the database directory 
org.neo4j.server.database.location=data/graph.db

当我尝试在 webadmin 界面(http://localhost:7474/webadmin)中编写查询时,例如:

START n=node:nodes(uname="user45")
RETURN n

我收到错误消息:Index 'nodes' doesnt exist 那么我怎么才能看到呢?或者我做错了什么?

Neo4j 版本:1.9.3

【问题讨论】:

    标签: java neo4j


    【解决方案1】:

    您确定您的代码与您的 Neo4j 服务器在同一工作目录中运行吗?或许你可以在你的Java程序中打印出当前工作目录的路径?

    【讨论】:

    • hmmm.. 我想我很确定,因为我读到了一些问题和答案,如果我给 newEmbeddedGraphDatabase 的 DB_PATH 和 conf/neo4j-server.properties 文件中的 db 位置设置完全相同.所以,我的 neo4j 安装文件夹是:D:\Development\Servers\neo4j-community-1.9.3 ..... 并且在代码中设置了相同的 data/graph.db ?这是我想念的吗?这是 System.out.println(new File(".")); 的输出: D:\Development\workspaces\GraphDB\GraphTest\ 那么,我应该改变什么?
    • 我更改了static GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( "D:\\Development\\Servers\\neo4j-community-1.9.3\\data\\graphdb" ) 并且成功了
    【解决方案2】:

    在 Web 控制台上,您可以键入 index --indexes 以查看所有可用索引。

    【讨论】:

    • 我在电源控制台上使用了这个选项,但是它返回因为没有索引.. 那么可能是什么问题?我错过了什么吗?我无法理解的是,数据库位置的两个设置在代码和 conf/server.properties 文件中都是相同的。但是我可以从代码中读取数据库,但在 Web 界面上看不到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    相关资源
    最近更新 更多