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
检查时,我看到连接计数增加了,即使在脚本完成后仍然存在。
我错过了什么吗?我们需要显式关闭连接吗?
感谢您的帮助。
得到了解决方案.原来是这个
try:
connection.close()
except Exception as e:
print "Unable to close connection to hbase "
print e
如果程序退出,则会自动关闭所有网络连接。您可能会看到已关闭连接的TIME_WAIT状态。