我正在研究java中的neo4j,我有一个问题。当我创建节点时,如何验证数据库是否为空?
这是我创建节点的代码:
br = new BufferedReader(new FileReader(csvFile));
while (br.ready()==true) {
transaction = graphDb.beginTx();
int cont = 0;//limitador de tuplas por query
while ((line = br.readLine()) != null && cont < 10000) {
String[] dado = line.split(cvsSplitBy);
// inserir comando para criar o nós com a data
Node no = graphDb.createNode();
no.setProperty("data", dado[0]);
no.setProperty("temperatura", dado[1]);
no.setProperty("latitude", dado[2]);
no.setProperty("longitude", dado[3]);
no.setProperty("variação", dado[4]);
System.out.println(cont);
cont++;
}
transaction.success();
transaction.close();
}
答案 0 :(得分:3)
这是一个有效的Cypher查询,它返回一个布尔isEmpty
结果。
MATCH (n)
RETURN n IS NULL AS isEmpty
LIMIT 1;
答案 1 :(得分:2)
答案 2 :(得分:1)
match (n)
return 1
limit 1
我希望Neo4J会更少处理这个查询,知道它不必像先前的答案中所建议的那样费心计算或打扰过滤
答案 3 :(得分:0)
import org.neo4j.graphdb.*; // For Node and ResourceIterator
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.EnterpriseGraphDatabaseFactory;
其他方式
public GraphDatabaseService graphDb;
GraphDatabaseService graphDb = new EnterpriseGraphDatabaseFactory().newEmbeddedDatabase(dbDirectory);
以下在交易中称为
private boolean isEmpty() {
boolean isEmpty = true;
try (ResourceIterator<Node> nodesResult = graphDb.getAllNodes().iterator())
{
while (nodesResult.hasNext()) {
isEmpty = false;
break;
}
}
return isEmpty;
}
您会认为
boolean result = graphDb.getAllNodes().iterator().hasNext();
可以,但是不能。