Advertisement
Guest User

Untitled

a guest
Mar 29th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1.  
  2. # coding: utf-8
  3.  
  4.  
  5. import psycopg2
  6.  
  7. class HLL():
  8.  
  9. @staticmethod
  10. def create_hll_item(name):
  11. details = "dbname='postgres' user='joe' host='localhost' password='joe'"
  12. conn = psycopg2.connect(details)
  13. cur = conn.cursor()
  14. create_id = """insert into dailyUniques(id, set) VALUES({0}, hll_empty());""".format(name)
  15. cur.execute(create_id)
  16. conn.commit()
  17. cur.close()
  18. conn.close()
  19. print("{0} committed".format(name))
  20.  
  21. @staticmethod
  22. def add_hll_number(key, value):
  23. details = "dbname='postgres' user='joe' host='localhost' password='joe'"
  24. conn = psycopg2.connect(details)
  25. cur = conn.cursor()
  26. update = """update dailyUniques set set = hll_add(set, hll_hash_integer({1})) where id={0};""".format(key, value)
  27. cur.execute(update)
  28. create_id
  29. conn.commit()
  30. cur.close()
  31. conn.close()
  32. print("{0} and {1} committed to db".format(key, value))
  33.  
  34. @staticmethod
  35. def add_hll_word(key, value):
  36. details = "dbname='postgres' user='joe' host='localhost' password='joe'"
  37. conn = psycopg2.connect(details)
  38. cur = conn.cursor()
  39. update = """update dailyUniques set set = hll_add(set, hll_hash_text('{1}')) where id={0};""".format(key, value)
  40. cur.execute(update)
  41. conn.commit()
  42. cur.close()
  43. conn.close()
  44. print("{0} and {1} committed to db".format(key, value))
  45.  
  46. @staticmethod
  47. def get_hll_count(key):
  48. details = "dbname='postgres' user='joe' host='localhost' password='joe'"
  49. conn = psycopg2.connect(details)
  50. cur = conn.cursor()
  51. query = """select hll_cardinality(set) from dailyUniques where id = 2;""".format(key, value)
  52. cur.execute(query)
  53. result = cur.fetchone()[0]
  54. put = conn.commit()
  55. cur.close()
  56. conn.close()
  57. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement