java - 我如何在我在java应用程序中创建的Neo4j Web管理中查看索引及其节点?

标签 java neo4j

我正在尝试访问数据并将其保存到我的 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

我收到错误消息:索引“节点”不存在那么我怎样才能看到它呢?或者我做错了什么?

Neo4j 版本:1.9.3

最佳答案

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

关于java - 我如何在我在java应用程序中创建的Neo4j Web管理中查看索引及其节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18561200/

相关文章:

java - 我使用什么作为数据库路径?

sql-server - 使用 Neo4j 进行最终用户身份验证

java - 如何用 Math Ml 标签包围数字?

java - 如何从 vaadin 7 中的流打印 pdf 文件?

neo4j - 必须为 Neo4j 中的 CREATE 指定单一关系类型

python - 与新模型中多种类型(多态性)的关系

java - 使用 Selenium WebDriver 的 Spring Boot Web 应用程序

java - 有没有我可以用来运行一些测试的免费虚拟智能卡?

java - 在 WildFly 上部署 Spring Boot WAR

c# - 针对 C# 开发人员的 Neo4j 和 MSSQL 实用性能比较