python连接hbase
安装HBase
HBase是一个构建在HDFS上的分布式列存储系统,主要用于海量结构化数据存储。这里,我们的目标只是为Python访问HBase提供一个基本的环境,故直接下载二进制包,采用单机安装。下载后解压,修改配置文件,然后可以直接启动HBase了。所用系统版本为ubuntu14.04。
下载
wget https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/1.2.4/hbase-1.2.4-bin.tar.gz
tar zxvf hbase-1.2.4-bin.tar.gz
配置
修改hbase-env.sh,设置JAVA_HOME。
export JAVA_HOME=/usr/lib/jvm/java-8-oracle
修改hbase-site.xml,设置存储数据的根目录。
<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:///home/mi/work/hbase/data</value>
</property>
</configuration>
启动
bin/start-hbase.sh # 启动
bin/hbase shell # 进入hbase交互shell
安装Thrift
安装好HBase之后,还需安装Thrift,因为其他语言调用HBase时,需要通过Thrift进行连接。
安装Thrift依赖
sudo apt-get install automake bison flex g++ git libboost1.55 libevent-dev libssl-dev libtool make pkg-config
- 1
PS: libboost1.55-all-dev,在我的ubuntu14.04上安装有点问题,所以装的是libboost1.55。
编译安装
下载源码,解压后进行编译安装。Thrift下载地址
tar zxf thrift-0.10.0.tar.gz
cd thrift-0.10.0/
./configure --with-cpp --with-boost --with-python --without-csharp --with-java --without-erlang --without-perl --with-php --without-php_extension --without-ruby --without-haskell --without-go
make # 编译耗时较长
sudo make install
参考文档:
https://thrift.apache.org/docs/install/debian, https://thrift.apache.org/docs/BuildingFromSource
启动HBase的Thrift服务
bin/hbase-daemon.sh start thrift
检查系统进程
~/work/hbase/hbase-1.2.4/conf$ jps
3009 ThriftServer
4184 HMaster
5932 Jps
733 Main
可以看到ThriftServer已成功启动,然后我们就可以使用多种语言,通过Thrift来访问HBase了。
Python操作HBase
下面以Python为例来演示如何访问HBase。
安装依赖包
sudo pip install thrift
sudo pip install hbase-thrift
Demo程序
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 *
transport = TSocket.TSocket('localhost', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)
transport.open()
contents = ColumnDescriptor(name='cf:', maxVersions=1)
# client.deleteTable('test')
client.createTable('test', [contents])
print client.getTableNames()
# insert data
transport.open()
row = 'row-key1'
mutations = [Mutation(column="cf:a", value="1")]
client.mutateRow('test', row, mutations)
# get one row
tableName = 'test'
rowKey = 'row-key1'
result = client.getRow(tableName, rowKey)
print result
for r in result:
print 'the row is ', r.row
print 'the values is ', r.columns.get('cf:a').value
执行结果:
['test']
[TRowResult(columns={'cf:a': TCell(timestamp=1488617173254, value='1')}, row='row-key1')]
the row is row-key1
the values is 1
python连接hbase的更多相关文章
- ambari安装集群下python连接hbase之安装thrift
简介: python连接hbase是需要通过thrift连进行连接的,ambari安装的服务中貌似没有自带安装hbase的thrift,我是看配置hbase的配置名称里面没有thrift,cdh版本的 ...
- 【hbase】使用thrift with python 访问HBase
HBase 版本: 0.98.6 thrift 版本: 0.9.0 使用 thrift client with python 连接 HBase 报错: Traceback (most recent ...
- python实现Hbase
1. 下载thrift 作用:翻译python语言为hbase语言的工具 2. 运行时先启动hbase 再启动thrift,最后在pycharm中通过happybase包连接hbase 在hbase目 ...
- Hbase理论&&hbase shell&&python操作hbase&&python通过mapreduce操作hbase
一.Hbase搭建: 二.理论知识介绍: 1Hbase介绍: Hbase是分布式.面向列的开源数据库(其实准确的说是面向列族).HDFS为Hbase提供可靠的底层数据存储服务,MapReduce为Hb ...
- 全网最详细的hive-site.xml配置文件里如何添加达到Hive与HBase的集成,即Hive通过这些参数去连接HBase(图文详解)
不多说,直接上干货! 一般,普通的情况是 全网最详细的hive-site.xml配置文件里添加<name>hive.cli.print.header</name>和<na ...
- python 操作 hbase
python 是万能的,当然也可以通过api去操作big database 的hbase了,python是通过thrift去访问操作hbase 以下是在centos7 上安装操作,前提是hbase已经 ...
- 【初学python】使用python连接mysql数据查询结果并显示
因为测试工作经常需要与后台数据库进行数据比较和统计,所以采用python编写连接数据库脚本方便测试,提高工作效率,脚本如下(python连接mysql需要引入第三方库MySQLdb,百度下载安装) # ...
- python连接mysql的驱动
对于py2.7的朋友,直接可以用MySQLdb去连接,但是MySQLdb不支持python3.x.这是需要注意的~ 那应该用什么python连接mysql的驱动呢,在stackoverflow上有人解 ...
- paip. 解决php 以及 python 连接access无效的参数量。参数不足,期待是 1”的错误
paip. 解决php 以及 python 连接access无效的参数量.参数不足,期待是 1"的错误 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源 ...
随机推荐
- Python如何引入自定义模块?
Python运行环境在查找库文件时是对 sys.path 列表进行遍历,如果我们想在运行环境中注册新的类库,主要有以下四种方法: 1.在sys.path列表中添加新的路径.这里可以在运行环境中直接修改 ...
- python【文件操作:修改文件】
- samba和nginx服务
samba和nginx服务 1.s配置amba samba的功能: samba是一个网络服务器,用于Linux和Windows之间共享文件. 1.1配置环境 关闭防火墙和selinux systemc ...
- 机器学习算法整理(二)梯度下降求解逻辑回归 python实现
逻辑回归(Logistic regression) 以下均为自己看视频做的笔记,自用,侵删! 还参考了:http://www.ai-start.com/ml2014/ 用梯度下降求解逻辑回归 Logi ...
- Linux安装python模块
# 依赖解决 # yum -y install epel-release python-devel gcc # 安装模块 # pip install psutil (若没有pip命令,可通过 yum ...
- Linux下安装PHP的lua扩展库
一.安装Lua 5.3.4 下载 http://www.lua.org/ftp/lua-.tar.gz tar xvf lua-.tar.gz cd lua- 重要:进入解压缩后的路径 cd .../ ...
- 基本控件文档-UIScrollView
CHENYILONG 基本控件文档-UIScrollView Fullscreen 基本使用作用UIScrollView可 ...
- 【leetcode 简单】 第八十题 3的幂
给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例 1: 输入: 27 输出: true 示例 2: 输入: 0 输出: false 示例 3: 输入: 9 输出: true 示例 4: 输 ...
- redis写定时任务获取root权限
前提: 1.redis由root用户启动. 2.开启cron的时候,/var/spool/cron linux机器下默认的计划任务,linux会定时去执行里面的任务. 启动服务 :/sbin/serv ...
- Python文件操作-文件的增删改查
需求:对文件进行增删改查 由于时间原因,本次代码没有增加任何注释,如有疑问,请联系编辑者:闫龙 其实我也是醉了,看着这些个代码,我脑袋也特么大了,没办法,大神说了,不让用新知识,只可以使用学过的,所以 ...