使用 DataStax 驱动程序连接到 "old" 列族时批量更新失败

Batch update fails when connecting to an "old" column family with DataStax driver

我有一个在早期 Cassandra 版本中创建和定义的 Cassandra 进程。到目前为止,我们使用 hector 驱动程序连接到它。我正在将驱动程序更改为 DataStax 以享受 CQL 新功能并允许异步访问。

我在做那个过渡的过程中遇到了一些问题。我读过 this upgrade guide 虽然我仍然遇到一些问题,但它提供了一些启示。

最大的一个是我无法访问协议版本大于一个的密钥空间。当我尝试以下 python 代码时:

cass4 = Cluster(['MyIp'])
cass4.protocol_version = 2
session = cass4.connect('myKeySpace')

此代码产生以下错误和警告:

ERROR:cassandra.connection:Closing connection <AsyncoreConnection(4849045328) IP:9042> due to protocol error: code=000a [Protocol error] message="Invalid or unsupported protocol version: 2"
WARNING:cassandra.cluster:Downgrading core protocol version from 2 to 1 for IP
WARNING:cassandra.metadata:Building table metadata with no column meta for keyspace

使用 Java 驱动程序,如果我尝试连接大于 1 的协议版本,我只会收到 NoHostAvailableException: All host(s) tried for query failed 连接错误。

这个连接问题让我在构建合适的 Java DAO 时遇到了很多麻烦。例如,如果我尝试进行批量更新,例如:

BatchStatment batch = new BatchStatement()
batch.add(somePreparedStatement)
cqlSession.executeAsync(batch)

我收到以下错误:

com.datastax.driver.core.exceptions.UnsupportedFeatureException: Unsupported feature with the native protocol version 1 (which is currently in use): Protocol level batching is not supported

运行 使用 cqlsh 直接在集群节点上执行 "BEGIN BATCH.." 操作,所以我知道可以执行此 CQL 命令,但我不知道如何在 Java 中准备它并使用协议版本 1 执行它。此外,集群的 cassandra 和 CQL 版本似乎合适:

[cqlsh 3.1.7 | Cassandra 1.2.13.2 | CQL spec 3.0.0 | Thrift protocol 19.36.2]

所以,问题是:

  1. 为什么会这样?
  2. 我可以使用大于 1 的协议版本连接到该密钥空间吗?
  3. 如果没有,我能以某种方式绕过这个批量更新问题吗?

终于找到了这个问题的答案here:

Can I combine Batches and PreparedStatements?

Starting with Cassandra 2.0 and the corresponding versions of the C#, Java, and Python drivers, PreparedStatements can be used in batch operations (nb before that you could still prepare a complete batch operation, but you’d need to know apriori the number of statements that will be included).

由于我的Cassandra版本是1.2xx,所以不能使用批量更新和prepared statements。

解决方法是将查询创建为字符串(是的,这很脏)然后执行字符串查询。