我正在尝试使用此代码在neo4j demo.db文件夹中创建节点和关系。它只是创建空白的demo.db文件夹。当我在neo4j中打开这个db文件夹时,它显示零节点的关系。我提供了relations.xls文件。
n <= 0
这是执行此代码后的控制台输出....
relations.xls 西南贝尔公司华纳通信公司收购时间表:AT&amp; T与时代华纳的合并关注数十年的行业交易http://www.npr.org/sections/thetwo-way/2016/10/22/498996253/timeline-at-ts-merger-with-time-warner-follows-decades-of-industry-deals?utm_medium=RSS&utm_campaign=technology 24/10/16 Verizon公司雅虎公司收购时间表:AT&amp; T与时代华纳的合并遵循数十年的行业优惠http://www.npr.org/sections/thetwo-way/2016/10/22/498996253/timeline-at-ts-merger-with-time-warner-follows-decades-of-industry-deals?utm_medium=RSS&utm_campaign=technology 24/10/16 AOL公司时代华纳公司收购时间表:AT&amp; T与时代华纳合并,追随数十年的行业竞争http://www.npr.org/sections/thetwo-way/2016/10/22/498996253/timeline-at-ts-merger-with-time-warner-follows-decades-of-industry-deals?utm_medium=RSS&utm_campaign=technology 24/10/16 康卡斯特公司沃尔特迪斯尼公司公司获得时间表:AT&amp; T与时代华纳的合并遵循数十年的行业优惠http://www.npr.org/sections/thetwo-way/2016/10/22/498996253/timeline-at-ts-merger-with-time-warner-follows-decades-of-industry-deals?utm_medium=RSS&utm_campaign=technology 24/10/16 SBC Corporation公司西南贝尔公司公司收购时间表:AT&amp; T与时代华纳的合并关注数十年的行业交易http://www.npr.org/sections/thetwo-way/2016/10/22/498996253/timeline-at-ts-merger-with-time-warner-follows-decades-of-industry-deals?utm_medium=RSS&utm_campaign=technology 24/10/16 康卡斯特公司NBC环球公司获得时间表:AT&amp; T与时代华纳的合并关注数十年的行业交易http://www.npr.org/sections/thetwo-way/2016/10/22/498996253/timeline-at-ts-merger-with-time-warner-follows-decades-of-industry-deals?utm_medium=RSS&utm_campaign=technology 24/10/16 sss公司sdadasfd公司收购bndfhfdhedr http://www.npr.org/sections/thetwo-way/2016/10/22/498996253/timeline-at-ts-merger-with-time-warner-follows-decades-of-industry-deals?utm_medium=RSS&utm_campaign=technology 24/10/16
完成!!
答案 0 :(得分:1)
很抱歉这样说,但这段代码真的很乱!此外,我们无法重现您的结果,因为我们没有数据,而且代码远非minimal example。我们无法为您调试:隔离每一步,看看是否做了什么等等。
下面&#39;但是,有一些提示和评论。
if (result.toString().equals("empty iterator"))
真的?请使用API而不是字符串转换,这绝不是一个稳定的界面(它不是任何合同的一部分):
if (!result.hasNext())
subjecttype
和objecttype
的值是代表变量名还是标签的节点?前者没有意义(为什么查询在功能上相同时会发生变化),但后者没有得到正确使用:
result=graphDb.execute("match ("+subjecttype+"{name:\""+subject+"\"}) return "+subjecttype+".name;");
subjecttype
用作return
子句中的变量,但看起来像match
子句中的标签,除了它缺少前导冒号:
result=graphDb.execute("match (n:"+subjecttype+"{name:\""+subject+"\"}) return n.name");
(最后的分号是不必要的)
您实际上正确地使用它来匹配create
:
result=graphDb.execute("create (a:"+subjecttype+"{name:\""+subject+"\"}) return a;");
此外,您的查询容易受到&#34; Cypher注入&#34; (SQL注入的亲戚),如果subject
包含引号。改为使用查询参数:
result = graphDb.execute("match (n:" + subjecttype + " {name:{name}}) return n.name",
Collections.<String, Object>singletonMap("name", subject));
它具有使查询具有通用性的额外好处,这意味着它不会被解析,并且不会为每一行计算其执行计划(每个标签只能执行一次)。
MERGE
您只需使用MERGE
代替MATCH
+ CREATE
即可替换您的逻辑:
result = graphDb.execute("merge (n:" + subjecttype + " {name:{name}}) return n",
Collections.<String, Object>singletonMap("name", subject));
您的多个查询实际上可以简化为单个查询,但relationship
中包含的relations
上的过滤器除外:
Map<String, Object> params = new HashMap<>();
params.put("subject", subject);
params.put("object", object);
params.put("headline", headline);
params.put("newslink", newslink);
params.put("date", date);
graphDb.execute(
"MERGE (a:" + subjecttype + " {name: {subject}}) " +
"MERGE (b:" + objecttype + " {name: {object}}) " +
"MERGE (a)-[r:" + relationship + "]->(b) " +
"ON CREATE SET r.headlines = {headline}, " +
" r.newslink = {newslink}, " +
" r.date = {date}",
params);
使用过滤器,实际上有3个查询:
Map<String, Object> params = new HashMap<>();
params.put("subject", subject);
params.put("object", object);
params.put("headline", headline);
params.put("newslink", newslink);
params.put("date", date);
graphDb.execute("MERGE (a:" + subjecttype + " {name: {subject}})", params);
graphDb.execute("MERGE (b:" + objecttype + " {name: {object}})", params);
if (relations.contains(relationship)) {
graphDb.execute(
"MATCH (a:" + subjecttype + " {name: {subject}}) " +
"MATCH (b:" + objecttype + " {name: {object}}) " +
"MERGE (a)-[r:" + relationship + "]->(b) " +
"ON CREATE SET r.headlines = {headline}, " +
" r.newslink = {newslink}, " +
" r.date = {date}",
params);
}
Transaction
是AutoCloseable
,这意味着您应该使用try-with-resources而不是手动管理它。而不是
Transaction tx = graphDb.beginTx();
try {
// ...
} finally {
tx.close();
}
只是做
try (Transaction tx = graphDb.beginTx()) {
// ...
}
答案 1 :(得分:1)
实际上if条件总是返回false,这就是为什么它没有创建任何节点和关系。我只是改变了我的if条件,现在它工作正常。
试
{
Result result;
result = graphDb.execute("merge (a:" + subjecttype + "{name:\"" + subject + "\"}) return a;");
result = graphDb.execute("merge (a:" + objecttype + "{name:\"" + object + "\"}) return a;");
result = graphDb.execute("merge (a:" + subjecttype + "{name:\"" + subject + "\"}) " + "merge(b:"
+ objecttype + "{name:\"" + object + "\"}) " + "merge (a)-[r:" + relationship + "{headlines:\""
+ headline + "\",newslink:\"" + newslink + "\",date:\"" + date + "\"" + "}]->(b) return r;");
tx.success();
}
finally {
tx.close();
}