SPL: Access Redis

 

Redis is a K-V database, in which values can be String, Hashmap, List, Set, and Sortedset. There are a variety of operating commands on each type of value, which amount to over three hundred with some administrative commands in total. However, these commands return the results of different formats and can only perform some simple operations such as accessing data. Their computational abilities for grouping, aggregation, sorting, sub-query, multi-step, etc., are relatively weak. The computation engine SPL encapsulates Java API (java-redis-client)offered by Redis to a redis_command() function in order to execute Redis commands and access data more easily, making up for the computation shortcomings of Redis.

 

Create/close Redis connection

Similar to the JDBC connection of relational database, SPL also connects Redis with paired “create/close”.

 

redis_open(redisUrl), parameter redisUrl is the address of Redis server.

 

redis_close(redisConn), redisConn is the Redis connection to be closed.

 

Code sample: A1 creates the connection, and A3 closes the connection after some other calculations in the middle steps.

 


A

1

=redis_open("127.0.0.1:6379")

2

……

3

=redis_close(A1)

 

Execute Redis commands

redis_command(redisConn,commandParts). There are two parameters: redisConn is the Redis connection; commandParts is a sequence of unfixed length that forms a full Redis command. The lengths of Redis commands vary from one another. Not only different Redis commands but also the same Redis command with different control options are all various in length.

 

Code sample:


A

1

=redis_open("127.0.0.1:6379")

2


3

=redis_command(A1,["COMMAND"])

4

=create(Name,Arity,Flags,FirstKey,LastKey,Step)

5

>A3.run(curr=~,A4.insert(0,curr(1),curr(2),curr(3),curr(4),curr(5),curr(6)))

6


7

=redis_command(A1,"SCAN 0 COUNT 5".split(" "))

8

=redis_command(A1,["SCAN",A7(1),"COUNT","5"])

9

=redis_command(A1,["SCAN",A8(1),"COUNT","5"])

10

=A7(2)&A8(2)&A9(2)

11


12

'return redis.call("SET",KEYS[1],ARGV[1])

13

=redis_command(A1,["EVAL",A12,"1","foo","bar"])

14


15

=redis_command(A1,["SET","time1",string(long(now()))])

16

=redis_command(A1,["GET","time1"])

17

=datetime(long(A16))

18


19

=hbase_close(A1)

 

In the first part of the code (A3-A5), A3 executes COMMAND to get the definitions of all Redis commands.

 

..

 

Every command definition is a sequence without structure, making it difficult to view. A4 defines a sequence table for convenient view. A5 organizes the result of A3 in the table sequence of A4. The result of A4 is shown as:

..

 

The second part of the code (A7-A10) executes SCAN command to get “key name” list. In “SCAN 0 COUNT 5”, 0 is the cursor id, and the new SCAN is initially 0. There are two values in the return result, the first one is cursor id, and the other is “key name” list:

..

 

Spell the cursor id returned in A7 to the A8 command, and spell the cursor id returned in A8 to the A9 command in the same way, then A10 merges the three result lists as:

..

 

The three-part of the code (A12-A13) executes the EVAL command to call a piece of Lua script: EVAL "return redis.call('SET', KEYS[1], ARGV[1])" 1 foo bar.

 

A12 defines a piece of Lua script of any complexity; A13 uses the redis_command()function in SPL to call the EVAL command in order to execute the script of A12. Every part of a simple command is split by spaces such as SCAN 0 COUNT 5, so we can use the split(“ ”) function in SPL to split the command and get the command sequence. But if the script is executed right now, it will be inappropriate to be spelled with EVAL command to a big string and then be split since there may be line breaks or quotation marks in the script. Instead, we should directly define the various elements of the command sequence like in A13.

..

 

There is only string type and no date type of data in Redis, so we need to convert the date data to save them. For example, in the fourth part (A15-A17) of the script, we need to save the timestamp (in millisecond) strings of the date and convert them back to the original type when reading in SPL. A15 calls the SET time1 1644980173398 command to save the current time to Redis, during which the string(), long(), and now()functions in SPL are used to generate the timestamp strings of the current time; A16 uses the GET time 1 command to read the time back; A17 uses the long() and datetime() functions in SPL to convert them back to date type of data:

..

..

..