[英]What is the fastest way to import data from application (Java) into temporary table?
[英]What is the fastest way to get data into Cassandra 2 from a Java application?
我已经尝试过DataStax Java驱动程序,它似乎是插入数据的最快方法是组成一个内联所有参数的CQL字符串。
这个循环在我的测试集群上需要2500ms左右:
PreparedStatement ps = session.prepare("INSERT INTO perf_test.wibble (id, info) VALUES (?, ?)")
for (int i = 0; i < 1000; i++) session.execute(ps.bind("" + i, "aa" + i));
参数内联的相同循环大约是1300ms。 如果有很多参数会变得更糟。 我知道我可以使用批处理同时插入所有行但这不是此测试的目的。 我也尝试使用session.execute(cql, params)
,它更快但仍然不匹配内联值。
编写CQL字符串当然既方便又简单,但有更快的方法吗?
您可以做两件事来提高性能。 首先是在驱动程序中使用executeAsynch函数而不是execute
。
第二件事是使用批处理语句而不是循环(我知道你提到它不是测试的目的,但是当涉及到带循环的插入时,批处理就是你想要的)。
PreparedStatement ps = session.prepare("INSERT INTO messages (user_id, msg_id, title, body) " +
"VALUES (?, ?, ?, ?)");
BatchStatement batch = new BatchStatement();
batch.add(ps.bind(uid, mid1, title1, body1));
batch.add(ps.bind(uid, mid2, title2, body2));
batch.add(ps.bind(uid, mid3, title3, body3));
session.execute(batch);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.