我最近遇到了starbase模块,用于将python脚本连接到hbase。面对python starbase fetch函数获取字节和字符串数据的问题
我会给你一个我正面临什么问题的简要说明,然后将它逐步分解给你一个清晰的图像。
我在我的hbase表中有一些名为'dummy_table'的数据。此表中的键都是由\ x00填充分隔的所有字符串。这里有一个例子: -
00:00:00:00:00:00\x001441767600\x001\x0040.0.2.1\x00
现在,我将解释什么是真正的字段有: -
00:00:00:00:00:00 - This is the mac address
1441767600 - This is the time in epoch (Wed Sep 9 03:00:00 UTC 2015)
1 - this is the customer id
40.0.2.1 - this is store id
因为他们都串以前它是很容易获取对应于这些键的值。
这里是代码片段: -
from starbase import Connection
import starbase
C = Connection(host='10.10.122.136', port='60010')
get_table = C.table('dummy_table')
mac = "00:00:00:00:00:00"
hours = "1441767600"
cus_id = "1"
store_id = "40.0.2.1"
create_query = '%s\x00%s\x00%s\x00%s\x00' % (mac,hours,cus_id,store_id)
fetch_result = get_table.fetch(create_query)
print fetch_result
此前经常给我确切的值,它是一个散列现在
{count:100}
这小小的一段代码,问题在于时间,这因为hbase中的数据量非常大,所以存储在hbase中的8字节格式以获得更好的性能目的。
现在在HBase的行/键看起来是这样的: -
00:00:00:00:00:00\x00\x80\x00\x00\x00U\xEF\xA0\xB0\x001\x0040.0.2.1\x00
打破下来: -
00:00:00:00:00:00 - Mac address
\x80\x00\x00\x00U\xEF\xA0\xB0 - time in bytes (1441767600 if converted into long from bytes)
1 - customer id
40.0.2.1 - store_id
现在,如果我运行类似的代码,以最小的变化,使蟒蛇取字符串作为字符串,它不起作用。 这里是代码片段: -
from starbase import Connection
import starbase
C = Connection(host='10.10.122.136', port='60010')
get_table = C.table('dummy_table')
mac = "00:00:00:00:00:00"
hours = "\\x80\\x00\\x00\\x00U\\xEF\\xA0\\xB0"
cus_id = "1"
store_id = "40.0.2.1"
create_query = '%s\x00%s\x00%s\x00%s\x00' % (mac,hours,cus_id,store_id)
fetch_result = get_table.fetch(create_query)
print fetch_result
当我运行它,它给了我“无”作为结果。
有趣的是,当我直接访问我的hbase并运行相同的get查询时,它的工作原理。 下面是相同的HBase的GET查询: -
get 'dummy_table', "00:00:00:00:00:00\x00\x80\x00\x00\x00U\xEF\xA0\xB0\x001\x0040.0.2.1\x00", COLUMN => ['cf1:count:toLong']
这使输出为: -
100
这是正确的。 我已经搜索了很多,但我没有遇到任何可以解决我的问题。
任何帮助?谢谢