Neo4j、REST API、java - 密码查询 [英] Neo4j, REST API, java - cypher queries

查看:25
本文介绍了Neo4j、REST API、java - 密码查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Java 学习了 REST API,并尝试运行这个简单的代码,但出现错误.这部分代码有问题:RestAPI graphDb = new RestAPI .... 我使用这个外部 JAR (http://m2.neo4j.org/content/repositories/releases/org/neo4j/neo4j-rest-graphdb/2.0.0/neo4j-rest-graphdb-2.0.0.jar)

I learn REST API with Java and tried run this simple code, but I got error. Something wrong with this part of code: RestAPI graphDb = new RestAPI.... I use this external JAR (http://m2.neo4j.org/content/repositories/releases/org/neo4j/neo4j-rest-graphdb/2.0.0/neo4j-rest-graphdb-2.0.0.jar)

import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.rest.graphdb.RestAPI;
import org.neo4j.rest.graphdb.RestAPIFacade;
import org.neo4j.rest.graphdb.RestGraphDatabase;
import org.neo4j.rest.graphdb.query.QueryEngine;
import org.neo4j.rest.graphdb.query.RestCypherQueryEngine;
import org.neo4j.rest.graphdb.util.QueryResult;

public class CypherQuery {

    public static void main(String[] args) {

        RestAPI graphDb = new RestAPIFacade("http://localhost:7474/db/data/");

        QueryEngine engine=new RestCypherQueryEngine(graphDb);  
        QueryResult<Map<String,Object>> result = engine.query("start n=node(*) return count(n) as total", Collections.EMPTY_MAP);  

        Iterator<Map<String, Object>> iterator=result.iterator();  
        if(iterator.hasNext()) {  
          Map<String,Object> row= iterator.next();  
          System.out.println("Total nodes: " + row.get("total"));

        }
    }
}

错误:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/ws/rs/core/Response$StatusType
    at org.neo4j.rest.graphdb.RestAPIFacade.<init>(RestAPIFacade.java:295)
    at cz.mendelu.bp.CypherQuery.main(CypherQuery.java:19)
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.core.Response$StatusType
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

推荐答案

就像@chrylis 指出的那样,您的错误似乎是您没有所需的 jars 从而导致错误的问题.现在从您的评论中,我看到您在理解 Maven 和依赖项方面遇到了困难.所以这是我为您制作的简单指南.

Like @chrylis pointed out, your error seems to be the issue that you don’t have the required jars and hence the errors. Now from your comments, I see that you are having difficulties with understanding maven and dependencies. So here's a simple guide that I made for you.

[了解这不是一站式指南,该程序可能开箱即用.目前它正在为我运行,但它取决于很多事情,包括您正在运行的 Neo4j 版本以及其他几个配置因素.尽管如此,这应该足以获得你开始了.]

[Understand that this is NOT a one stop guide and this program might not run out of the box. Its running for me at the moment but it depends on many things including what version of neo4j you are running and several other configuration factors. Nonetheless, this should be sufficient to get you started. ]

您需要在您的系统上安装 maven.Maven 上很少有很酷的教程.一个在这里.https://www.youtube.com/watch?v=al7bRZzz4oU&list=PL92E89440B7BFD0F6

You need to have maven installed on your system. There are few cool tutorials on maven. One is here. https://www.youtube.com/watch?v=al7bRZzz4oU&list=PL92E89440B7BFD0F6

但像我一样,如果你想要更快的方式,新的 Eclipse Luna 已经安装了 maven.因此,如果您愿意,请下载新的 eclipse luna.即使使用较旧的 Eclipse 版本,您也可以前往市场并为 Eclipse 安装 Maven.

But like me if you want a faster way, the new Eclipse Luna comes with maven installed for it. So download the new eclipse luna if you wish. Even with older eclipse versions you can go to marketplace and install the maven for eclipse.

完成后,创建一个 Maven 快速入门项目并将您的 pom.xml 文件替换为以下文件.

Once done, make a maven quickstart project and replace your pom.xml file with the one below.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>rash.experiments</groupId>
    <artifactId>neo4j</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>neo4j</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <repositories>
        <repository>
            <id>neo4j-release-repository</id>
            <name>Neo4j Maven 2 release repository</name>
            <url>http://m2.neo4j.org/content/repositories/releases/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-rest-graphdb</artifactId>
            <version>2.0.1</version>
        </dependency>
    </dependencies>
</project>

我假设您已经安装了 neo4j.我不会详细介绍,但是在neo4j目录,conf下,neo4j-server.properties文件中,您需要取消注释该行

I assume that you have neo4j setup. I will not go into major details, but in the neo4j directory, under conf, in neo4j-server.properties file, you need to uncomment the line

org.neo4j.server.webserver.address = 0.0.0.0

这基本上允许您从将在另一台机器上运行的 Java 代码访问此服务器.进行此更改后,请确保重新启动服务器并且其他机器可以访问它.要测试,您可以运行 http://ip.address.of.this.machine:7474 并且应该打开neo4j 附带的门户网站.

This basically lets you access this server from your java code that you will run from another machine. After making this change, make sure that you restart your server and that its accessible to other machines. To test you can run http://ip.address.of.this.machine:7474 and the web portal that comes with neo4j should open up.

注意:我假设您的数据库中有一些数据.这是需要,否则程序将失败.如果你需要一些样品数据,转到 http://ip_address_of_your_neo4j_web_server:7474/ 并加载安装时附带的电影图形数据库.

Note: I assume that you have some data in your database. This is required otherwise the program will fail. If you need some sample data, go to http://ip_address_of_your_neo4j_web_server:7474/ and load the movie graph database that comes with the installation.

现在让我们编码.在您上面创建的项目中创建这个类.

Now lets code. Make this class in the project that you created above.

package rash.experiments.neo4j;

import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

import org.neo4j.rest.graphdb.RestAPI;
import org.neo4j.rest.graphdb.RestAPIFacade;
import org.neo4j.rest.graphdb.query.QueryEngine;
import org.neo4j.rest.graphdb.query.RestCypherQueryEngine;
import org.neo4j.rest.graphdb.util.QueryResult;

public class Neo4JRestTest
{
    public static void main(String args[])
    {
        RestAPI graphDb = new RestAPIFacade("http://192.168.1.8:7474/db/data/");
        QueryEngine engine = new RestCypherQueryEngine(graphDb);
        QueryResult<Map<String, Object>> result = engine.query("start n=node(*) return count(n) as total", Collections.EMPTY_MAP);
        Iterator<Map<String, Object>> iterator = result.iterator();
        if (iterator.hasNext())
        {
            Map<String, Object> row = iterator.next();
            System.out.print("Total nodes: " + row.get("total"));
        } 
    }
}

现在要运行,您需要先构建您的项目,因为在您运行它之前,maven 不会下载您在 pom.xml 中指定的任何 jar.所以如果你安装了maven,到你的pom.xml所在的目录下,然后在命令行中写入mvn clean package.此命令将运行并安装所有依赖项,然后将运行您的程序.由于没有要运行的测试用例,它会成功.但我们的目标不是运行任何测试用例.它是下载所有的罐子.现在您拥有了一切,您可以运行 java 代码,您将看到最终结果.

Now to run, you need to build your project first because maven will not download any of your jars that your specified in pom.xml until you have run it. So if you installed maven, go to the directory where you have your pom.xml and then write in the command line mvn clean package. This command will run and install all the dependencies and then will run your program. Since there are no test cases to run, it will succeed. But our goal was not to run any test cases. It was to download all the jars. Now that you have everything, you can run the java code and you will see your end results.

如果您使用的是来自 eclispe 的 maven,那么您可以右键单击您的项目,然后以 -> maven build 的身份运行.第一次,将出现一个对话框.只需在其中写入包并按回车键即可.它会做与上面相同的事情并带来所有的罐子.然后像上面一样执行程序.

In case you are using maven from eclispe, then you right click your project and then do run as -> maven build. For the first time, a dialog will appear. Just write package in it and press enter. It will do the same things as above and bring all the jars. Then execute the program like you would above.

如果您收到诸如无端点"或读取 JSON 错误"之类的错误,请了解 REST API 以某种方式无法读取您的图表.

In case you get errors such as "No endpoint" or "error reading JSON", then understand that somehow the REST API is not able to read your graph.

检查neo4j-server.properties 中的属性.它应该是您在网址中提到的任何内容.

check the property inside neo4j-server.properties. It should be whatever you are mentioning in your URL.

org.neo4j.server.webadmin.data.uri =/db/data/

org.neo4j.server.webadmin.data.uri = /db/data/

这篇关于Neo4j、REST API、java - 密码查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆