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

gk7wooem  于 2021-06-09  发布在  Hbase
关注(0)|答案(2)|浏览(350)
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 我看到连接计数增加了,即使在脚本完成之后,这种情况仍然存在。
我错过什么了吗?我们需要显式关闭连接吗?
谢谢你的帮助。

7uzetpgm

7uzetpgm1#

找到解决办法了。原来是这样的

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

58wvjzkj2#

如果程序退出,所有网络连接都将自动关闭。您可能看到的是已关闭连接的等待时间状态。

相关问题