如何用java实现Neo4j中的unique节点
我整晚都在搜索和阅读相关的东西,但我找不到任何可以运行的例子如何用java实现Neo4j中的unique节点,java,neo4j,unique,nodes,Java,Neo4j,Unique,Nodes,我整晚都在搜索和阅读相关的东西,但我找不到任何可以运行的例子 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 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(); }
}
你能帮我实施吗?
此外,我运行总是得到一个空的迭代 你确定twitter.txt在你的类路径上并且没有得到
FileNotFoundException
?你可能想先检查一下。如果FileReader
找不到您的文件,则不会创建任何节点和关系
虽然我不确定为什么你的例子不起作用,但我有一些建议。首先,我要去掉“id”属性,因为它非常混乱。每个节点都已经有一个id,因此您希望将属性名称更改为其他名称。其次,由于您显然在使用java 7,因此还可以使用FileReader
和BufferedReader
的AutoClosable
接口
当我运行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”的关系。这或多或少回答了您的问题吗?我通过构建hashmap处理了唯一节点问题,认为这是一种原始的方法
public void createNodespace() throws IOException {
try (Transaction tx = graphDb.beginTx()) {
FileReader fr = new FileReader("twitter.txt");
BufferedReader br = new BufferedReader(fr);
String line = "";
Map<String, Node> check = new HashMap<String, Node>();
while ((line = br.readLine()) != null) {
String s = line;
String[] sa = s.split(" ");
if (!check.containsKey(sa[0])) {
Node node1 = graphDb.createNode();
node1.setProperty("id", sa[0]);
check.put(sa[0], node1);
}
if (!check.containsKey(sa[1])) {
Node node2 = graphDb.createNode();
node2.setProperty("id", sa[1]);
check.put(sa[1], node2);
}
check.get(sa[0]).createRelationshipTo(check.get(sa[1]), RelTypes.HASFOLLOW);
}
tx.success();
}
}
public void createNodespace()引发IOException{
try(事务tx=graphDb.beginTx()){
FileReader fr=新的FileReader(“twitter.txt”);
BufferedReader br=新的BufferedReader(fr);
字符串行=”;
映射检查=新建HashMap();
而((line=br.readLine())!=null){
字符串s=行;
字符串[]sa=s.split(“”);
如果(!check.containsKey(sa[0])){
Node node1=graphDb.createNode();
node1.setProperty(“id”,sa[0]);
检查。放置(sa[0],节点1);
}
如果(!check.containsKey(sa[1])){
Node node2=graphDb.createNode();
node2.setProperty(“id”,sa[1]);
检查。放置(sa[1],节点2);
}
check.get(sa[0]).createRelationshipTo(check.get(sa[1]),RelTypes.HASFOLLOW);
}
成功();
}
}
实际上,有两个不同的问题。第一个是如何创建唯一节点。在上面的代码中,我阅读了twitter和他的追随者,他们都是id,比如“12312 334234”。我想把他们保存到node,你知道每个twitter都有很多追随者。I新节点,如Node node1=graphDb.createNode();node2.setProperty(“id”,sa[1]);node1.createRelationshipTo(node2,RelTypes.HASFOLLOW);在neo4j中,我得到1->2,1->3,2 1节点,但我只得到一个1节点,它与2和3连接。我发现unique node可以处理它,但我不知道如何实现它。在我的第二个问题,即查询问题中,我复制了原始文件,没有任何更改,它没有运行错误,但我不能,查询结果为空。您的示例确实有效,但我发布的示例仍然无效。谢谢