从python 3连接到Hbase的最佳库?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

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

被纵养的懒猫 2019-09-17 17:09:40 84

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

分布式数据库 Python Hbase
分享到
取消 提交回答
全部回答(2)
  • 游客aasf2nc2ujisi
    2019-11-22 09:44:49
    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)
    
    0 0
  • 不语奈何
    2019-09-18 10:24:03

    pip install thrift pip install hbase-thrift

    0 0
添加回答
数据库
使用钉钉扫一扫加入圈子
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

推荐文章
相似问题
推荐课程