Bigtable Google Happybase Python KeyError when trying to do `counter_inc`
Bigtable Google Happybase Python KeyError when trying to do `counter_inc`
我不知道为什么,但是 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')
完全一样
作为脚本执行时:
root@2d21638ea17f:/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还是这个片段有问题?
绝对是一个错误,它们对列 id 限定符和返回的 dict 键类型有不同的类型,它由 commit()
返回的类型,而你从 split 得到的类型被解码为 str,
如果你去下层:
# 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
方法
我不知道为什么,但是 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')
完全一样
作为脚本执行时:
root@2d21638ea17f:/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还是这个片段有问题?
绝对是一个错误,它们对列 id 限定符和返回的 dict 键类型有不同的类型,它由 commit()
返回的类型,而你从 split 得到的类型被解码为 str,
如果你去下层:
# 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
方法