def redis_pro(first, last): client = db.redis_conn() with client.pipeline() as pipe: for n in range(first, last): pipe.set('KEY:' + str(n), 'VALUE:' + str(n)) pipe.execute()
from mysql.connector import errorcode import db # connect to redis client = db.redis_conn() # set a key client.set('test-redis', 'Redis OK') # get a value value = client.get('test-redis') print(value) # Test connection with mysql try: db_connection = db.mysql_conn() print("Database connection made!") except connector.Error as error: if error.errno == errorcode.ER_BAD_DB_ERROR: print("Database doesn't exist") elif error.errno == errorcode.ER_ACCESS_DENIED_ERROR: print("User name or password is wrong") else: print(error) else: db_connection.close() # Test connection with cassandra cluster session = db.cassandra_conn() #session.execute('USE CityInfo') rows = session.execute('SELECT * FROM users') for row in rows: print(row.age, row.name, row.username)
import time import random import db def get_key(): rand = random.randint(1, db.records) return "KEY:"+str(rand) if __name__ == "__main__": val = get_key() print("\nRealizando consultas a Redis") print(f"\nObteniendo 1 clave aleatoria: {val}") # Una clave conn = db.redis_conn() start_time = time.time() result = conn.get(val) seconds = (time.time() - start_time) print(f" Se obtuvo {result} en {seconds} segundos\n") # 10 claves start_time = time.time() print(f"\nObteniendo 10 clave aleatorias:") for n in range(10): val = get_key() result = conn.get(val) print(f' {val} - {result}') seconds = (time.time() - start_time) print(f"En {seconds} segundos\n")