编写自己的代码.. Cypher 很慢。仅查看带有查询标签(“USER”)的 4 个节点的差异。full db 只有 8 个节点!
package com.mycompany.neo4j_0;
import java.util.Iterator;
import java.util.Map;
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
public class NewMain {
private static void registerShutdownHook(final GraphDatabaseService graphDb) {
// Registers a shutdown hook for the Neo4j instance so that it
// shuts down nicely when the VM exits (even if you "Ctrl-C" the
// running application).
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
graphDb.shutdown();
}
});
}
public static void PrintPropertyKeysAndValues(Node t_node) {
Map<String, Object> propertiesMap = t_node.getAllProperties();
propertiesMap.entrySet().stream().forEach((entry) -> {
System.out.printf("Key : %s and Value: %s %n", entry.getKey(), entry.getValue());
});
}
public static void GetNodesWithLabel_and_PropertyKey(GraphDatabaseService graphDB, String label, String PropertyKey) {
ResourceIterator<Node> nodes = graphDB.findNodes(DynamicLabel.label(label));
int flag = 0;
while (nodes.hasNext()) {
Node t_node = nodes.next();
Map<String, Object> propertiesMap = t_node.getAllProperties();
for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
//System.out.printf("Key : %s and Value: %s %n", entry.getKey(), entry.getValue());
if (entry.getKey().equals(PropertyKey)) {
PrintPropertyKeysAndValues(t_node);
flag = 1;
}
}
}
if (flag == 0) {
System.out.println("No Nodes found with this property");
}
}
public static void GetNodesWithLabel_and_PropertyKeyCypher(GraphDatabaseService graphDB, String label, String PropertyKey) {
String query = "MATCH (n:" + label + ") where n." + PropertyKey + " =~ '.*' RETURN n;";
Result result = graphDB.execute(query);
Iterator<Node> nodes = result.columnAs("n");
int flag = 0;
while (nodes.hasNext()) {
Node t_node = (Node) nodes.next();
Map<String, Object> propertiesMap = t_node.getAllProperties();
for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) {
//System.out.printf("Key : %s and Value: %s %n", entry.getKey(), entry.getValue());
if (entry.getKey().equals(PropertyKey)) {
PrintPropertyKeysAndValues(t_node);
flag = 1;
}
}
}
if (flag == 0) {
System.out.println("No Nodes found with this property");
}
}
public static void main(String[] args) {
// Find nodes with Specified Label and with a Specified PropertyKey.
long start_time = 0, end_time = 0, difference1 = 0, difference2 = 0;
String label="USER";
String PropertyKey="name";
GraphDatabaseService graphDB;
graphDB = new GraphDatabaseFactory().newEmbeddedDatabase("Z:\\neo4j-community-2.3.2\\data\\test\\graph.db");
registerShutdownHook(graphDB);
try (Transaction tx = graphDB.beginTx()) {
System.out.println("==================JavaApi==================");
start_time = System.nanoTime();
GetNodesWithLabel_and_PropertyKey(graphDB, label, PropertyKey);
end_time = System.nanoTime();
difference1 = end_time - start_time;
System.out.println("java time: " + difference1);
System.out.println("==================Cypher==================");
start_time = System.nanoTime();
GetNodesWithLabel_and_PropertyKeyCypher(graphDB, label, PropertyKey);
end_time = System.nanoTime();
difference2 = end_time - start_time;
System.out.println("Cypher time: " + difference2);
tx.success();
}
System.out.println("====================================");
System.out.println("difference in nanoseconds : "+(difference2-difference1));
System.out.println("Done successfully");
}
}
------------------------------------------------------------------------
构建 neo4j_0 1.0-SNAPSHOT
--- exec-maven-plugin:1.2.1:exec (default-cli) @ neo4j_0 ---
==================JavaApi==================
Key : name and Value: Steve
Key : name and Value: Linda
Key : name and Value: Michael
Key : name and Value: Rebecca
java time: 43800364
==================Cypher==================
Key : name and Value: Steve
Key : name and Value: Linda
Key : name and Value: Michael
Key : name and Value: Rebecca
Cypher time: 1583408758
====================================
difference in nanoseconds : 1539608394
Done successfully
------------------------------------------------------------------------
BUILD SUCCESS