>  기사  >  백엔드 개발  >  Python의 HBase 작업 예제 코드 분석

Python의 HBase 작업 예제 코드 분석

黄舟
黄舟원래의
2017-05-07 11:02:511656검색


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

데모 프로그램

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으로 문의하세요.