我整夜都在搜索和阅读相关内容,但是我找不到任何可以运行的示例。
public class TestNeo4j {
public enum RelTypes implements RelationshipType {
HASFOLLOW
}
private static final String MATRIX_DB = "target/matrix-db";
private GraphDatabaseService graphDb;
private long matrixNodeId;
public static void main(String[] args) throws IOException {
TestNeo4j matrix = new TestNeo4j();
matrix.setUp();
matrix.shutdown();
}
public void setUp() throws IOException {
deleteRecursively(new File(MATRIX_DB));
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(MATRIX_DB);
registerShutdownHook();
createNodespace();
}
public void shutdown() {
graphDb.shutdown();
}
public void createNodespace() throws IOException {
try (Transaction tx = graphDb.beginTx()) {
FileReader fr = new FileReader("twitter.txt");
BufferedReader br = new BufferedReader(fr);
String line = "";
while ((line = br.readLine()) != null) {
//read data like "1 1"
// "2 3"
String s = line;
String[] sa = s.split(" ");
//build relation twitter and his follow each line
//In my program, for every line, it will create a new
//For example, I will get 1-2, 1-3, 1-4, but I want it to be
// / 4
// 1 - 3
// \ 2
//!!!!i want to use unique node how to change it!!!!
Node node1 = graphDb.createNode();
Node node2 = graphDb.createNode();
node1.setProperty("id", sa[0]);
node2.setProperty("id", sa[1]);
node1.createRelationshipTo(node2, RelTypes.HASFOLLOW);
}
tx.success(); }
}
您能帮我实施吗?此外,我运行JavaQuery.java 。有关查询的示例始终会得到一个空的迭代。
您确定twitter.txt在类路径上并且没有得到FileNotFoundException
吗?您可能要先检查一下。如果FileReader
找不到您的文件,则不会创建任何节点和关系。
尽管我不确定您的示例为何行不通,但我确实有一些建议。首先,我将摆脱“ id”属性,因为它非常令人困惑。每个节点都已经有一个ID,因此您想将属性名称更改为其他名称。其次重要的是,因为你清楚地使用Java 7中,你可以使用的AutoClosable
接口FileReader
,并BufferedReader
为好。
当我运行JavaQuery类时,确实得到了预期的结果。您遇到不同的行为再次让您感到非常奇怪。您为什么不尝试所有这些中最简单的示例?查看您从以下脚本获得的输出:
public class TestNeo4j {
public static void main(String[] args) throws Exception {
final GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase("db");
final Transaction tx = graphDb.beginTx();
final Node node = graphDb.createNode();
node.setProperty("test", "test");
tx.success();
tx.close();
final ExecutionEngine engine = new ExecutionEngine(graphDb);
System.out.println(engine.execute("START n=node(*) RETURN id(n), n.test").dumpToString());
graphDb.shutdown();
}
}
您的输出应类似于:
+----------------+
| id(n) | n.test |
+----------------+
| 0 | "test" |
+----------------+
1 row
让我知道你的结果。
编辑:
如果要创建唯一的节点(或路径),请使用Cyphers MERGE命令。它可以创建唯一的节点和/或完整的路径。一个例子:
MERGE (tweep1:Tweep{user: "twitter_account"})
MERGE (tweep1)-[:FOLLOWS]->(tweep2:Tweep{user: "another_account"})
如果已经有匹配的节点,它将创建或使用节点“ tweep1”。该节点将用于创建与节点“ tweep2”的关系。这是否或多或少地回答了您的问题?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句