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

时间:2015-02-11 12:51:34

标签: 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进行检查时,我发现连接数已经增加,即使在脚本完成后也会持续存在。

我错过了什么吗?我们需要明确关闭连接吗?

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

得到了解决方案。转而成为

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

答案 1 :(得分:0)

如果程序退出,则会自动关闭所有网络连接。你可能会看到的是已经关闭的连接的TIME_WAIT状态。