Neo4j的,REST API,JAVA - 暗号查询 [英] Neo4j, REST API, java - cypher queries

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

问题描述

我学会了与Java REST API,并试图运行这个简单的code,但我得到了错误。蹊跷的code的这一部分:RESTAPI graphDb =新RESTAPI ....我用这个外部JAR(<一个href=\"http://m2.neo4j.org/content/repositories/releases/org/neo4j/neo4j-rest-graphdb/2.0.0/neo4j-rest-graphdb-2.0.0.jar\" rel=\"nofollow\">http://m2.neo4j.org/content/repositories/releases/org/neo4j/neo4j-rest-graphdb/2.0.0/neo4j-rest-graphdb-2.0.0.jar)

 进口java.util.Collections中;
进口java.util.Iterator的;
进口的java.util.Map;进口org.neo4j.graphdb.GraphDatabaseService;
进口org.neo4j.rest.graphdb.RestAPI;
进口org.neo4j.rest.graphdb.RestAPIFacade;
进口org.neo4j.rest.graphdb.RestGraphDatabase;
进口org.neo4j.rest.graphdb.query.QueryEngine;
进口org.neo4j.rest.graphdb.query.RestCypherQueryEngine;
进口org.neo4j.rest.graphdb.util.QueryResult;公共类CypherQuery {    公共静态无效的主要(字串[] args){        RESTAPI graphDb =新RestAPIFacade(HTTP://本地主机:7474 / DB /数据/);        QueryEngine发动机=新RestCypherQueryEngine(graphDb);
        QueryResult中&LT;地图&LT;弦乐,对象&gt;&GT;结果= engine.query(开始N =节点(*)返回数(N)占总,Collections.EMPTY_MAP);        迭代器&LT;地图&LT;弦乐,对象&gt;&GT;迭代= result.iterator();
        如果(iterator.hasNext()){
          地图&LT;弦乐,对象&gt;行= iterator.next();
          的System.out.println(总节点:+ row.get(总));        }
    }
}

错误:

 异常线程mainjava.lang.NoClassDefFoundError的:使用javax / WS / RS /核心/响应$ StatusType
    在org.neo4j.rest.graphdb.RestAPIFacade&LT;&初始化GT;(RestAPIFacade.java:295)
    在cz.mendelu.bp.CypherQuery.main(CypherQuery.java:19)
抛出java.lang.ClassNotFoundException:引起javax.ws.rs.core.Response $ StatusType
    在java.net.URLClassLoader的$ 1.run(URLClassLoader.java:366)
    在java.net.URLClassLoader的$ 1.run(URLClassLoader.java:355)
    在java.security.AccessController.doPrivileged(本机方法)
    在java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    在java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)
    在java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2个


解决方案

像@chrylis指出的那样,你的错误似乎是没有必要的罐子,因此错误的问题。现在从您的意见,我看到你有与Maven的理解和依赖的困难。所以这里有一个简单的指南,我为你做。


  

[明白这不是一个一站式指南,该程序可能
  还没用完的方块。它为我跑过来的时刻,但它
  取决于很多事情,包括你正在运行的版本的Neo4j的
  和其他一些配置因素。然而,这应当足以获得
  你开始。 ]


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

但像我这样的,如果你想更快的方法,新的Eclipse月神预装有它的Maven。所以,如果你想下载新的Eclipse月神。即使与旧的Eclipse版本,你可以去市场和安装的Maven的Eclipse插件。

一旦这样做,使Maven快速启动项目,并与下面的替换你的pom.xml文件。

 &LT;项目的xmlns =htt​​p://maven.apache.org/POM/4.0.0的xmlns:XSI =htt​​p://www.w3.org/2001 / XML模式实例
    XSI:的schemaLocation =htt​​p://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">
    &LT; modelVersion&GT; 4.0.0&LT; / modelVersion&GT;    &LT;&的groupId GT; rash.experiments&LT; /的groupId&GT;
    &LT;&的artifactId GT;的Neo4j&LT; / artifactId的&GT;
    &LT;&版GT; 0.0.1-SNAPSHOT&LT; /版本&GT;
    &LT;包装和GT;&罐子LT; /包装&GT;    &LT;名称&gt;&Neo4j的LT; /名称&gt;
    &LT; URL&GT; HTTP://maven.apache.org< / URL&GT;    &LT;性状&gt;
        &LT; project.build.sourceEncoding&GT; UTF-8&LT; /project.build.sourceEncoding>
    &LT; /性状&gt;    &LT;库&GT;
        &LT;&库GT;
            &LT;&ID GT;的Neo4j释放存储库&LT; / ID&GT;
            &LT;名称&gt;的Neo4j的Maven 2版本库&LT; /名称&gt;
            &LT; URL&GT; HTTP://m2.neo4j.org/content/repositories/releases/< / URL&GT;
            &LT;发布和G​​T;
                &LT;&启用GT;真&LT; /启用&GT;
            &LT; /释放&GT;
            &LT;&快照GT;
                &LT;&启用GT假LT; /启用&GT;
            &LT; /快照&GT;
        &LT; /存储库&GT;
    &LT; /存储库&GT;    &LT;依赖和GT;
        &LT;&依赖性GT;
            &LT;&的groupId GT;的JUnit&LT; /的groupId&GT;
            &LT;&的artifactId GT;的JUnit&LT; / artifactId的&GT;
            &LT;&版GT; 3.8.1&LT; /版本&GT;
            &LT;&范围GT;试验&LT; /&范围GT;
        &LT; /依赖性&GT;
        &LT;&依赖性GT;
            &LT;&的groupId GT; org.neo4j&LT; /的groupId&GT;
            &LT;&的artifactId GT;的Neo4j-休息-graphdb&LT; / artifactId的&GT;
            &LT;&版GT; 2.0.1&LT; /版本&GT;
        &LT; /依赖性&GT;
    &LT; /依赖和GT;
&LT; /项目&GT;

我假设你有Neo4j的设置。我不会去到大的细节,但在Neo4j的目录,conf下,在neo4j-server.properties文件,你需要去掉注释

  org.neo4j.server.webserver.address = 0.0.0.0

这基本上可以让你从你的java code,你会从另一台机器上运行访问该服务器。进行此更改后,请确保您重新启动服务器,其对其他机器访问。为了测试你可以运行 HTTP://ip.address.of.this.machine:7474 和随之而来的Neo4j门户网站应该开放。


  

请注意:我假设你在数据库中有一些数据。这是
  所需否则程序将失败。如果你需要一些样本
  数据,转到<一个href=\"http://ip_address_of_your_neo4j_web_server:7474/\">http://ip_address_of_your_neo4j_web_server:7474/并加载
  附带的安装电影图形数据库。


现在让code。使您在上面创建的项目这个类。

 包rash.experiments.neo4j;进口java.util.Collections中;
进口java.util.Iterator的;
进口的java.util.Map;进口org.neo4j.rest.graphdb.RestAPI;
进口org.neo4j.rest.graphdb.RestAPIFacade;
进口org.neo4j.rest.graphdb.query.QueryEngine;
进口org.neo4j.rest.graphdb.query.RestCypherQueryEngine;
进口org.neo4j.rest.graphdb.util.QueryResult;公共类Neo4JRestTest
{
    公共静态无效的主要(字符串ARGS [])
    {
        RESTAPI graphDb =新RestAPIFacade(http://192.168.1.8:7474/db/data/);
        QueryEngine发动机=新RestCypherQueryEngine(graphDb);
        QueryResult中&LT;地图&LT;弦乐,对象&gt;&GT;结果= engine.query(开始N =节点(*)返回数(N)占总,Collections.EMPTY_MAP);
        迭代器&LT;地图&LT;弦乐,对象&gt;&GT;迭代= result.iterator();
        如果(iterator.hasNext())
        {
            地图&LT;弦乐,对象&gt;行= iterator.next();
            System.out.print(总节点:+ row.get(总));
        }
    }
}

现在运行,你需要首先建立自己的项目,因为直到你运行它的Maven不会下载任何你在pom.xml中指定的罐子。所以,如果你安装的Maven,转到你有你的pom.xml,然后在命令行MVN清洁套装写的目录。此命令将运行并安装所有的依赖关系,然后将运行程序。由于没有测试用例运行时,它会成功。但是,我们的目标是不要运行任何测试用例。这是下载所有的罐子。现在,你拥有了一切,你可以运行Java code和你会看到你的最终结果。

如果你正在使用maven从Eclispe的,然后你右键点击你的项目,然后就作为运行 - > Maven构建。对于第一次,将出现一个对话框。就在写包和preSS进入。它会做同样的事情如前,把所有的罐子。然后,像你上面将执行程序。

在情况下,你会得到错误,如没有终点或错误读取JSON,然后明白,不知何故REST API无法读取您的图形。

检查里面neo4j-server.properties财产。它应该是无论你在你的URL提。


  

org.neo4j.server.webadmin.data.uri = /数据库/数据/


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"));

        }
    }
}

Error:

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

解决方案

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.

[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. ]

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

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.

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>

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

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.

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"));
        } 
    }
}

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.

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.

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.

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/

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

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