-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_loader.py
62 lines (47 loc) · 2.1 KB
/
data_loader.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
from dotenv import load_dotenv
from pathlib import Path
from time import sleep
import argparse
import json
import os
import redis
load_dotenv()
# Parse arguments and make sure we were invoked correctly.
arg_parser = argparse.ArgumentParser(description = "Load JSON data into Redis for search demo.")
arg_parser.add_argument("--load", dest="data_file_name", required=True, help="File containing JSON data to load.")
args = arg_parser.parse_args()
# Check if the data file exists...
data_file_path = Path(args.data_file_name)
if not data_file_path.is_file():
print(f"Data file {args.data_file_name} does not exist!")
os._exit(1)
# Connect to Redis
redis_client = redis.from_url(os.getenv("REDIS_URL"))
# Drop any existing index.
try:
print("Checking for previous index and dropping if found.")
redis_client.ft("idx:regions").dropindex(delete_documents = False)
print("Dropped old search index.")
except redis.exceptions.ResponseError as e:
# Dropping an index that doesn't exist throws an exception
# but isn't an error in this case - we just want to start
# from a known point.
if not str(e).startswith("Unknown Index"):
print("Error:")
print(e)
os._exit(1)
# Create a new index.
print("Creating index.")
redis_client.execute_command("FT.CREATE", "idx:regions", "ON", "JSON", "PREFIX", "1", "region:", "SCHEMA", "$.name", "AS", "name", "TAG", "$.boundaries", "AS", "boundaries", "GEOSHAPE", "SPHERICAL", "$.forecast.wind", "AS", "wind", "TEXT", "$.forecast.sea", "AS", "sea", "TEXT", "$.forecast.weather", "AS", "weather", "TEXT", "$.forecast.visibility", "AS", "visibility", "TEXT")
# Load the shipping forecast regional data from the JSON file.
num_loaded = 0
with open (args.data_file_name, "r") as input_file:
file_data = json.load(input_file)
for region in file_data["regions"]:
redis_key = f"region:{region['name'].replace(' ', '_').lower()}"
redis_client.json().set(redis_key, "$", region)
num_loaded += 1
print(f"Stored {redis_key} ({region['name']})")
redis_client.quit()
print(f"Regions loaded: {num_loaded}")
print("Done!")