1
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我看到连接数增加了,即使在脚本完成后仍然存在。

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

感谢您的帮助。

4

2 回答 2

1

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

    try:
        connection.close()
    except Exception as e:
            print "Unable to close connection to hbase "
            print e
于 2015-02-21T18:46:48.070 回答
0

如果程序退出,任何网络连接都会自动关闭。您可能会看到已关闭连接的 TIME_WAIT 状态。

于 2015-04-12T09:55:21.423 回答