开发者社区> 问答> 正文

从python 3连接到Hbase的最佳库?

我想使用python3连接到hbase 1.4。什么是最好的解决方案。谢谢。

展开
收起
被纵养的懒猫 2019-09-17 17:09:40 1320 0
2 条回答
写回答
取消 提交回答
  • import happybase
    
    class HbaseUtil(object):
        # 获取一个连接
        def getHbaseConnection(self):
            conn = happybase.Connection(host='192.168.1.12', port=9090, timeout=None, autoconnect=True,
                                        table_prefix=None, table_prefix_separator=b'_', compat='0.98',
                                        transport='buffered', protocol='binary')
            return conn
    
        # 返回单行数据,返回tuple
        def querySingleLine(self, table, rowkey):
            conn = self.getHbaseConnection()
            t = happybase.Table(table, conn)
            return t.row(rowkey)
    
        # 返回多行数据,返回dict
        def queryMultilLines(self, table, list):
            conn = self.getHbaseConnection()
            t = happybase.Table(table, conn)
            return dict(t.rows(list))
    
        # 批量插入数据
        def batchPut(self, table):
            conn = self.getHbaseConnection()
            t = happybase.Table(table, conn)
            batch = t.batch(batch_size=10)
            return batch
    
        # exmple
        # batch_put = batchPut(table)
        #     person1 = {'info:name': 'lianglin', 'info:age': '30', 'info:addr': 'hubei'}
        #     person2 = {'info:name': 'jiandong', 'info:age': '22', 'info:addr': 'henan', 'info:school': 'henandaxue'}
        #     person3 = {'info:name': 'laowei', 'info:age': '29'}
        #     with batch_put as bat:
        #         bat.put('lianglin', person1)
        #         bat.put('jiandong', person2)
        #         bat.put('laowei', person3)
    
        # 插入单条数据
        def singlePut(self, table, rowkey, data):
            conn = self.getHbaseConnection()
            t = happybase.Table(table, conn)
            t.put(rowkey, data=data)
    
        # 批量删除数据
        def batchDelete(self, table, rowkeys):
            conn = self.getHbaseConnection()
            t = happybase.Table(table, conn)
            with t.batch() as bat:
                for rowkey in rowkeys:
                    bat.delete(rowkey)
    
        # 删除单行数据
        def singleDelete(self, table, rowkey):
            conn = self.getHbaseConnection()
            t = happybase.Table(table, conn)
            t.delete(rowkey)
    
        # 删除多个列族的数据
        def deleteColumns(self, table, rowkey, columns):
            conn = self.getHbaseConnection()
            t = happybase.Table(table, conn)
            t.delete(rowkey, columns=columns)
    
        # 删除一个列族中的几个列的数据
        def deleteDetailColumns(self, table, rowkey, detailColumns):
            conn = self.getHbaseConnection()
            t = happybase.Table(table, conn)
            t.delete(rowkey, columns=detailColumns)
    
        # 清空表
        def truncatTable(self, table, name, families):
            conn = self.getHbaseConnection()
            conn.disable_table(table)
            conn.delete_table(table)
            conn.create_table(table, name, families)
    
        # 删除hbase中的表
        def deletTable(self, table):
            conn = self.getHbaseConnection()
            conn.disable_table(table)
            conn.delete_table(table)
    
        # 扫描一张表
        def scanTable(self, table, row_start, row_stop, row_prefix):
            conn = self.getHbaseConnection()
            t = happybase.Table(table, conn)
            scan = t.scan(row_start=row_start, row_stop=row_stop, row_prefix=row_prefix)
            for key, value in scan:
                print(key, value)
    
    2019-11-22 09:44:49
    赞同 展开评论 打赏
  • 精于基础,广于工具,熟于业务。

    pip install thrift pip install hbase-thrift

    2019-09-18 10:24:03
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载