Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- import psycopg2
- class HLL():
- @staticmethod
- def create_hll_item(name):
- details = "dbname='postgres' user='joe' host='localhost' password='joe'"
- conn = psycopg2.connect(details)
- cur = conn.cursor()
- create_id = """insert into dailyUniques(id, set) VALUES({0}, hll_empty());""".format(name)
- cur.execute(create_id)
- conn.commit()
- cur.close()
- conn.close()
- print("{0} committed".format(name))
- @staticmethod
- def add_hll_number(key, value):
- details = "dbname='postgres' user='joe' host='localhost' password='joe'"
- conn = psycopg2.connect(details)
- cur = conn.cursor()
- update = """update dailyUniques set set = hll_add(set, hll_hash_integer({1})) where id={0};""".format(key, value)
- cur.execute(update)
- create_id
- conn.commit()
- cur.close()
- conn.close()
- print("{0} and {1} committed to db".format(key, value))
- @staticmethod
- def add_hll_word(key, value):
- details = "dbname='postgres' user='joe' host='localhost' password='joe'"
- conn = psycopg2.connect(details)
- cur = conn.cursor()
- update = """update dailyUniques set set = hll_add(set, hll_hash_text('{1}')) where id={0};""".format(key, value)
- cur.execute(update)
- conn.commit()
- cur.close()
- conn.close()
- print("{0} and {1} committed to db".format(key, value))
- @staticmethod
- def get_hll_count(key):
- details = "dbname='postgres' user='joe' host='localhost' password='joe'"
- conn = psycopg2.connect(details)
- cur = conn.cursor()
- query = """select hll_cardinality(set) from dailyUniques where id = 2;""".format(key, value)
- cur.execute(query)
- result = cur.fetchone()[0]
- put = conn.commit()
- cur.close()
- conn.close()
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement