33# DuckDB Redis Client Extension
44This extension provides Redis client functionality for DuckDB, allowing you to interact with a Redis server directly from SQL queries.
55
6- > Experimental: USE AT YOUR OWN RISK!
7- <img src="https://github.com/user-attachments/assets/46a5c546-7e9b-42c7-87f4-bc8defe674e0" width=250 />
8-
9- # DuckDB Redis Client Extension
10- This extension provides Redis client functionality for DuckDB, allowing you to interact with a Redis server directly from SQL queries.
11-
126> Experimental: USE AT YOUR OWN RISK!
137
148## Features
159Currently supported Redis operations:
1610- String operations: ` GET ` , ` SET `
1711- Hash operations: ` HGET ` , ` HSET `
1812- List operations: ` LPUSH ` , ` LRANGE `
19- - Batch operations: ` MGET ` , ` SCAN `
13+
2014
2115## Installation
2216``` sql
2317INSTALL redis FROM community;
2418LOAD redis;
25- INSTALL redis FROM community;
26- LOAD redis;
2719```
2820
2921## Usage
3022### Setting up Redis Connection
3123First, create a secret to store your Redis connection details:
24+
3225``` sql
3326-- Create a Redis connection secret
34- CALL redis_create_secret(' my_redis' , {
35- ' host' : ' localhost' ,
36- ' port' : ' 6379' ,
37- ' password' : ' optional_password'
38- });
39-
40- -- For cloud Redis services (e.g., Redis Labs)
41- CALL redis_create_secret(' redis_cloud' , {
42- ' host' : ' redis-xxxxx.cloud.redislabs.com' ,
43- ' port' : ' 16379' ,
44- ' password' : ' your_password'
45- });
27+ CREATE SECRET IF NOT EXISTS redis (
28+ TYPE redis,
29+ PROVIDER config,
30+ host ' localhost' ,
31+ port ' 6379' ,
32+ password ' optional_password'
33+ );
34+
35+ -- Create a Redis cloud connection secret
36+ CREATE SECRET IF NOT EXISTS redis (
37+ TYPE redis,
38+ PROVIDER config,
39+ host ' redis-1234.ec2.redns.redis-cloud.com' ,
40+ port ' 16959' ,
41+ password ' xxxxxx'
42+ );
4643```
4744
48- ### String Operations
4945### String Operations
5046``` sql
5147-- Set a value
52- SELECT redis_set(' user:1' , ' John Doe' , ' my_redis ' ) as result;
48+ SELECT redis_set(' user:1' , ' John Doe' , ' redis ' ) as result;
5349
5450-- Get a value
55- SELECT redis_get(' user:1' , ' my_redis ' ) as user_name;
51+ SELECT redis_get(' user:1' , ' redis ' ) as user_name;
5652
5753-- Set multiple values in a query
5854INSERT INTO users (id, name)
59- SELECT id, redis_set(
60- INSERT INTO users (id, name)
6155SELECT id, redis_set(
6256 ' user:' || id::VARCHAR ,
6357 name,
6458 ' my_redis'
65- ' my_redis'
6659)
6760FROM new_users;
6861```
6962
7063### Hash Operations
7164``` sql
7265-- Set hash fields
73- SELECT redis_hset(' user:1' , ' email' , ' john@example.com' , ' my_redis ' );
74- SELECT redis_hset(' user:1' , ' age' , ' 30' , ' my_redis ' );
66+ SELECT redis_hset(' user:1' , ' email' , ' john@example.com' , ' redis ' );
67+ SELECT redis_hset(' user:1' , ' age' , ' 30' , ' redis ' );
7568
7669-- Get hash field
77- SELECT redis_hget(' user:1' , ' email' , ' my_redis ' ) as email;
70+ SELECT redis_hget(' user:1' , ' email' , ' redis ' ) as email;
7871
7972-- Store user profile as hash
8073WITH profile(id, field, value) AS (
@@ -87,51 +80,52 @@ SELECT redis_hset(
8780 ' user:' || id::VARCHAR ,
8881 field,
8982 value,
90- ' my_redis '
83+ ' redis '
9184)
9285FROM profile;
9386```
9487
9588### List Operations
9689``` sql
9790-- Push items to list
98- SELECT redis_lpush(' mylist' , ' first_item' , ' my_redis ' );
99- SELECT redis_lpush(' mylist' , ' second_item' , ' my_redis ' );
91+ SELECT redis_lpush(' mylist' , ' first_item' , ' redis ' );
92+ SELECT redis_lpush(' mylist' , ' second_item' , ' redis ' );
10093
10194-- Get range from list (returns comma-separated values)
10295-- Get all items (0 to -1 means start to end)
103- SELECT redis_lrange(' mylist' , 0 , - 1 , ' my_redis ' ) as items;
96+ SELECT redis_lrange(' mylist' , 0 , - 1 , ' redis ' ) as items;
10497
10598-- Get first 5 items
106- SELECT redis_lrange(' mylist' , 0 , 4 , ' my_redis ' ) as items;
99+ SELECT redis_lrange(' mylist' , 0 , 4 , ' redis ' ) as items;
107100
108101-- Push multiple items
109102WITH items(value) AS (
110103 VALUES (' item1' ), (' item2' ), (' item3' )
111104)
112- SELECT redis_lpush(' mylist' , value, ' my_redis ' )
105+ SELECT redis_lpush(' mylist' , value, ' redis ' )
113106FROM items;
114107```
115108
116109### Batch Operations
117110``` sql
118111-- Get multiple keys at once
119- SELECT redis_mget(' key1,key2,key3' , ' my_redis' ) as values ;
112+ SELECT redis_mget(' key1,key2,key3' , ' redis' ) as values ;
113+ -- Returns comma-separated values for all keys
120114
121115-- Scan keys matching a pattern
122- SELECT redis_scan(' 0' , ' user:*' , 10 , ' my_redis ' ) as result;
116+ SELECT redis_scan(' 0' , ' user:*' , 10 , ' redis ' ) as result;
123117-- Returns: "cursor:key1,key2,key3" where cursor is the next position for scanning
124118-- Use the returned cursor for the next scan until cursor is 0
125119
126120-- Scan all keys matching a pattern
127121WITH RECURSIVE scan(cursor, keys) AS (
128122 -- Initial scan
129- SELECT split_part(redis_scan(' 0' , ' user:*' , 10 , ' my_redis ' ), ' :' , 1 ),
130- split_part(redis_scan(' 0' , ' user:*' , 10 , ' my_redis ' ), ' :' , 2 )
123+ SELECT split_part(redis_scan(' 0' , ' user:*' , 10 , ' redis ' ), ' :' , 1 ),
124+ split_part(redis_scan(' 0' , ' user:*' , 10 , ' redis ' ), ' :' , 2 )
131125 UNION ALL
132126 -- Continue scanning until cursor is 0
133- SELECT split_part(redis_scan(cursor, ' user:*' , 10 , ' my_redis ' ), ' :' , 1 ),
134- split_part(redis_scan(cursor, ' user:*' , 10 , ' my_redis ' ), ' :' , 2 )
127+ SELECT split_part(redis_scan(cursor, ' user:*' , 10 , ' redis ' ), ' :' , 1 ),
128+ split_part(redis_scan(cursor, ' user:*' , 10 , ' redis ' ), ' :' , 2 )
135129 FROM scan
136130 WHERE cursor != ' 0'
137131)
@@ -156,11 +150,9 @@ Follow the standard DuckDB extension build process:
156150make
157151```
158152
159- ## Dependencies
160- - Boost.Asio (header-only, installed via vcpkg)
161-
162153## Future Enhancements
163154Planned features include:
155+ - Table functions for scanning Redis keys
164156- Additional Redis commands (SADD, SMEMBERS, etc.)
165157- Batch operations using Redis pipelines
166158- Connection timeout handling
0 commit comments