python操作 hbase 数据的方法

yipeiwu_com6年前Python基础

配置 thrift

python使用的包 thrift

个人使用的python 编译器是pycharm community edition. 在工程中设置中,找到project interpreter, 在相应的工程下,找到package,然后选择 “+” 添加, 搜索 hbase-thrift (Python client for HBase Thrift interface),然后安装包。

安装服务器端thrift。

参考官网,同时也可以在本机上安装以终端使用。

thrift Getting Started

也可以参考安装方法 python 调用HBase 范例

首先,安装thrift

下载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 install

然后,到HBase的源码包里,找到

src/main/resources/org/apache/hadoop/hbase/thrift/

执行

thrift –gen py Hbase.thrift
mv gen-py/hbase/ /usr/lib/python2.4/site-packages/ (根据python版本可能有不同)

获取数据示例 1

# 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
# from hbase.ttypes import ColumnDescriptor, Mutation, BatchMutation
from hbase.ttypes import *

import csv


def client_conn():
 # Make socket
 transport = TSocket.TSocket('hostname,like:localhost', port)
 # Buffering is critical. Raw sockets are very slow
 transport = TTransport.TBufferedTransport(transport)
 # Wrap in a protocol
 protocol = TBinaryProtocol.TBinaryProtocol(transport)
 # Create a client to use the protocol encoder
 client = Hbase.Client(protocol)
 # Connect!
 transport.open()
 return client

if __name__ == "__main__":

 client = client_conn()

 # r = client.getRowWithColumns('table name', 'row name', ['column name'])
 # print(r[0].columns.get('column name')), type((r[0].columns.get('column name')))

 result = client.getRow("table name","row name")
 data_simple =[]

 # print result[0].columns.items()

 for k, v in result[0].columns.items(): #.keys()
  #data.append((k,v))
  # print type(k),type(v),v.value,,v.timestamp
  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"

会基础的python应该知道result是个list,result[0].columns.items()是一个dict 的键值对。可以查询相关资料。或者通过输出变量,观察变量的值与类型。

说明:上面程序中 transport.open()进行链接,在执行完后,还需要断开transport.close()

目前只涉及到读数据,之后还会继续更新其他dbase操作。

以上这篇python操作 hbase 数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python实现批量获取指定文件夹下的所有文件的厂商信息

本文实例讲述了python实现批量获取指定文件夹下的所有文件的厂商信息的方法。分享给大家供大家参考。具体如下: 功能代码如下: import os, string, shutil,r...

用Python实现web端用户登录和注册功能的教程

用Python实现web端用户登录和注册功能的教程

用户管理是绝大部分Web网站都需要解决的问题。用户管理涉及到用户注册和登录。 用户注册相对简单,我们可以先通过API把用户注册这个功能实现了: _RE_MD5 = re.compil...

django fernet fields字段加密实践详解

一、fernet介绍 Fernet 用于django模型字段对称加密,使用 crytography 库。 官网帮助文档 1、先决条件 django-fernet-fields 支持D...

对django layer弹窗组件的使用详解

父层: <div class="col-xs-12"> <div class="box"> <div class="box-header...

python 多维高斯分布数据生成方式

python 多维高斯分布数据生成方式

我就废话不多说了,直接上代码吧! import numpy as np import matplotlib.pyplot as plt def gen_clusters():...