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 "

Every time I run this script it populated data to hbase table but it leaves a connection open. When I check using netstat -an I see the connection count has increased which persists even after the script completes.

Am I missing something? Do we need to explicitly close connection?

Thanks for helping.

ForceBru
  • 43,482
  • 10
  • 63
  • 98

2 Answers2

1

Got the solution .Turns out to be this

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

If the program exited, any network connections are automatically closed. What you're likely seeing is the TIME_WAIT state for already closed connections.

wouter bolsterlee
  • 3,879
  • 22
  • 30