如何在cassandra中多行插入

如何在cassandra中多行插入

本文介绍了如何在cassandra中多行插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是在cassandra列族中插入多行的最有效的方法。这是可能在一个单一的调用。

What is the most efficient way of inserting multiple rows in cassandra column family. Is it possible to do this in a single call.

现在我的方法是添加插入多个列,然后执行。在一个单独的调用,我坚持一行。我正在寻找策略,以便我可以做一个批插入。

Right now my approach is to addinsert multiple column and then execute. There in a single call I am persisting one row. I am looking for strategy so that I can do a batch insert.

推荐答案

CQL配备了一个BEGIN BATCH ... APPLY BATCH语句,使得可以将多个插入组合在一起,使得开发者可以构建这样的批量请求的字符串并执行它。
[

CQL comes with a BEGIN BATCH...APPLY BATCH statement that makes it possible to group together several inserts, so that a developer can build a string of such a batch request and execute it.[http://www.datastax.com/dev/blog/client-side-improvements-in-cassandra-2-0]

以下适用于我:

PreparedStatement ps = session.prepare(
"BEGIN BATCH" +
"INSERT INTO messages (user_id, msg_id, title, body) VALUES (?, ?, ?, ?);" +
"INSERT INTO messages (user_id, msg_id, title, body) VALUES (?, ?, ?, ?);" +
"INSERT INTO messages (user_id, msg_id, title, body) VALUES (?, ?, ?, ?);" +
"APPLY BATCH" );

session.execute(ps.bind(uid, mid1, title1, body1, uid, mid2, title2, body2, uid, mid3, title3, body3));

如果您事先不知道要执行的语句,可以使用以下语法(Scala):

If you don't know in advance what statements you want to execute, you can use the following syntax (Scala):

var statement: PreparedStatement = session.prepare("INSERT INTO people (name,age) VALUES (?,?)")
var boundStatement = new BoundStatement(statement)
val batchStmt = new BatchStatement()
batchStmt.add(boundStatement.bind("User A", "10"))
batchStmt.add(boundStatement.bind("User B", "12"))
session.execute(batchStmt)

注意:BatchStatement可以容纳65536条语句。学习这个硬的方式。 : - )

Note: BatchStatement can hold upto 65536 statement. Learnt this the hard way. :-)

这篇关于如何在cassandra中多行插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 22:00