0

我正在尝试将 csv 批量加载到 neo4j 中,并且无法将字符串数组从 csv 加载器类传递到数据库创建器类。我在返回语句的 csvloader 中都遇到了错误(方法的返回类型丢失和语法错误)。当我尝试调用该方法时,在 NodeLoader CellInput 中无法解析。我想我已经盯着这个太久了。

这是我的 .csv 阅读器:

package com.Neo4J;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CsvLoader {

public CellInput(String[] args){

    String fileName = "data.csv";
    File file = new File(fileName);
    try {
        Scanner inputStream = new Scanner(file);
        inputStream.next(); //ignore first line
        while (inputStream.hasNext()){
            String data = inputStream.next();
            String[] value = data.split(",");
            return String[] value;

        }
        inputStream.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
}
}
}

这是数据库创建者:

package com.Neo4J;

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;

public class nodeLoader {

// This is the path for Neo4J
private static final String Neo4J_DBPath = "/Program Files (x86)/neo4j-community-1.9.3";

Node one;
Node two;
Node three;
Node four;
Node five;
Node six;
Node seven;

Relationship relation;
GraphDatabaseService graphDataService;

// list of relationships first knows second
private static enum RelTypes implements RelationshipType {
    CONNECTS
}

public static void main(String[] values) {
    // TODO Auto-generated method stub
    nodeLoader nodes = new nodeLoader();
    nodes.createDatabase(values);
    nodes.removeData();
    nodes.shutDown();

}

void createDatabase(String[] values) {
    // GraphDatabaseService
    graphDataService = new GraphDatabaseFactory()
            .newEmbeddedDatabase(Neo4J_DBPath);

    // Begin Transaction
    Transaction transaction = graphDataService.beginTx();
    CsvLoader Loader= new CsvLoader();

    try {
        // Create Node & set properties
        one = graphDataService.createNode();
        one.setProperty("Source IP", Loader.CellInput[0]);
        two = graphDataService.createNode();
        two.setProperty("Target Port", Loader.CellInput[1]);
        three = graphDataService.createNode();
        three.setProperty("Protocol", Loader.CellInput[2]);
        four = graphDataService.createNode();
        four.setProperty("Reports", Loader.CellInput[3]);
        five = graphDataService.createNode();
        five.setProperty("Targets", Loader.CellInput[4]);
        six = graphDataService.createNode();
        six.setProperty("First Seen", Loader.CellInput[5]);
        seven = graphDataService.createNode();
        seven.setProperty("Last Seen", Loader.CellInput[6]);

        // Relationship
        relation = one.createRelationshipTo(two, RelTypes.CONNECTS);
        relation = two.createRelationshipTo(three, RelTypes.CONNECTS);
        relation = three.createRelationshipTo(four, RelTypes.CONNECTS);
        relation = four.createRelationshipTo(five, RelTypes.CONNECTS);
        relation = five.createRelationshipTo(six, RelTypes.CONNECTS);
        relation = six.createRelationshipTo(seven, RelTypes.CONNECTS);
        relation = seven.createRelationshipTo(one, RelTypes.CONNECTS);

        relation.setProperty("relationship-type", "CONNECTS");
        System.out.println(one.getProperty("Source IP").toString()
                + relation.getProperty("relationship-type").toString()
                + two.getProperty("Target Port").toString()
                + three.getProperty("Protocol").toString()
                + four.getProperty("Reports").toString()
                + five.getProperty("Targets").toString()
                + six.getProperty("First Seen").toString()
                + seven.getProperty("Last Seen").toString());

        // Success Transaction
        transaction.success();
    } finally {
        // finish the transaction
        transaction.finish();
    }
}

void removeData() {
    Transaction transaction = graphDataService.beginTx();
    try {
        // delete
        one.getSingleRelationship(RelTypes.CONNECTS, Direction.OUTGOING).delete();
        System.out.println("Nodes are removed");

        // delete nodes
        one.delete();
        two.delete();
        three.delete();
        four.delete();
        five.delete();
        six.delete();
        seven.delete();

        transaction.success();
    } finally {
        // finish the transaction
        transaction.finish();

    }
}

void shutDown() {
    // shut down graphDataService
    graphDataService.shutdown();
    System.out.println("Neo4J database is shut down");
}

}
4

1 回答 1

2

您遇到的错误中说明了一些但不是所有问题:

1) 该CellInput方法缺少返回类型。我从代码中假设您要返回 a String[],在这种情况下,方法签名将是:

public String[] CellInput(String[] args){

2)return相同方法中的语句格式错误。 value已经是一个字符串[],所以你要做的就是

return value;

3)你从while循环内部返回,所以你只会得到一排。这是故意的吗?

4) 您需要在 try/catch 之外的 return 语句:编译器会正确地注意到,如果 try/catch 中的任何语句抛出异常,您将无法返回。

这应该足以帮助您继续前进。

于 2013-09-30T13:23:36.413 回答