0

我不知道爲什麼,但counter_inc方法不工作,沒有模擬器,指向一個開發真實的實例。Bigtable Google Happybase Python KeyError試圖做`counter_inc`

段:

from google.cloud import bigtable 
from google.cloud import happybase 

client = bigtable.Client(project='robbie-ai', admin=True) 
instance = client.instance('visio-bt-staging') 
connection = happybase.Connection(instance=instance) 
connection.create_table('commons_TestBTModelsTable', {'family': None, 'counters': None}) 
table = connection.table('commons_TestBTModelsTable') 
table.put('row-key1', {'family:surname': 'Trump'}) 
print("Getting row 'row-key1': {}".format(table.row(b'row-key1'))) 
table.counter_inc(b'row1', b'counters:qual1') 

如果我做table.counter_inc(b'row1', 'counters:qual1')是完全一樣的。

當作爲腳本執行:

[email protected]:/app# python scripts/counters.py 
Getting row 'row-key1': {b'family:surname': b'Trump'} 
Traceback (most recent call last): 
    File "scripts/counters.py", line 28, in <module> 
    table.counter_inc(b'row1', b'counters:qual1') 
    File "/usr/local/lib/python3.5/dist-packages/google/cloud/happybase/table.py", line 591, in counter_inc 
    column_cells = modified_cells[column_family_id][column_qualifier] 
KeyError: 'qual1' 

它是一個bug或者這個片段有問題?

回答

0

明確的錯誤,他們有不同的類型列ID限定符和返回的字典密鑰類型,它是由commit()返回一個,當你從拆分得到了一個被解碼成海峽,

這裏:https://github.com/GoogleCloudPlatform/google-cloud-python-happybase/blob/master/src/google/cloud/happybase/table.py#L577

這裏https://github.com/GoogleCloudPlatform/google-cloud-python-happybase/blob/master/src/google/cloud/happybase/table.py#L582

這裏https://github.com/GoogleCloudPlatform/google-cloud-python-happybase/blob/master/src/google/cloud/happybase/table.py#L591

如果你去低級別:

# As it is in the source code 
row = table._low_level_table.row(b'row-key1', append=True) 
column_family_id, column_qualifier = 'qual1'.split(':') 
row.increment_cell_value(column_family_id, column_qualifier, 1) 
row.commit() 
"""outputs 
{'counters': {b'qual1': [(b'\x00\x00\x00\x00\x00\x00\x00\x01', 
datetime.datetime(2017, 6, 12, 14, 2, 28, 858000, tzinfo=<UTC>))]}} 
""" 
# of course if using the original portion code it breaks with KeyError as the returned key type is bytes 
# Get the cells in the modified column, 
column_cells = modified_cells[column_family_id][column_qualifier] 
"""outputs 
KeyError         Traceback (most recent call last) 
<ipython-input-24-fe55fb8c47c5> in <module>() 
     1 # Get the cells in the modified column, 
----> 2 column_cells = modified_cells[column_family_id][column_qualifier] 

KeyError: 'counters' 
""" 
# But it works if re-encoded the column 
# Get the cells in the modified column, 
column_cells = modified_cells[column_family_id][column_qualifier.encode()] 
column_cells 
[(b'\x00\x00\x00\x00\x00\x00\x00\x04', 
    datetime.datetime(2017, 6, 12, 14, 16, 18, 150000, tzinfo=<UTC>))] 

這是針對Python 3.5.2的,無論如何奇怪的是commit的響應是返回異類的鍵類型,這對我來說似乎是錯誤的。

解決方法,直到他們修復它在PyPI中:與上述編碼重寫counter_inc方法