gpt4 book ai didi

java - 不在 neo4j db 文件夹中创建任何节点和关系只是使用 java 创建空白 .db 文件夹

转载 作者:行者123 更新时间:2023-11-30 02:44:25 26 4
gpt4 key购买 nike

我正在尝试使用此代码在 neo4j demo.db 文件夹中创建节点和关系。它只是创建空白的 demo.db 文件夹。当我在 neo4j 中打开此 db 文件夹时,它显示零个节点和关系。我正在提供关系.xls 文件。

public class TestAut {


private static final File DB_PATH = new File("databases/demo.db");
private static GraphDatabaseService graphDb;

private static String [] r1={"PARTNERS_JV_WITH","EXEC_JOINS","EXEC_QUITS","INVESTS_IN_TECH_IP","ACQUIRES","LAUNCHES_NEW_PRODUCT_SERVICE","LAUNCHES",
"ACQUIRE_TALENT","DOWNSIZES_TALENT","ENTER_NEW_MARKET","DELIVERS_VALUE","OPENS_NEW_CENTER"};
private static String [] r2={"PARTNERS_JV_WITH","EXEC_JOINS","EXEC_QUITS","INVESTS_IN_TECH_IP","ACQUIRES","LAUNCHES_NEW_PRODUCT_SERVICE","LAUNCHES",
"ACQUIRE_TALENT","DOWNSIZES_TALENT","ENTER_NEW_MARKET","DELIVERS_VALUE","OPENS_NEW_CENTER"};
private static Relations relations;
public static void main(String args[]) {//throws FileNotFoundException {
String fileName = "relations.xls";


Workbook workbook;

startDb();
relations=new Relations(r1,r2);
System.out.println (fileName);
BufferedReader br;
try {
br = new BufferedReader( new InputStreamReader( new FileInputStream(fileName)));



br.close();

workbook = Workbook.getWorkbook(new File(fileName));
for(int i=0;i<workbook.getNumberOfSheets();i++) {
Sheet sheet=workbook.getSheet(i);
for(int j=0;j<sheet.getRows();j++) {
Cell cell[]=sheet.getRow(j);
for(int k=0;k<cell.length;k++)
System.out.print(cell[k].getContents()+" ");
System.out.print("\n");
createNodesAndRelationship(cell[0].getContents(),cell[1].getContents(),
cell[2].getContents(),cell[3].getContents(),
cell[4].getContents(),cell[5].getContents(),cell[6].getContents(),cell[7].getContents());
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stopDb();
System.out.println("Done!!");
successfully.... ");
}
public static void startDb() {

graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
}
public static void stopDb() {
graphDb.shutdown();

}
public static void createNodesAndRelationship(String subject,String subjecttype,String object,
String objecttype,String relationship,String headline,String newslink,String date) {

Transaction tx = graphDb.beginTx();
try

{

Result result;

result=graphDb.execute("match ("+subjecttype+"{name:\""+subject+"\"}) return "+subjecttype+".name;");

if(result.toString().equals("empty iterator")) {
//Query="create (a:"+subjecttype+"{name:\""+subject+"\"}) return a;";
result=graphDb.execute("create (a:"+subjecttype+"{name:\""+subject+"\"}) return a;");
System.out.println(result.toString());
}
//Query="match ("+objecttype+"{name:\""+object+"\"}) return "+objecttype+".name;";
result=graphDb.execute("match ("+objecttype+"{name:\""+object+"\"}) return "+objecttype+".name;");
if(result.toString().equals("empty iterator")) {

result=graphDb.execute("create (a:"+objecttype+"{name:\""+object+"\"}) return a;");
System.out.println(result.toString());
}

result=graphDb.execute("match (a:"+subjecttype+"{name:\""+subject+"\"}) "
+ "match(b:"+objecttype+"{name:\""+object+"\"}) "
+ "match (a)-[:"+relationship+"]->"
+ "(b) return a.name,b.name;");
if(result.toString().equals("empty iterator")&&relations.contains(relationship)) {

result=graphDb.execute("match (a:"+subjecttype+"{name:\""+subject+"\"}) "
+ "match(b:"+objecttype+"{name:\""+object+"\"}) "
+ "create (a)-[r:"+relationship+"{headlines:\""+
headline+"\",newslink:\""+newslink+ "\",date:\""+date+"\""+ "}]->(b) return r;");
System.out.println(result.toString());

}

tx.success();

}
finally {
tx.close();
}

}
}

这是执行此代码后的控制台输出......

关系.xls西南贝尔公司华纳通讯公司获取时间表:AT&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 2016年10月24日Verizon 公司、雅虎公司获得时间表:AT&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 2016年10月24日AOL 公司时代华纳公司收购时间表:AT&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 2016年10月24日康卡斯特公司华特迪士尼公司收购时间表:AT&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 2016年10月24日SBC Corporation Company Southwestern Bell Corporation Company 获取时间表:AT&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 2016年10月24日康卡斯特公司 NBC 环球公司收购时间表:AT&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 2016年10月24日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 2016 年 10 月 24 日

完成!!

最佳答案

实际上,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();
}

关于java - 不在 neo4j db 文件夹中创建任何节点和关系只是使用 java 创建空白 .db 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40632432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com