我正在尝试将 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");
}
}