从Java应用程序获取数据到Cassandra 2的最快方法是什么?

时间:2021-08-12 16:49:34

I have tried the DataStax Java driver and it seems the fastest way to insert data is to compose a CQL string with all parameters inline.

我已经尝试过DataStax Java驱动程序,看起来插入数据的最快方法是编写一个内联所有参数的CQL字符串。

This loop takes 2500ms or so on my test cluster:

这个循环在我的测试集群上需要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));

The same loop with the parameters inline is about 1300ms. It gets worse if there are many parameters. I know I can use batching to insert all the rows at once but thats not the purpose of this test. I also tried using session.execute(cql, params) and it is faster but still doesn't match inline values.

参数内联的相同循环大约是1300ms。如果有很多参数会变得更糟。我知道我可以使用批处理一次插入所有行但这不是此测试的目的。我也尝试使用session.execute(cql,params),它更快但仍然不匹配内联值。

Composing CQL strings is certainly convenient and simple but is there a much faster way?

编写CQL字符串当然既方便又简单,但有更快的方法吗?

1 个解决方案

#1


5  

You can do two things to improve performance. First is to use the executeAsynch function in the driver instead of execute.

您可以做两件事来提高性能。首先是在驱动程序中使用executeAsynch函数而不是execute。

Second thing is to use a batch statement instead of a loop (I know you mentioned it's not the purpose of the test, but when it comes to inserts with a loop, batching is what you want).

第二件事是使用批处理语句而不是循环(我知道你提到它不是测试的目的,但是当涉及带循环的插入时,批处理就是你想要的)。

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);

#1


5  

You can do two things to improve performance. First is to use the executeAsynch function in the driver instead of execute.

您可以做两件事来提高性能。首先是在驱动程序中使用executeAsynch函数而不是execute。

Second thing is to use a batch statement instead of a loop (I know you mentioned it's not the purpose of the test, but when it comes to inserts with a loop, batching is what you want).

第二件事是使用批处理语句而不是循环(我知道你提到它不是测试的目的,但是当涉及带循环的插入时,批处理就是你想要的)。

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);