参考脚本之家
首先,安装thrift
thrift链接:https://github.com/SparksFly8/Tools
下载thrift,这里用的是thrift-0.7.0-dev.tar.gz 这个版本
tar xzf thrift-0.7.0-dev.tar.gz
cd thrift-0.7.0-dev
sudo ./configure --with-cpp=no --with-ruby=no
sudo make
sudo make instal
src/main/resources/org/apache/hadoop/hbase/thrift/然后,到HBase的源码包里,找到
执行
thrift --gen py Hbase.thrift
mv gen-py/hbase/ /usr/lib/python3.6.2/site-packages/ (根据python版本可能有不同)
# coding:utf-8
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
import hbase.ttypes
import csv
def client_conn():
transport = TSocket.TSocket('hostname,like:localhost', port)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)
transport.open()
return client
if __name__ == "__main__":
client = client_conn()
result = client.getRow("table name","row name")
data_simple =[]
for k, v in result[0].columns.items(): #.keys()
data_simple.append((v.timestamp, v.value))
writer.writerows(data)
csvfile.close()
csvfile_simple = open("data_xy_simple.csv", "wb")
writer_simple = csv.writer(csvfile_simple)
writer_simple.writerow(["timestamp", "value"])
writer_simple.writerows(data_simple)
csvfile_simple.close()
print(finished)