mankand007
mankand007

Reputation: 992

KeyError using happybase because column families are returned as byte strings

I'm trying to use happybase to access my Hbase table.

I'm using the python3 branch from the happybase github repo.

Here's my test code:

import happybase
from settings import HBASE_THRIFT_SERVER

connection = happybase.Connection(HBASE_THRIFT_SERVER)
print(connection.tables())
table = connection.table('people')


def get_audience(audience_qual,start,end):
    row = table.row(audience_qual)
    print(row)
    return row['data:record']


def get_all_audience():
    for key, data in table.scan():
        print(key, data)


def put_people_row(row_key, people):
    table.put(row_key, {'data:record': people})

if __name__ == '__main__':
    get_all_audience()
    print(get_audience('daily201605180000004', 0, 0))

When I execute this I'm getting Key Error on the row because the column family is returned as a byte string.

2016-05-18 21:04:10,843 DS-Client-Dashboard INFO     Settings Loaded
[b'ambarismoketest', b'people', b'tsdb', b'tsdb-meta', b'tsdb-tree', b'tsdb-uid']
b'daily201604030000001' {b'data:': b'{"clientid":1, "customerid":23499382}'}
b'daily201605180000002' {b'data:record': b'{"clientid": 2, "customerid": 2383020475}'}
b'daily201605180000003' {b'data:record': b'{"clientid": 2, "customerid": 2383020475}'}
b'daily201605180000004' {b'data:record': b'{"clientid": 2, "customerid": 2383020475}'}
{b'data:record': b'{"clientid": 2, "customerid": 2383020475}'}
Traceback (most recent call last):
  File "/home/user100/ds_one_api/app/V1/hbase_operations.py", line 25, in <module>
    print(get_audience('daily201605180000004', 0, 0))
  File "/home/user100/ds_one_api/app/V1/hbase_operations.py", line 12, in get_audience
    return row['data:record']
KeyError: 'data:record'

Process finished with exit code 1

Happybase documentation does not show any conversion to get column families working. However, it does mention that HBase stores data as byte strings which happybase cannot handle.

How do I return a column family from a row without Key Error?

Upvotes: 0

Views: 770

Answers (1)

mankand007
mankand007

Reputation: 992

    return row[b'data:record']

Works fine.

Upvotes: 1

Related Questions