Skip to content

Instantly share code, notes, and snippets.

@ketuls
Last active August 29, 2015 14:13
Show Gist options
  • Save ketuls/7f849de7f045270c1e6a to your computer and use it in GitHub Desktop.
Save ketuls/7f849de7f045270c1e6a to your computer and use it in GitHub Desktop.
This file inserts random image descriptors in Postgres DB. You need to change the conn_string as per your requirement. All the modules of import list needs to be installed to use this. Sample command would be "python postgres_generate.py <Number of records>" . Table Name is hardcoded as "img_des1"
import psycopg2
import json
import md5
import random
import sys
from datetime import datetime
conn_string= "host='localhost' dbname='postgres' user='ketulshah'"
conn= psycopg2.connect(conn_string)
conn.autocommit = True
cur=conn.cursor()
try:
cur.execute('drop table img_des1')
except:
pass
try:
cur.execute('drop index img_dec1_index')
except:
pass
cur.execute('CREATE TABLE img_des1 (id char(32) PRIMARY KEY, des integer[500][32])')
cur.execute('CREATE Index img_dec1_index on img_des1 using hash(id) ')
count = int(sys.argv[1]);
bp = 1000
mod = count/bp;
startTime = datetime.now()
for j in range(mod):
des=[]
m = []
si = j * bp
ei = si + bp
print si
print ei
for i in range(si,ei):
des.append([[random.randint(1,255) for r in xrange(32)] for p in xrange(500)])
m.append(md5.md5(str(i)).hexdigest())
queryTime = datetime.now()
for i in range(bp):
cur.execute('insert into img_des1 values(%s,%s);',(m[i],des[i]) )
print("Query Time is ")
print datetime.now() - queryTime
cur.close()
conn.close()
print("Script Tme is ")
print datetime.now() - startTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment