search
HomeBackend DevelopmentPython TutorialHBase operation example code analysis in Python


Installing HBase

HBase is a distributed column storage system built on HDFS, mainly used for massive structured data storage. Here, our goal is just to provide a basic environment for Python to access HBase, so we download the binary package directly and install it on a single machine. After downloading, unzip it, modify the configuration file, and then start HBase directly. The system version used is ubuntu14.04.

Download

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

Configuration

Modify hbase-env.sh and set JAVA_HOME.

export JAVA_HOME=/usr/lib/jvm/java-8-oracle

Modify hbase-site.xml and set the root directory for storing data.

<configuration>
    <property>
        <name>hbase.rootdir</name>
        <value>file:///home/mi/work/hbase/data</value>
    </property></configuration>

Start

bin/start-hbase.sh  # 启动bin/hbase shell  # 进入hbase交互shell

Install Thrift

After installing HBase, you need to install Thrift, because when calling HBase in other languages, you need to connect through Thrift.

Installing Thrift dependencies

sudo apt-get install automake bison flex g++ git libboost1.55 libevent-dev libssl-dev libtool make pkg-config

PS: libboost1.55-all-dev, there was a problem with the installation on my ubuntu14.04, so I installed libboost1.55.

Compile and install

Download the source code, unzip it and compile and install. Thrift download address

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

Start the Thrift service of HBase

bin/hbase-daemon.sh start thrift

Check the system process

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

You can see that the ThriftServer has been started successfully, and then we can use multiple languages ​​through Thrift comes to access HBase.

Python operation HBase

The following uses Python as an example to demonstrate how to access HBase.

Install dependency packages

sudo pip install thriftsudo pip install hbase-thrift

Demo program

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

Execution results:

[&#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

The above is the detailed content of HBase operation example code analysis in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python: compiler or Interpreter?Python: compiler or Interpreter?May 13, 2025 am 12:10 AM

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Python For Loop vs While Loop: When to Use Which?Python For Loop vs While Loop: When to Use Which?May 13, 2025 am 12:07 AM

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Python loops: The most common errorsPython loops: The most common errorsMay 13, 2025 am 12:07 AM

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

For loop and while loop in Python: What are the advantages of each?For loop and while loop in Python: What are the advantages of each?May 13, 2025 am 12:01 AM

Forloopsareadvantageousforknowniterationsandsequences,offeringsimplicityandreadability;whileloopsareidealfordynamicconditionsandunknowniterations,providingcontrolovertermination.1)Forloopsareperfectforiteratingoverlists,tuples,orstrings,directlyacces

Python: A Deep Dive into Compilation and InterpretationPython: A Deep Dive into Compilation and InterpretationMay 12, 2025 am 12:14 AM

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Is Python an interpreted or a compiled language, and why does it matter?Is Python an interpreted or a compiled language, and why does it matter?May 12, 2025 am 12:09 AM

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

For Loop vs While Loop in Python: Key Differences ExplainedFor Loop vs While Loop in Python: Key Differences ExplainedMay 12, 2025 am 12:08 AM

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

For and While loops: a practical guideFor and While loops: a practical guideMay 12, 2025 am 12:07 AM

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),