Search code examples
pythonhbasethrifthappybase

Filter doesn't work when using happybase to scan an HBase table with Chinese character


I have a table in HBase where Chinese characters are stored in a certain column, say 'FLT:CREW_DEPT'. Now I need to filter out all the rows that 'FLT:CREW_DEPT' equals a certain value. When doing this in hbase shell, it works fine, shown as below

scan 'PAX_EXP_FACT', {COLUMNS => ['FLT:CREW_DEPT'], FILTER => "SingleColumnValueFilter ('FLT', 'CREW_DEPT', =, 'binary:\xe4\xb8\x80\xe9\x83\xa8', true, true)", LIMIT => 5}
ROW                                                  COLUMN+CELL
 CA101-20160808-PEK-001192753702                     column=FLT:CREW_DEPT, timestamp=1500346136328, value=\xE4\xB8\x80\xE9\x83\xA8
 CA101-20161103-PEK-001181988752                     column=FLT:CREW_DEPT, timestamp=1500346230204, value=\xE4\xB8\x80\xE9\x83\xA8
 CA101-20161105-PEK-000728690130                     column=FLT:CREW_DEPT, timestamp=1500346244963, value=\xE4\xB8\x80\xE9\x83\xA8
 CA101-20161201-PEK-006731936575                     column=FLT:CREW_DEPT, timestamp=1500346233640, value=\xE4\xB8\x80\xE9\x83\xA8
 CA101-20161212-PEK-001512808262                     column=FLT:CREW_DEPT, timestamp=1500346223572, value=\xE4\xB8\x80\xE9\x83\xA8
5 row(s) in 0.0060 seconds

However, when doing the similar thing in python using happybase, nothing's returned:

import happybase
import datetime
import pytz

connection = happybase.Connection('192.168.199.200', port=9090)
table = connection.table('PAX_EXP_FACT')

filter_str = ""
filter_str += "SingleColumnValueFilter('FLT', 'CREW_DEPT', =, 'binary:\xe4\xba\x8c\xe9\x83\xa8')"

results = table.scan(
    filter=filter_str
    #     ,limit=100
)

count = 0
for key, data in results:
    count += 1
    print(data[b'FLT:CREW_DEPT'].decode('utf-8'))

print('No. of flight matches:', count)

connection.close()

0 rows are returned ...

Can anyone help? Much appreciated!!!


Solution

  • The answer turned out to be very simple ... I should've used

    "SingleColumnValueFilter('FLT', 'CREW_DEPT', =, 'binary:中文')"
    

    instead of converting it to utf-8 encoded byte string first ... even in hbase shell i can do the same thing (though it's displayed in question marks)

    scan 'PAX_EXP_FACT', {COLUMNS => ['FLT:CREW_DEPT'], FILTER => "SingleColumnValueFilter ('FLT', 'CREW_DEPT', =, 'binary:??', true, true)", LIMIT => 5}
    ROW                                                  COLUMN+CELL
     CA101-20160808-PEK-001192753702                     column=FLT:CREW_DEPT, timestamp=1500353334419, value=\xE4\xB8\x80\xE9\x83\xA8
     CA101-20161103-PEK-001181988752                     column=FLT:CREW_DEPT, timestamp=1500353426641, value=\xE4\xB8\x80\xE9\x83\xA8
     CA101-20161105-PEK-000728690130                     column=FLT:CREW_DEPT, timestamp=1500353447707, value=\xE4\xB8\x80\xE9\x83\xA8
     CA101-20161201-PEK-006731936575                     column=FLT:CREW_DEPT, timestamp=1500353432222, value=\xE4\xB8\x80\xE9\x83\xA8
     CA101-20161212-PEK-001512808262                     column=FLT:CREW_DEPT, timestamp=1500353417107, value=\xE4\xB8\x80\xE9\x83\xA8
    5 row(s) in 0.0100 seconds
    

    Using substring will not work, which was what I had been trying before I raised this silly question ...