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条答案
按热度按时间7uzetpgm1#
找到解决办法了。原来是这样的
58wvjzkj2#
如果程序退出,所有网络连接都将自动关闭。您可能看到的是已关闭连接的等待时间状态。