su-*_*su- 3 java cassandra astyanax
我知道我们可以用这样的东西向Cassandra插入单个记录(下面的例子取自这里):
final String INSERT_STATEMENT = "INSERT INTO employees (empID, deptID, first_name, last_name) VALUES (?, ?, ?, ?);";
result = keyspace
.prepareQuery(CQL3_CF)
.withCql(INSERT_STATEMENT)
.asPreparedStatement()
.withIntegerValue(222)
.withIntegerValue(333)
.withStringValue("Eric")
.withStringValue("Cartman")
.execute();
Run Code Online (Sandbox Code Playgroud)
是否可以使用Astyanax的cql3 API(如JDBC的executeBatch)进行批量插入(用于多个记录)?
注意:使用Astyanax的MutationBatch(基于Thrift,而不是CQL)对我来说似乎不是一个选择,因为我遇到了与此问题相同的问题.
使用Astyanax实现以下目标:
用cqlsh:
cqlsh:TEST_KS> INSERT INTO "MESSAGE_CF" (KEY, "DELETED_RECEIVER", "DELETED_SENDER", "SENDER")
VALUES ('user-1241324', 'Yes', 'No', 'user@mail.com');
cqlsh:TEST_KS> SELECT * FROM "MESSAGE_CF";
key | DELETED_RECEIVER | DELETED_SENDER | RECEIVER | SENDER
--------------+------------------+----------------+----------+---------------
user-1241324 | Yes | No | null | user@mail.com
Run Code Online (Sandbox Code Playgroud)
与Astyanax:
Keyspace keyspace = Astyanax.getKeyspaceContext();
ColumnFamily<String, String> mail = new ColumnFamily<String, String>(
keyspace.getKeyspaceName(), // CF Name
StringSerializer.get(), // Key Serializer
StringSerializer.get()); // Column Serializer
// You could start looping here to alter what data is being inserted
// or make the method take in parameters and call it multiple times.
String cqlStatement =
"INSERT INTO MESSAGE_CF (KEY, DELETED_RECEIVER, DELETED_SENDER, SENDER) "
+ "VALUES ('user-1281324', 'Yes', 'No', 'user@mail.com');";
// execute the insertion
OperationResult<CqlResult<String, String>> result =
keyspace.prepareQuery(mail).withCql(cqlStatement).execute();
// stop looping
Run Code Online (Sandbox Code Playgroud)
作为一个注释:我无法用准备好的语句来实现这一点,Astyanax在他们的wiki中(在准备好的CQL下)显示支持预备语句,但我使用的是astyanax-1.56.21并且asPreparedStatement()缺少该功能.
此外,为此工作不要忘记设置您的AstyanaxContext使用CQL3.
new com.netflix.astyanax.impl.AstyanaxConfigurationImpl()
.setCqlVersion("3.0.0")) //using CQL3
Run Code Online (Sandbox Code Playgroud)
UPDATE
查看批处理关键字.批量加速能力的主要因素是它可以节省往返次数.管理CQL语句会更难,但它确实提高了更新速度.它可以执行CUD操作(插入,更新和删除),但它不能执行SELECT.另外,我建议您阅读CQL3文档,了解cql可以做什么.
| 归档时间: |
|
| 查看次数: |
2504 次 |
| 最近记录: |