【问题标题】:cassandra not set default value for new column added later in python modelcassandra没有为稍后在python模型中添加的新列设置默认值
【发布时间】:2017-05-12 03:26:27
【问题描述】:

我有如下代码。

from uuid import uuid4
from uuid import uuid1

from cassandra.cqlengine import columns, connection
from cassandra.cqlengine.models import Model
from cassandra.cqlengine.management import sync_table


class BaseModel(Model):
    __abstract__ = True

    id = columns.UUID(primary_key=True, default=uuid4)
    created_timestamp = columns.TimeUUID(primary_key=True,
                                         clustering_order='DESC',
                                         default=uuid1)
    deleted = columns.Boolean(required=True, default=False)

class OtherModel(BaseModel):
    __table_name__ = 'other_table'



if __name__ == '__main__':
    connection.setup(hosts=['localhost'],
                     default_keyspace='test')
    sync_table(OtherModel)

    OtherModel.create()

第一次执行后,运行查询时可以看到db中的记录。

cqlsh> select * from test.other_table;

 id                                   | created_timestamp                    | deleted
--------------------------------------+--------------------------------------+---------
 febc7789-5806-44d8-bbd5-45321676def9 | 840e1b66-cc73-11e6-a66c-38c986054a88 |   False

(1 rows)

在此之后,我在OtherModel 中添加了新列name 并运行相同的程序。

class OtherModel(BaseModel):
    __table_name__ = 'other_table'
    name = columns.Text(required=True, default='')




if __name__ == '__main__':
    connection.setup(hosts=['localhost'],
                     default_keyspace='test')
    sync_table(OtherModel)

    OtherModel.create(name='test')

当检查数据库条目时

cqlsh> select * from test.other_table;

 id                                   | created_timestamp                    | deleted | name
--------------------------------------+--------------------------------------+---------+------
 936cfd6c-44a4-43d3-a3c0-fdd493144f4b | 4d7fd78c-cc74-11e6-bb49-38c986054a88 |   False | test
 febc7789-5806-44d8-bbd5-45321676def9 | 840e1b66-cc73-11e6-a66c-38c986054a88 |   False | null

(2 rows)

有一行namenull

但我无法查询null 的值。

cqlsh> select * from test.other_table where name=null;
InvalidRequest: code=2200 [Invalid query] message="Unsupported null value for indexed column name"

我得到了参考How Can I Search for Records That Have A Null/Empty Field Using CQL?

当我在模型中设置default=''时,为什么没有为表中的所有null值设置?

有没有办法通过查询将name 中的null 值设置为默认值''

【问题讨论】:

    标签: python cassandra null cqlsh


    【解决方案1】:

    空单元格实际上只是没有设置。并且没有数据不是您可以查询的,因为它是一种过滤操作。它不可扩展或无法高效执行,因此 C* 不会鼓励(或者在这种情况下甚至允许)。

    返回并追溯设置所有先前创建的行的值将非常昂贵(必须读取所有内容,然后执行尽可能多的写入)。不过在应用程序方面很容易说if name is null its ''

    【讨论】:

    • 谢谢克里斯,我的问题是,当我使用name=XYZ 进行查询时,我还想获取null 的数据,有什么方法可以获取数据吗?
    • 对于像这样的临时事情考虑使用 Spark。如果没有它,甚至无法使用此模式查询 name=XYZ,除非您创建新表、物化视图或二级索引。请注意,MV,尤其是 2is 具有负面影响,我建议您仅在完全了解它们的工作原理后才使用它们,以确保您不会被烧毁。我会在这里推荐一张新桌子。 Cassandra 专为大规模分布的大型数据集而设计,因此不支持查询任何数据块之类的事情,因为它在许多情况下都不起作用。架构需要支持查询。
    猜你喜欢
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 2017-11-12
    • 2021-06-07
    • 1970-01-01
    • 2018-04-13
    相关资源
    最近更新 更多