Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Using Python to call HBASE

Shulou Source: shulou.com Published: 2022-06-03 04:30:10 10月14日 Update

It is necessary to install thrift hbase-thrift to call HBASE with Python

Start the thrift service for hbase: the default port for bin/hbase-daemon.sh start thrift is 9090

Data synchronization from mysql to hbase:

1 、 put

2 、 Importtsv

3. Write MapReduce Job import

4 、 sqoop

Simple code:

#! / usr/bin/env python

# coding=utf-8

Import sys

Sys.path.append ('/ usr/lib/python2.6/site-packages/hbase')

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 *

Import csv

From hbase.ttypes import ColumnDescriptor, Mutation, BatchMutation, TRegionInfo

From hbase.ttypes import IOError, AlreadyExists

#

Def client_conn ():

Transport=TSocket.TSocket (172.16.10.87 ", 9090)

Transport=TTransport.TBufferedTransport (transport)

Protocol=TBinaryProtocol.TBinaryProtocol (transport)

Client=Hbase.Client (protocol)

Transport.open ()

Return client

Def _ _ del__ ():

Transport.close ()

If _ _ name__== "_ _ main__":

Client=client_conn ()

# get the table name

Print client.getTableNames ()

# # creating a Table

# client.createTable ('name2', [ColumnDescriptor (name= "user_id:", maxVersions=1), ColumnDescriptor (name= "user_name"),])

# write data

Client.mutateRow ('name2','a1', [Mutation (column='user_id:1',value= "1")])

Client.mutateRow ('ca_record','1', [Mutation (column='user_id:0',value='5')])

# # obtaining data

Aa=client.getRow ('name2','a1')

For r in aa:

Print 'row',r.row

Print'\ br'

Print 'value',r.columns.get ("user_id:1"). Value

# # deleting tables

# client.disableTable ("T1")

# client.deleteTable ("T1")

Print client.getTableNames ()

# get the row key value of the table

# print client.scannerGet (client.scannerOpen ('T2, "cmd", ["a"]))

Print client.scannerGet (client.scannerOpen ('T2, ", [" a ")) # when row key fetches the first one empty

Print "-"

Print client.getColumnDescriptors ('t3')

# as long as you install thrift and hbase-thrift packages in your own development environment, there will be no problems with import

Tags: Data name time environment port problem installation synchronization development service Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi Microsoft OPPO Reno Shulou Tech Info