安装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('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 datatransport.open() row = 'row-key1'mutations = [Mutation(column="cf:a", value="1")] client.mutateRow('test', row, mutations) # get one rowtableName = 'test'rowKey = 'row-key1'result = client.getRow(tableName, rowKey) print resultfor 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的操作示例代码分析的详细内容。更多信息请关注PHP中文网其他相关文章!

theDifferenceBetweewneaforoopandawhileLoopInpythonisthataThataThataThataThataThataThataNumberoFiterationSiskNownInAdvance,而leleawhileLoopisusedWhenaconDitionNeedneedneedneedNeedStobeCheckedStobeCheckedStobeCheckedStobeCheckedStobeceDrepeTysepectients.peatsiveSectlyStheStobeCeptellyWithnumberofiterations.1)forloopsareAceareIdealForitoringercortersence

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

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

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

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

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

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

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


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

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

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用