Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Using Python to call HBASE

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report