我正在尝试使用以下代码将一堆节点插入Neo4J:
import org.neo4j.driver.v1.*;
public class BoltClass
{
public static void minimalWorkingExample() throws Exception
{
Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "admin4j" ) );
Session session = driver.session();
int k=0;
for (int i = 0; i < 1000; i++) {
int count = 1000;
long begin = System.currentTimeMillis();
for (int j = 0; j < count; j ++) {
session.run("CREATE (a:Person {id:" + k + ", name:'unknown'})");
}
long end = System.currentTimeMillis();
System.out.print("Inserting " + (double)count/((double)(end-begin)/count) + " nodes per second.\n");
k++;
}
session.close();
driver.close();
}
public static void main(String[] args)
{
try {
minimalWorkingExample();
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果:
Inserting 58.8235294117647 nodes per second.
Inserting 76.92307692307692 nodes per second.
Inserting 50.0 nodes per second.
Inserting 76.92307692307692 nodes per second.
Inserting 55.55555555555556 nodes per second.
Inserting 62.5 nodes per second.
Inserting 66.66666666666667 nodes per second.
Inserting 55.55555555555556 nodes per second.
Inserting 62.5 nodes per second.
Inserting 55.55555555555556 nodes per second.
Inserting 47.61904761904762 nodes per second.
Inserting 45.45454545454545 nodes per second.
Inserting 58.8235294117647 nodes per second.
Inserting 83.33333333333333 nodes per second.
我正在使用Neo4j 3.0.3和org.neo4j.driver 1.0.4。 在插入之前,图表为空。 所使用的机器具有i5 2-2.6GHz CPU和8GB RAM。
提示:我刚刚发现了事务。
public static void TransactionExample() throws Exception
{
Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "admin4j" ) );
Session session = driver.session();
int k=0;
for (int i = 0; i < 1000; i++) {
int count = 1000;
long begin = System.currentTimeMillis();
try ( Transaction tx = session.beginTransaction() )
{
for (int j = 0; j < count; j ++) {
tx.run("CREATE (a:Person {id:" + k + ", name:'unknown'})");
}
tx.success();
}
long end = System.currentTimeMillis();
System.out.print("Inserting " + (double)count/((double)(end-begin)/count) + " nodes per second.\n");
k++;
}
session.close();
driver.close();
}
结果:
Inserting 20000.0 nodes per second.
Inserting 17857.142857142855 nodes per second.
Inserting 18867.924528301886 nodes per second.
Inserting 15384.615384615385 nodes per second.
Inserting 19607.843137254902 nodes per second.
Inserting 16666.666666666668 nodes per second.
Inserting 16393.44262295082 nodes per second.
性能提升不错,还能再进一步改善吗?
try (...)
包装事务是否意味着tx.close()
会自动调用? - Aviran Katz{param}
不再受支持。请改用$param
,例如"CREATE (a:Person {id:$id, name:$name})"。 - Klajd Deda