Skip to content

Commit 15d3149

Browse files
author
saul-data
committedDec 28, 2022
updated for redis store
·
v0.1.3v0.0.19
1 parent bd44d48 commit 15d3149

File tree

4 files changed

+132
-18
lines changed

4 files changed

+132
-18
lines changed
 

‎src/dataplane/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
pipeline_pandas_redis_get,
44
)
55

6+
from dataplane.pipelinerun.data_persist.redis_store import (
7+
pipeline_redis_store,
8+
pipeline_redis_get,
9+
)
10+
611
from dataplane.pipelinerun.data_persist.pandas_s3_store import (
712
pipeline_pandas_s3_get,
813
pipeline_pandas_s3_store,
@@ -27,6 +32,8 @@
2732
"hello",
2833

2934
# Pipeline transfers
35+
"pipeline_redis_store",
36+
"pipeline_redis_get",
3037
"pipeline_pandas_redis_store",
3138
"pipeline_pandas_redis_get",
3239
"pipeline_pandas_s3_get",
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from datetime import timedelta
2+
3+
def RedisCheck(r):
4+
5+
import redis
6+
7+
try:
8+
r.ping()
9+
except (redis.exceptions.ConnectionError, ConnectionRefusedError):
10+
print("Redis connection error.", redis.exceptions.ConnectionError, ConnectionRefusedError)
11+
return False
12+
return True
13+
14+
""" StoreKey: is the key to look up for retrieval later on.
15+
Redis: e.g. Redis = redis.Redis(host='redis-service', port=6379, db=0)
16+
Value: The value to pass
17+
Expire: Expires the data if true.
18+
ExpireDuration: If expires is true, how much time to expire. Default 15 mins
19+
"""
20+
def pipeline_redis_store(StoreKey, Value, Redis, Expire=True, ExpireDuration=timedelta(minutes=15)):
21+
22+
import os
23+
import io
24+
from datetime import datetime
25+
26+
# Start the timer
27+
start = datetime.now()
28+
29+
InsertKey = StoreKey+ "-" +os.getenv("DP_RUNID")
30+
31+
# Connect to Redis
32+
if RedisCheck(Redis) == False:
33+
raise Exception("Redis connection failed.")
34+
35+
if Expire:
36+
Redis.setex(InsertKey, ExpireDuration, value=Value)
37+
else:
38+
Redis.set(InsertKey, value=Value)
39+
40+
duration = datetime.now() - start
41+
42+
return {"result":"OK", "duration": str(duration), "key":InsertKey}
43+
44+
45+
"""
46+
StoreKey: is the key to look up for retrieval (set with RedisStore).
47+
Redis: e.g. Redis = redis.Redis(host='redis-service', port=6379, db=0)
48+
"""
49+
def pipeline_redis_get(StoreKey, Redis):
50+
51+
import os
52+
import io
53+
from datetime import datetime
54+
55+
# Start the timer
56+
start = datetime.now()
57+
58+
InsertKey = StoreKey+ "-" +os.getenv("DP_RUNID")
59+
60+
# Connect to Redis
61+
if RedisCheck(Redis) == False:
62+
raise Exception("Redis connection failed.")
63+
64+
# Retrieve dataframe from key
65+
value = Redis.get(InsertKey)
66+
67+
duration = datetime.now() - start
68+
69+
return {"result":"OK", "duration": str(duration), "key":InsertKey,"value": value}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
import os
3+
from .pandas_redis_store import pipeline_pandas_redis_store
4+
from .pandas_redis_store import pipeline_pandas_redis_get
5+
import redis
6+
from datetime import timedelta
7+
from nanoid import generate
8+
from dotenv import load_dotenv
9+
10+
def test_pandas_redis_store():
11+
12+
load_dotenv()
13+
14+
# ---------- Dataplane pipeline run ------------
15+
REDIS_HOST = os.environ["REDIS_HOST"]
16+
print("Redis:", REDIS_HOST)
17+
18+
# Dataplane run id
19+
os.environ["DP_RUNID"] = generate('1234567890abcdef', 10)
20+
21+
# Data to store in Redis as parquet
22+
data = {
23+
"calories": [420, 380, 390],
24+
"duration": [50, 40, 45]
25+
}
26+
import pandas as pd
27+
df = pd.DataFrame(data)
28+
dfrows = df.shape[0]
29+
30+
# Redis connection
31+
redisConnect = redis.Redis(host=REDIS_HOST, port=6379, db=0)
32+
33+
34+
# ---------- STORE PARQUET TO REDIS ------------
35+
36+
# Store the data with key hello - run id will be attached
37+
rs = pipeline_pandas_redis_store(StoreKey="hello", DataFrame=df, Redis=redisConnect, Expire=True, ExpireDuration=timedelta(minutes=15))
38+
print(rs)
39+
assert rs["result"]=="OK"
40+
41+
# ---------- RETRIEVE PARQUET FROM REDIS ------------
42+
43+
# Get the data
44+
rsget = pipeline_pandas_redis_get(StoreKey="hello", Redis=redisConnect)
45+
print(rsget)
46+
df = rsget["dataframe"]
47+
print(df.shape[0])
48+
# Test before and after rows
49+
assert df.shape[0] == dfrows
50+
assert rsget["result"]=="OK"
Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import os
3-
from .pandas_redis_store import pipeline_pandas_redis_store
4-
from .pandas_redis_store import pipeline_pandas_redis_get
3+
from .redis_store import pipeline_redis_store
4+
from .redis_store import pipeline_redis_get
55
import redis
66
from datetime import timedelta
77
from nanoid import generate
@@ -19,32 +19,20 @@ def test_redis_store():
1919
os.environ["DP_RUNID"] = generate('1234567890abcdef', 10)
2020

2121
# Data to store in Redis as parquet
22-
data = {
23-
"calories": [420, 380, 390],
24-
"duration": [50, 40, 45]
25-
}
26-
import pandas as pd
27-
df = pd.DataFrame(data)
28-
dfrows = df.shape[0]
22+
data = "hi there 123"
2923

3024
# Redis connection
3125
redisConnect = redis.Redis(host=REDIS_HOST, port=6379, db=0)
32-
33-
34-
# ---------- STORE PARQUET TO REDIS ------------
3526

3627
# Store the data with key hello - run id will be attached
37-
rs = pipeline_pandas_redis_store(StoreKey="hello", DataFrame=df, Redis=redisConnect, Expire=True, ExpireDuration=timedelta(minutes=15))
28+
rs = pipeline_redis_store(StoreKey="hello", Value=data, Redis=redisConnect, Expire=True, ExpireDuration=timedelta(minutes=15))
3829
print(rs)
3930
assert rs["result"]=="OK"
4031

4132
# ---------- RETRIEVE PARQUET FROM REDIS ------------
4233

4334
# Get the data
44-
rsget = pipeline_pandas_redis_get(StoreKey="hello", Redis=redisConnect)
45-
print(rsget)
46-
df = rsget["dataframe"]
47-
print(df.shape[0])
35+
rsget = pipeline_redis_get(StoreKey="hello", Redis=redisConnect)
4836
# Test before and after rows
49-
assert df.shape[0] == dfrows
37+
assert rsget["value"] == data
5038
assert rsget["result"]=="OK"

0 commit comments

Comments
 (0)