Skip to main content

Profile picture for Ajeet Raina
Author:
Ajeet Raina, was Developer Growth Manager at Redis

Manage Redis time-series data using RedisTimeSeries Browser Tool

If you want to add a time series data structure to your Redis database, check out RedisTimeSeries browser tool that comes with RedisInsight.

RedisTimeseries is a Redis module developed by Redis to enhance your experience managing time-series data with Redis. It simplifies the use of Redis for time-series use cases such as internet of things (IoT) data, stock prices, and telemetry. With RedisTimeSeries, you can ingest and query millions of samples and events at the speed of Redis. Advanced tooling such as downsampling and aggregation ensure a small memory footprint without impacting performance. Use a variety of queries for visualization and monitoring with built-in connectors to popular monitoring tools like Grafana, Prometheus, and Telegraf.

With RedisInsight browser tool, you can perform the below sets of activities:

  • TS.RANGE & TS.MRANGE are supported
  • Charts support milliseconds.
  • Ability to configure auto refresh interval.
  • Ability to submit query with ‘ctrl + enter’ in single line mode
  • Display tabular as well as JSON view

Step 1. Create Redis database

Follow this link to create Redis database using Docker container that comes with RedisTimeSeries module enabled

Step 2: Download RedisInsight

To install RedisInsight on your local system, you need to first download the software from the Redis website.

Click this link to access a form that allows you to select the operating system of your choice.

My Image

Run the installer. After the web server starts, open http://YOUR_HOST_IP:8001 and add a Redis database connection.

Select "Connect to a Redis database" My Image

Enter the requested details, including Name, Host (endpoint), Port, and Password. Then click “ADD REDIS DATABASE”.

Step 3. Clone the repository

$ git clone https://github.com/redis-developer/redis-datasets
cd redis-datasets/redistimeseries/AirQualityUCI

Step 4. Execute the Python script

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""sample module for dataset loading into redistimeseries from csv file
"""

import argparse
import redis
import csv
import datetime
import logging
from tqdm import tqdm


def parse_dataset_row(line):

result = False
date = None
Time = None
unix_ts = None
carbon_monoxide = None
temperature_c = None
relative_humidity = None
# check if we have 15 fields or more, and all fields have something on it
if len(line) > 14 and sum([len(line[x]) > 0 for x in range(0, 14)]) == 14:
str_date = line[0]
str_time = line[1]
carbon_monoxide = (
float(line[2].replace(",", "."))
if (float(line[2].replace(",", ".")) > -200.0)
else None
)
temperature_c = (
float(line[12].replace(",", "."))
if (float(line[12].replace(",", ".")) > -200.0)
else None
)
relative_humidity = (
float(line[13].replace(",", "."))
if (float(line[13].replace(",", ".")) > -200.0)
else None
)
unix_ts = int(
datetime.datetime.strptime(
"{0} {1}".format(str_date, str_time), "%d/%m/%Y %H.%M.%S"
).timestamp()
)
result = True

return result, unix_ts, carbon_monoxide, temperature_c, relative_humidity


parser = argparse.ArgumentParser()
parser.add_argument("--port", type=int, help="redis instance port", default=6379)
parser.add_argument(
"--password", type=int, help="redis instance password", default=None
)
parser.add_argument("--verbose", help="enable verbose output", action="store_true")
parser.add_argument("--host", type=str, help="redis instance host", default="127.0.0.1")
parser.add_argument(
"--csv",
type=str,
help="csv file containing the dataset",
default="./AirQualityUCI/AirQualityUCI.csv",
)
parser.add_argument(
"--csv_delimiter", type=str, help="csv file field delimiter", default=";"
)
args = parser.parse_args()

log_level = logging.ERROR
if args.verbose is True:
log_level = logging.INFO
logging.basicConfig(level=log_level)

# redis setup
redis_obj = redis.Redis(host=args.host, port=args.port, password=args.password)
temperature_key = "ts:temperature"
carbon_monoxide_key = "ts:carbon_monoxide"
relative_humidity_key = "ts:relative_humidity"

with open(args.csv, newline="") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=args.csv_delimiter)
next(csv_reader, None) # skip the headers
for row in tqdm(csv_reader):
(
result,
unix_ts,
carbon_monoxide,
temperature_c,
relative_humidity,
) = parse_dataset_row(row)
if result is True:
try:
if temperature_c is not None:
redis_obj.execute_command(
"ts.add", temperature_key, unix_ts, temperature_c
)
logging.info(
"ts.add {0} {1} {2}".format(
temperature_key, unix_ts, temperature_c
)
)
if carbon_monoxide is not None:
redis_obj.execute_command(
"ts.add", carbon_monoxide_key, unix_ts, carbon_monoxide
)
logging.info(
"ts.add {0} {1} {2}".format(
carbon_monoxide_key, unix_ts, carbon_monoxide
)
)
if relative_humidity is not None:
redis_obj.execute_command(
"ts.add", relative_humidity_key, unix_ts, relative_humidity
)
logging.info(
"ts.add {0} {1} {2}".format(
relative_humidity_key, unix_ts, relative_humidity
)
)
except redis.RedisError as err:
logging.error(err)

Step 5. Execute the script

$ python3 dataloader.py
9471it [00:29, 326.33it/s]

Step 6. Query a range across one or multiple time-series

TS.RANGE ts:carbon_monoxide 1112596200 1112603400

alt_text

Step 7 . Displaying the JSON view

alt_text

Step 8. Displaying the tabular view

alt_text

Additional Resources