搜索
首页后端开发Python教程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

PS: libboost1.55-all-dev,在我的ubuntu14.04上安装有点问题,所以装的是libboost1.55。

编译安装

下载源码,解压后进行编译安装。Thrift下载地址

tar zxf thrift-0.10.0.tar.gzcd 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-gomake  # 编译耗时较长sudo make install

启动HBase的Thrift服务

bin/hbase-daemon.sh start thrift

检查系统进程

~/work/hbase/hbase-1.2.4/conf$ jps3009 ThriftServer4184 HMaster5932 Jps733 Main

可以看到ThriftServer已成功启动,然后我们就可以使用多种语言,通过Thrift来访问HBase了。

Python操作HBase

下面以Python为例来演示如何访问HBase。

安装依赖包

sudo pip install thriftsudo pip install hbase-thrift

Demo程序

from thrift import Thriftfrom thrift.transport 
import TSocketfrom thrift.transport import TTransportfrom thrift.protocol 
import TBinaryProtocolfrom hbase import Hbasefrom hbase.ttypes import *

transport = TSocket.TSocket(&#39;localhost&#39;, 9090)

transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)
transport.open()

contents = ColumnDescriptor(name=&#39;cf:&#39;, maxVersions=1)# client.deleteTable(&#39;test&#39;)client.createTable(&#39;test&#39;, [contents])print client.getTableNames()# insert datatransport.open()

row = &#39;row-key1&#39;mutations = [Mutation(column="cf:a", value="1")]
client.mutateRow(&#39;test&#39;, row, mutations)
# get one rowtableName = &#39;test&#39;rowKey = &#39;row-key1&#39;result = client.getRow(tableName, rowKey)
print resultfor r in result:    
print &#39;the row is &#39;, r.row    
print &#39;the values is &#39;, r.columns.get(&#39;cf:a&#39;).value

执行结果:

[&#39;test&#39;]
[TRowResult(columns={&#39;cf:a&#39;: TCell(timestamp=1488617173254, value=&#39;1&#39;)}, row=&#39;row-key1&#39;)]
the row is  row-key1
the values is  1

以上是Python中HBase的操作示例代码分析的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
了解差异:用于循环和python中的循环了解差异:用于循环和python中的循环May 16, 2025 am 12:17 AM

theDifferenceBetweewneaforoopandawhileLoopInpythonisthataThataThataThataThataThataThataNumberoFiterationSiskNownInAdvance,而leleawhileLoopisusedWhenaconDitionNeedneedneedneedNeedStobeCheckedStobeCheckedStobeCheckedStobeCheckedStobeceDrepeTysepectients.peatsiveSectlyStheStobeCeptellyWithnumberofiterations.1)forloopsareAceareIdealForitoringercortersence

Python循环控制:对于vs -a -a比较Python循环控制:对于vs -a -a比较May 16, 2025 am 12:16 AM

在Python中,for循环适用于已知迭代次数的情况,而while循环适合未知迭代次数且需要更多控制的情况。1)for循环适用于遍历序列,如列表、字符串等,代码简洁且Pythonic。2)while循环在需要根据条件控制循环或等待用户输入时更合适,但需注意避免无限循环。3)性能上,for循环略快,但差异通常不大。选择合适的循环类型可以提高代码的效率和可读性。

如何在Python中结合两个列表:5种简单的方法如何在Python中结合两个列表:5种简单的方法May 16, 2025 am 12:16 AM

在Python中,可以通过五种方法合并列表:1)使用 运算符,简单直观,适用于小列表;2)使用extend()方法,直接修改原列表,适用于需要频繁更新的列表;3)使用列表解析式,简洁且可对元素进行操作;4)使用itertools.chain()函数,内存高效,适合大数据集;5)使用*运算符和zip()函数,适用于需要配对元素的场景。每种方法都有其特定用途和优缺点,选择时应考虑项目需求和性能。

循环时循环:python语法,用例和示例循环时循环:python语法,用例和示例May 16, 2025 am 12:14 AM

foroopsare whenthenemberofiterationsisknown,而whileLoopsareUseduntilacTitionismet.1)ForloopSareIdealForeSequencesLikeLists,UsingSyntaxLike'forfruitinFruitinFruitinFruitIts:print(fruit)'。2)'

python串联列表列表python串联列表列表May 16, 2025 am 12:08 AM

toConcateNateAlistofListsInpython,useextend,listComprehensions,itertools.Chain,orrecursiveFunctions.1)ExtendMethodStraightForwardButverBose.2)listComprechencomprechensionsareconconconciseandemandeconeandefforlargerdatasets.3)

Python中的合并列表:选择正确的方法Python中的合并列表:选择正确的方法May 14, 2025 am 12:11 AM

Tomergelistsinpython,YouCanusethe操作员,estextMethod,ListComprehension,Oritertools

如何在Python 3中加入两个列表?如何在Python 3中加入两个列表?May 14, 2025 am 12:09 AM

在Python3中,可以通过多种方法连接两个列表:1)使用 运算符,适用于小列表,但对大列表效率低;2)使用extend方法,适用于大列表,内存效率高,但会修改原列表;3)使用*运算符,适用于合并多个列表,不修改原列表;4)使用itertools.chain,适用于大数据集,内存效率高。

Python串联列表字符串Python串联列表字符串May 14, 2025 am 12:08 AM

使用join()方法是Python中从列表连接字符串最有效的方法。1)使用join()方法高效且易读。2)循环使用 运算符对大列表效率低。3)列表推导式与join()结合适用于需要转换的场景。4)reduce()方法适用于其他类型归约,但对字符串连接效率低。完整句子结束。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

北端:融合系统,解释
1 个月前By尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
4 周前By尊渡假赌尊渡假赌尊渡假赌
<🎜>掩盖:探险33-如何获得完美的色度催化剂
2 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用