在pycassa中找不到Cassandra列族

3 投票
2 回答
1809 浏览
提问于 2025-04-17 21:21

我正在尝试连接到Cassandra数据库,以便进行批量插入。但是,当我尝试连接时,出现了一个错误。

我使用的代码是:

from pycassa import columnfamily
from pycassa import pool

cassandra_ips = ['<an ip addr>']
conpool = pool.ConnectionPool('my_keyspace', cassandra_ips)

colfam = columnfamily.ColumnFamily(conpool, 'my_table')

但是在最后一行出错了,错误信息是:

pycassa.cassandra.ttypes.NotFoundException: NotFoundException(_message=None, why='Column family my_table not found.')

这个列族肯定是存在的:

cqlsh> use my_keyspace
   ... ;
cqlsh:my_keyspace> desc tables;

my_table

cqlsh:my_keyspace>

我觉得这不是表名的简单拼写错误,因为我检查了十几次,而且还有这个原因:

In [3]: sys_mgr = pycassa.system_manager.SystemManager(cassandra_ips[0])

In [4]: sys_mgr.get_keyspace_column_families('my_keyspace')
Out[4]: {}

为什么会是{}呢?

如果这有关系的话:

  • 这个表/列族是用CQL创建的。
  • 这个表现在是空的。
  • 这个表大致是这样创建的:

    CREATE TABLE my_table (
      user_id int,
      year_month int,
      t timestamp,
      <tons of other attributes>
      PRIMARY KEY ((user_id, year_month), t)
    ) WITH compaction =
        { 'class' : 'LeveledCompactionStrategy', 'sstable_size_in_mb' : 160 };
    

2 个回答

0

这种情况可能发生在创建了一个名字用大写字母的列族(CF)之后: https://docs.datastax.com/en/cql/3.0/cql/cql_reference/ucase-lcase_r.html

我遇到了一个奇怪的命名空间结构,里面有带引号的列族:

cqlsh:testkeyspace> DESC TABLES;

"Tabletest"  users  "PlayerLastStats"

我在使用pycassa的 system_manager.create_column_family(...) 时遇到了错误,但只有在这里有column_validation_classes参数的情况下才会出现这个错误。

pycassa.cassandra.ttypes.NotFoundException: NotFoundException(_message=None, why='Column family NewTable not found.')

在把所有表的名字改成小写后,一切看起来都正常了。

cqlsh:testkeyspace> DESC TABLES;

tabletest  newtable  users  playerlaststats
0

为了通过像pycassa这样的thrift API访问CQL3数据库,表格必须使用紧凑存储来创建。

CREATE TABLE my_table (
...
) WITH COMPACT STORAGE;

关于主键,来自文档的说明:

使用紧凑存储指令会限制你不能定义超过一个不属于复合主键的列。

目前你正在使用复合分区键,但启用紧凑存储后,我们只能使用复合分区键。所以你不需要限制为单一列,它只需要是复合键的一部分。最后还有一个参考链接

撰写回答