4 回答
TA贡献1821条经验 获得超6个赞
注意:正则的写法可能不对,保证能过滤出数据,但是可能不会严格匹配,正则问题请自己解决;
#导入thrift和habse包
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import *
#此处可以修改地址和端口
host = '192.168.1.1'
#默认端口为9090
port = 9090
#要查询的表名
table = 'table_name'
#定义一个过滤器,此为关键步骤
filter = "RowFilter(=,'regexstring:.3333.')" #此行原创:)
# Make socket
transport = TSocket.TSocket(host, port)
# Buffering is critical. Raw sockets are very slow
# 还可以用TFramedTransport,也是高效传输方式
transport = TTransport.TBufferedTransport(transport)
# Wrap in a protocol
#传输协议和传输过程是分离的,可以支持多协议
protocol = TBinaryProtocol.TBinaryProtocol(transport)
#客户端代表一个用户
client = Hbase.Client(protocol)
#打开连接
try:
transport.open()
scan.filterString=filter
scanner = client.scannerOpenWithScan(table, scan)
except Exception:
finally:
client.scannerClose(scan)
transport.close()
TA贡献1810条经验 获得超5个赞
#导入thrift和habse包
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import *
#此处可以修改地址和端口
host = '192.168.1.1'
#默认端口为9090
port = 9090
#要查询的表名
table = 'table_name'
#定义一个过滤器,此为关键步骤
filter = "RowFilter(=,'regexstring:.3333.')" #此行原创:)
# Make socket
transport = TSocket.TSocket(host, port)
# Buffering is critical. Raw sockets are very slow
# 还可以用TFramedTransport,也是高效传输方式
transport = TTransport.TBufferedTransport(transport)
# Wrap in a protocol
#传输协议和传输过程是分离的,可以支持多协议
protocol = TBinaryProtocol.TBinaryProtocol(transport)
#客户端代表一个用户
client = Hbase.Client(protocol)
#打开连接
try:
transport.open()
scan.filterString=filter
scanner = client.scannerOpenWithScan(table, scan)
except Exception:
finally:
client.scannerClose(scan)
transport.close()
连接代码网上一搜一大堆,非原创,来源已不可考,非本人研究成果;
关键就是这个:"RowFilter(=,'regexstring:.3333.')"
这个过滤器要写对,hbase有十几种内置的过滤器方法,有几种比较运算符和比较器,上面这个是正则方式,即'regexstring:.3333.';
过滤器整个双引号里面的内容会通过thrift传给hbase服务端处理,下划线这部分正则要支持java的正则要求不然会报错
TA贡献1836条经验 获得超5个赞
#导入thrift和habse包 from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol .
- 4 回答
- 0 关注
- 1398 浏览
添加回答
举报