python - 在python中使用happybase关闭与hbase数据库的连接

标签 python hbase happybase

def hbasePopulate(self,table="abc",MachineIP="xx.xx.xx.xx"):

    connection=happybase.Connection(MachineIP,autoconnect=True)
    tablename=Reptype.lower()+'rep'
    print "Connecting to table "
    print tablename
    try:
        table=connection.table(tablename)
        for key,data in table.scan():
            print key,data
        print table
    #except IOError as e:
    except:
        print "Table does not exists,creating"
        self.createTable(table=table,machineIP=machineIP)

    with table.batch() as b:
        with open('xxx.csv','r') as queryFile:

            for lines in queryFile:

                lines=lines.strip("\n")
                splitRecord=lines.split(",")
                key=splitRecord[0]
                key=key.replace("'","")
                val=",".join(splitRecord[1:])
                val=ast.literal_eval(val)
                table.put(splitRecord[0],val)

    for key,data in table.scan():
        print key,data

def createTable(self,table="abc",MachineIP=""):
    connection=happybase.Connection(MachineIP,autoconnect=True)
    print "Connection Handle",connection
    tname=table.lower()
    tablename=str(tname)
    print "Creating table : "+table+", On Hbase machine : "+MachineIP
    families={"cf":{} ,}   #using default column family
    connection.create_table(table,families=families)
    print "Creating table done "

每次我运行此脚本时,它都会将数据填充到 hbase 表中,但它会保持连接打开。当我使用 netstat -an 检查时,我发现连接计数增加了,即使在脚本完成后,连接计数仍然存在。

我错过了什么吗?我们需要显式关闭连接吗?

感谢您的帮助。

最佳答案

找到了解决方案。原来是这样

    try:
        connection.close()
    except Exception as e:
            print "Unable to close connection to hbase "
            print e

关于python - 在python中使用happybase关闭与hbase数据库的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28454855/

相关文章:

shell - 在 Hbase shell 上运行多个查询,无需再次调用 hbase shell

hadoop - 使用 Hadoop 处理来自多个数据源的数据

python - 导入错误 : No module named happybase

python - 为什么即使使用编码也无法在 python 中显示汉字?

python - 将 anaconda v4.4 升级到 v5.0 后 pip 停止工作

python - 按 Pandas 中的特定列名称对数据框进行排序

python - 逐项搜索Python列表中的元素,并根据条件语句创建新列表

hbase - 当 truncate HBase Table Splits 被删除时

hadoop - 不能使用 'put'()通过happybase将数据添加到hbase

python - HBase 的 HappyBase 和原子批量插入