I am using Google Cloud's memorystore for Redis server. My objective is to store the query results coming out of BigQuery (SQL like) and to retrieve the results from Redis for subsequent usage. The query returns about 1000 rows with two columns which I try to set using client.set.
Once the values along with key are set, when I try to retrieve, it is returning me only the last value of the key. I am guessing that is because while trying to set values in a loop it is overwriting the previous value of the key. How do I overcome this problem of storing multiple values of the same key and retrieving it.
The code that I have used is as below:
import redis
import os
from google.cloud import bigquery
client = bigquery.Client()
redis_host = os.environ.get('REDISHOST', 'IP address')
redis_port = int(os.environ.get('REDISPORT', 6379))
r = redis.StrictRedis(host=redis_host, port=redis_port)
def hello_world():
if (r.get('company_name')):
print(r.get("company_name"))
else:
query = """SELECT coompany_name, news FROM `some-project.news` LIMIT 1000 """
query_job = client.query(query)
results = query_job.result()
for row in results:
r.set("company_name",row.company_name)
r.set("news", row.news)
r.expire("company_name",15)
r.expire("news",15)
print(row.company)
if __name__ == '__main__':
hello_world()
Please let me know how can I print all the key and values that are set