2016-12-27 72 views
1

我有如下代碼。cassandra未在python模型中爲後面添加的新列設置默認值

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() 

第一次執行後,我可以在運行query as時看到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?

當我在Model中設置default=''時,爲什麼它沒有爲表中的所有null值設置?

是否有任何方法將nullname設置爲默認值''帶查詢?

回答

2

空單元實際上只是沒有設置。由於它的過濾操作,數據的缺失並不是您可以查詢的內容。它不可擴展,也不可能有效,所以它不是C *會鼓勵的東西(或者在這種情況下甚至是允許的)。

追溯並追溯設置所有先前創建的行的值將非常昂貴(必須讀取所有內容,然後執行儘可能多的寫操作)。不過在應用方面它很簡單,只是說if name is null its ''

+0

感謝克里斯,我的問題是,當我用'name = XYZ'進行查詢時,我還想獲取'null'的數據,有沒有什麼方法可以獲取數據? – Nilesh

+0

對於喜歡這個特別的事情可以考慮使用Spark。沒有它,甚至沒有辦法用這個模式查詢name = XYZ,除非你創建一個新的表,物化視圖或二級索引。請注意,MV和特別是2is有負面影響,我建議您只有在完全瞭解它們的工作方式以確保不會被燒傷後才使用它們。 Id在這裏推薦一個新表。 Cassandra專爲大量分佈的大型數據集而設計,因此不需要查詢任何數據片段,因爲它不適用於許多場景。該模式需要支持查詢。 –

+0

謝謝克里斯,讓我檢查一下,我該如何解決這個問題:) – Nilesh

相關問題