NEO4J Java Cypher驱动程序错误

时间:2016-07-13 14:33:59

标签: java maven neo4j lucene cypher

我正在关注neo4j tutorial,以便通过Java API将json数据加载到neo4j

    /*
 * Copyright (c) 2002-2016 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.helpers.collection.Iterators;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonMap;
import static org.neo4j.io.fs.FileUtils.deleteRecursively;

public class JavaQuery2 {
    private static final File DB_PATH = new File("/Users/ahmedov/Documents/Neo4j/db4");
    String resultString;
    String columnsString;
    String nodeResult;
    String rows = "";
    CloseableHttpClient http;
    HttpResponse response;
    Object json;
    String jsonData = "{\n" +
            "    \"items\": [{\n" +
            "        \"answers\": [{\n" +
            "            \"owner\": {\n" +
            "                \"reputation\": 488,\n" +
            "                \"user_id\": 737080,\n" +
            "                \"user_type\": \"registered\",\n" +
            "                \"accept_rate\": 45,\n" +
            "                \"profile_image\": \"https://www.gravatar.com/avatar/\n" +
            "ffa6eed1e8a9c1b2adb37ca88c07dede?s=128&d=identicon&r=PG\",\n" +
            "                \"display_name\": \"Chris Leishman\",\n" +
            "                \"link\": \"http://stackoverflow.com/users/737080/chris-leishman\"\n" +
            "            },\n" +
            "            \"tags\": [],\n" +
            "            \"comment_count\": 0,\n" +
            "            \"down_vote_count\": 0,\n" +
            "            \"up_vote_count\": 2,\n" +
            "            \"is_accepted\": false,\n" +
            "            \"score\": 2,\n" +
            "            \"last_activity_date\": 1404772223,\n" +
            "            \"creation_date\": 1404772223,\n" +
            "            \"answer_id\": 24620959,\n" +
            "            \"question_id\": 24620768,\n" +
            "            \"share_link\": \"http://stackoverflow.com/a/24620959\",\n" +
            "            \"body_markdown\": \"The simplest would be to use an ... some discussion on this here:\n" +
            " http://docs.neo4j.org/chunked/stable/cypherdoc-linked-lists.html)\",\n" +
            "            \"link\": \"http://stackoverflow.com/questions/24620768/neo4j-cypher-query-get-last-n-elements/24620959#24620959\",\n" +
            "            \"title\": \"Neo4j cypher query: get last N elements\"\n" +
            "        }],\n" +
            "        \"tags\": [\"neo4j\", \"cypher\"],\n" +
            "        \"owner\": {\n" +
            "            \"reputation\": 815,\n" +
            "            \"user_id\": 1212067,\n" +
            "            \"user_type\": \"registered\",\n" +
            "            \"accept_rate\": 73,\n" +
            "            \"profile_image\": \"http://i.stack.imgur.com/nnyS1.png?s=128&g=1\",\n" +
            "            \"display_name\": \"C&#233;sar Garc&#237;a Tapia\",\n" +
            "            \"link\": \"http://stackoverflow.com/users/1212067/c%c3%a9sar-garc%c3%ada-tapia\"\n" +
            "        },\n" +
            "        \"comment_count\": 0,\n" +
            "        \"delete_vote_count\": 0,\n" +
            "        \"close_vote_count\": 0,\n" +
            "        \"is_answered\": true,\n" +
            "        \"view_count\": 14,\n" +
            "        \"favorite_count\": 0,\n" +
            "        \"down_vote_count\": 0,\n" +
            "        \"up_vote_count\": 1,\n" +
            "        \"answer_count\": 1,\n" +
            "        \"score\": 1,\n" +
            "        \"last_activity_date\": 1404772230,\n" +
            "        \"creation_date\": 1404771217,\n" +
            "        \"question_id\": 24620768,\n" +
            "        \"share_link\": \"http://stackoverflow.com/q/24620768\",\n" +
            "        \"body_markdown\": \"I have a graph that...How can I do that?\",\n" +
            "        \"link\": \"http://stackoverflow.com/questions/24620768/neo4j-cypher-query-get-last-n-elements\",\n" +
            "        \"title\": \"Neo4j cypher query: get last N elements\"\n" +
            "    }, {\n" +
            "        \"tags\": [\"neo4j\", \"cypher\"],\n" +
            "        \"owner\": {\n" +
            "            \"reputation\": 63,\n" +
            "            \"user_id\": 845435,\n" +
            "            \"user_type\": \"registered\",\n" +
            "            \"accept_rate\": 67,\n" +
            "            \"profile_image\": \"https://www.gravatar.com/avatar/\n" +
            "610458a30958c9d336ee691fa1a87369?s=128&d=identicon&r=PG\",\n" +
            "            \"display_name\": \"user845435\",\n" +
            "            \"link\": \"http://stackoverflow.com/users/845435/user845435\"\n" +
            "        },\n" +
            "        \"comment_count\": 0,\n" +
            "        \"delete_vote_count\": 0,\n" +
            "        \"close_vote_count\": 0,\n" +
            "        \"is_answered\": false,\n" +
            "        \"view_count\": 16,\n" +
            "        \"favorite_count\": 0,\n" +
            "        \"down_vote_count\": 0,\n" +
            "        \"up_vote_count\": 0,\n" +
            "        \"answer_count\": 0,\n" +
            "        \"score\": 0,\n" +
            "        \"last_activity_date\": 1404768987,\n" +
            "        \"creation_date\": 1404768987,\n" +
            "        \"question_id\": 24620297,\n" +
            "        \"share_link\": \"http://stackoverflow.com/q/24620297\",\n" +
            "        \"body_markdown\": \n" +
            "\"I&#39;m trying to implement a simple graph db for NYC subway................Thanks!\\r\\n\",\n" +
            "        \"link\": \"http://stackoverflow.com/questions/24620297/cypher-query-with-infinite-relationship-takes-forever\",\n" +
            "        \"title\": \"Cypher query with infinite relationship takes forever\"\n" +
            "    }],\n" +
            "    \"has_more\": true,\n" +
            "    \"quota_max\": 300,\n" +
            "    \"quota_remaining\": 205\n" +
            "}";

    String query =
            "WITH {json} as data\n" +
                    "UNWIND data.items as q\n" +
                    "MERGE (question:Question {id:q.question_id}) ON CREATE\n" +
                    "  SET question.title = q.title, question.share_link = q.share_link, question.favorite_count = q.favorite_count\n" +
                    "MERGE (owner:User {id:q.owner.user_id}) ON CREATE SET owner.display_name = q.owner.display_name\n" +
                    "MERGE (owner)-[:ASKED]->(question)\n" +
                    "FOREACH (tagName IN q.tags | MERGE (tag:Tag {name:tagName}) MERGE (question)-[:TAGGED]->(tag))\n" +
                    "FOREACH (a IN q.answers |\n" +
                    "MERGE (question)<-[:ANSWERS]-(answer:Answer {id:a.answer_id})\n" +
                    "MERGE (answerer:User {id:a.owner.user_id}) ON CREATE SET answerer.display_name = a.owner.display_name\n" +
                    "MERGE (answer)<-[:PROVIDED]-(answerer))";


    String apiUrl = "https://api.stackexchange.com/2" + ".2/questions?pagesize=100&order=desc&sort=creation&tagged=neo4j&site=stackoverflow&filter=!5-i6Zw8Y)4W7vpy91PMYsKM-k9yzEsSC1_Uxlf";

    private void initiatehHttpResponse() throws IOException {
        http = HttpClients.custom()
                .disableContentCompression()
                .build();
        this.response = http.execute(new HttpGet(apiUrl));
        System.out.println("HTTP Encoding: " + this.response.getEntity().getContentEncoding());
        System.out.println(this.response);
    }


    private void initiateJson() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        HttpEntity entity = response.getEntity();

        //decompress the gzip entity
        HttpEntity entityDecomp = new GzipDecompressingEntity(entity);

        //print out the json string
        //Warning: EntityUtils.toString closes HttpEntity's input stream
        String content = null;
        if (entity != null) {
            content = EntityUtils.toString(entityDecomp, Charset.forName("UTF-8").name());
            //System.out.println(content);
        }
        //encode the string in utf8
        byte ptext[] = content.getBytes(ISO_8859_1);
        String jsonutf8 = new String(ptext, UTF_8);
        this.json = mapper.readValue(jsonutf8, Map.class);
        System.out.println(this.json);
    }

    public static void main(String[] args) throws IOException {
        JavaQuery2 javaQuery = new JavaQuery2();
        javaQuery.initiatehHttpResponse();
        javaQuery.initiateJson();

        javaQuery.run();
    }

    void run() {
        clearDbPath();


        GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
        Transaction tjson = db.beginTx();
        try {
            System.out.println(this.query);
            System.out.println(this.json);
            Result result1 = db.execute(this.query, singletonMap("json", this.json));
            System.out.println(result1.resultAsString());
            tjson.success();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tjson.close();
        }


        // START SNIPPET: addData
        Transaction tx = db.beginTx();
        try {
            Node myNode = db.createNode();
            myNode.setProperty("name", "my node");
            tx.success();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            tx.close();
        }

        // END SNIPPET: addData

        // START SNIPPET: execute
        Transaction ignored = db.beginTx();
        try {
            Result result = db.execute("match (n {name: 'my node'}) return n, n.name");
            while (result.hasNext()) {
                Map<String, Object> row = result.next();
                for (Entry<String, Object> column : row.entrySet()) {
                    rows += column.getKey() + ": " + column.getValue() + "; ";
                }
                rows += "\n";
            }
            ignored.success();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ignored.close();
        }
        // END SNIPPET: execute

        // the result is now empty, get a new one
        Transaction ignored2 = db.beginTx();
        try {

            Result result = db.execute("match (n {name: 'my node'}) return n, n.name");
            // START SNIPPET: items
            Iterator<Node> n_column = result.columnAs("n");
            for (Node node : Iterators.asIterable(n_column)) {
                nodeResult = node + ": " + node.getProperty("name");
            }
            // END SNIPPET: items

            // START SNIPPET: columns
            List<String> columns = result.columns();
            // END SNIPPET: columns
            columnsString = columns.toString();
            resultString = db.execute("match (n {name: 'my node'}) return n, n.name").resultAsString();
            ignored2.success();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ignored2.close();
        }

        db.shutdown();
    }

    private void clearDbPath() {
        try {
            deleteRecursively(DB_PATH);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

但是,我收到以下错误:

Exception in thread "main" org.neo4j.graphdb.TransactionFailureException: Transaction was marked as successful, but unable to commit transaction so rolled back.
    at org.neo4j.kernel.impl.coreapi.TopLevelTransaction.close(TopLevelTransaction.java:100)

引起
Caused by: org.neo4j.kernel.impl.store.UnderlyingStorageException: java.lang.IllegalArgumentException: An SPI class of type org.apache.lucene.codecs.PostingsFormat with name 'BlockTreeOrds' does not exist.  
You need to add the corresponding JAR file supporting this SPI to your classpath.  The current classpath supports the following names: [Lucene50]

我知道在我使用的neo4j和Lucene maven依赖项中存在某种不兼容问题。但是,我不知道如何修复它们。

当我浏览外部库时,我可以看到org.apache.lucene:lucene-core:5.5.1有一个名为META-INF/services/org.apache.lucene.codecs.PostingFormat的文件,其中只包含一行:org.apache.lucene.codecs.lucene50.Lucene50PostingsFormat 另一方面,org.apache.lucene:lucene-codecs:5.5.0中包含的同一文件包含以下内容:

org.apache.lucene.codecs.blocktreeords.BlockTreeOrdsPostingsFormat
org.apache.lucene.codecs.bloom.BloomFilteringPostingsFormat
org.apache.lucene.codecs.memory.DirectPostingsFormat
org.apache.lucene.codecs.memory.FSTOrdPostingsFormat
org.apache.lucene.codecs.memory.FSTPostingsFormat
org.apache.lucene.codecs.memory.MemoryPostingsFormat
org.apache.lucene.codecs.simpletext.SimpleTextPostingsFormat
org.apache.lucene.codecs.autoprefix.AutoPrefixPostingsFormat

以下是我用于项目的pom.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
<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>me.ahmedov</groupId>
<artifactId>neo4jtest</artifactId>
<version>1.0</version>
<properties>
    <version-package>cypher.internal.docs</version-package>
    <docs-plugin.skip>false</docs-plugin.skip>
    <attach-docs-phase>verify</attach-docs-phase>
    <remote-csv-upload>http://neo4j.com/docs/${project.version}/csv</remote-csv-upload>
    <scala.version>2.11.8</scala.version>
    <scala.binary.version>2.11</scala.binary.version>
    <project.version>3.0.3</project.version>
</properties>
<dependencies>

    <!-- shared versions are defined in the parent pom -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.5</version>
    </dependency>
    <!-- scala -->

    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>${scala.version}</version>
    </dependency>

    <!-- lucene -->
    <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
        <version>5.5.1</version>
    </dependency>


    <!-- neo4j -->

    <!-- https://mvnrepository.com/artifact/org.neo4j/neo4j-kernel -->
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-kernel</artifactId>
        <version>3.1.0-M04</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.neo4j/neo4j -->
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j</artifactId>
        <version>3.1.0-M04</version>
    </dependency>

    <!-- neo4j-cypher -->

    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-cypher</artifactId>
        <version>3.0.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.neo4j/neo4j-cypher-compiler-3.1 -->
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-cypher-compiler-3.1</artifactId>
        <version>3.1.0-M04</version>
    </dependency>

    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-cypher-frontend-3.1</artifactId>
        <version>3.1.0-M04</version>
        <type>test-jar</type>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.neo4j/neo4j-io -->
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-io</artifactId>
        <version>3.1.0-M04</version>
    </dependency>

    <!-- other -->

    <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>

            </configuration>
            <executions>
                <execution>
                    <id>assemble-all</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <configuration>
                <scalaVersion>${scala.version}</scalaVersion>
                <scalaCompatVersion>${scala.binary.version}</scalaCompatVersion>
            </configuration>
        </plugin>
    </plugins>

    <pluginManagement>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <systemPropertyVariables>
                        <remote-csv-upload>${remote-csv-upload}/</remote-csv-upload>
                        <dbms.pagecache.memory.default.override>8m</dbms.pagecache.memory.default.override>
                    </systemPropertyVariables>
                </configuration>
            </plugin>

        </plugins>
    </pluginManagement>
</build>

<profiles>
    <profile>
        <id>neo-docs-build</id>
        <activation>
            <activeByDefault>false</activeByDefault>
            <property>
                <name>docsBuild</name>
            </property>
        </activation>
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <includes>
                                <include>**/DocTest*.java</include>
                                <include>**/*DocTest.java</include>
                                <include>**/docgen/**</include>
                            </includes>
                            <systemPropertyVariables>
                                <dbms.pagecache.memory.default.override>8m</dbms.pagecache.memory.default.override>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </profile>
</profiles>

基本上,它归结为这个问题:如何确保在最终的汇编所有jar文件中选择了正确的PostingFormat文件? 我试图改变lucene版本等没有运气。

0 个答案:

没有答案