-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedisDB.py
97 lines (82 loc) · 2.43 KB
/
RedisDB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import redis
# from redis import Redis
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
from redis.commands.search.field import TextField
from redis.commands.search.query import Query
import json
import time
import random
DEFAULT_REDIS_CONNECTION = "192.168.230.135"
DEFAULT_REDIS_PORT = "6379"
KEY_PREFIX = "person:"
IDX_NAME = "personIDX"
COLLECTION_SIZE = 1000000
COLLECTION_FILE_NAME = "testData.json"
NUM_OF_QUERIES = 50000
RESULTS_PER_QUERY = 100
FIRST_NAME_LIST = [
"James",
"John",
"Robert",
"Michael",
"William",
"David",
"Richard",
"Charles",
"Joseph",
"Thomas",
"Olivia",
"Emma",
"Charlotte",
"Amelia",
"Sophia",
"Isabella",
"Ava",
"Mia",
"Evelyn",
"Luna",
"Santiago",
"Mateo",
"Sebastián",
"Leonardo",
"Diego",
]
def main():
with open(COLLECTION_FILE_NAME, "r") as f:
docs = json.load(f)
print(f"Connecting to Redis: '{DEFAULT_REDIS_CONNECTION}'")
client = redis.Redis(
host=DEFAULT_REDIS_CONNECTION,
port=DEFAULT_REDIS_PORT,
decode_responses=True,
)
keys = client.keys(f"{KEY_PREFIX}*")
if keys:
client.delete(*keys)
print(f"deleted {len(keys)} from '{DEFAULT_REDIS_CONNECTION}'")
else:
print(f"Database '{DEFAULT_REDIS_CONNECTION}' Not found. Creating new")
try:
schema = TextField("$.children[0:].firstName", as_name="firstName")
client.ft(f"{IDX_NAME}").create_index(
schema, definition=IndexDefinition(index_type=IndexType.JSON)
)
except:
print("Index already exists")
start_time = time.time()
for i in range(0, COLLECTION_SIZE):
client.json().set(f"{KEY_PREFIX}{i}", "$", docs[i])
print(f"Database created.")
end_time = time.time()
total_time = end_time - start_time
print(f"Inserted {COLLECTION_SIZE} documents in {total_time} seconds")
redis_index = client.ft(IDX_NAME)
start_time = time.time()
q = Query(f"{random.choice(FIRST_NAME_LIST)}").paging(0, 100)
for i in range(0, NUM_OF_QUERIES):
found = redis_index.search(q)
end_time = time.time()
total_time = end_time - start_time
print(f"Sent {NUM_OF_QUERIES} Queries in {total_time} seconds")
if __name__ == "__main__":
main()