This topic describes how to create an ApsaraDB for Redis result table in Realtime Compute. It also describes the parameters in the WITH clause, field type mapping, and attribute fields.

Note
  • This topic only applies to Realtime Compute V3.2.0 and later.
  • The ApsaraDB for Redis result tables in Realtime Compute support on-premises Redis databases.

Redis is an open-source key-value storage system written in ANSIC language. It features high performance and supports a wide range of data types. Realtime Compute allows you to create ApsaraDB for Redis dimension tables and result tables.

Syntax examples

Currently, ApsaraDB for Redis result tables support the following five types of Redis data structures. The DDL syntax is as follows:
  • STRING type
    A DDL statement has two columns: The first column is the key, and the second column is the value. To insert data into an ApsaraDB for Redis result table, run the set key value command.
    create table resik_output (
      a varchar,
      b varchar,
      primary key(a)
    ) with (
      type = 'redis',
      mode = 'string',
      host = '${redisHost}', -- For example, '127.0.0.1'.
      port = '${redisPort}', -- For example, '6379'.
      dbNum = '${dbNum}', -- The default value is 0.
      ignoreDelete = 'true' -- Specifies whether to delete the previously inserted data when the Retraction message is returned. The default value is false.
    )
  • LIST type
    A DDL statement has two columns: The first column is the key, and the second column is the value. To insert data into an ApsaraDB for Redis result table, run the lpush key value command.
    create table resik_output (
      a varchar,
      b varchar,
      primary key(a)
    ) with (
      type = 'redis',
      mode = 'list',
      host = '${redisHost}', -- For example, '127.0.0.1'.
      port = '${redisPort}', -- For example, '6379'.
      dbNum = '${dbNum}', -- The default value is 0.
      ignoreDelete = 'true' -- Specifies whether to delete the previously inserted data when the Retraction message is returned. The default value is false.
    )
  • SET type
    A DDL statement has two columns: The first column is the key, and the second column is the value. To insert data into an ApsaraDB for Redis result table, run the sadd key value command.
    create table resik_output (
      a varchar,
      b varchar,
      primary key(a)
    ) with (
      type = 'redis',
      mode = 'set',
      host = '${redisHost}', -- For example, '127.0.0.1'.
      port = '${redisPort}', -- For example, '6379'.
      dbNum = '${dbNum}', -- The default value is 0.
      ignoreDelete = 'true' -- Specifies whether to delete the previously inserted data when the Retraction message is returned. The default value is false.
    )
  • HASHMAP type
    A DDL statement has three columns: The first column is the key, the second column is the hash key, and the third column is the hash value. To insert data into an ApsaraDB for Redis result table, run the hmset key hash_key hash_value command.
    create table resik_output (
      a varchar,
      b varchar, 
      c varchar,
      primary key(a)
    ) with (
      type = 'redis',
      mode = 'hashmap',
      host = '${redisHost}', -- For example, '127.0.0.1'.
      port = '${redisPort}', -- For example, '6379'.
      dbNum = '${dbNum}', -- The default value is 0.
      ignoreDelete = 'true' -- Specifies whether to delete the previously inserted data when the Retraction message is returned. The default value is false.
    )
  • SORTEDSET type
    A DDL statement has three columns: The first column is the key, the second column is the score, and the third column is the value. To insert data into an ApsaraDB for Redis result table, run the add key score value command.
    create table resik_output (
      a varchar,
      b double,  -- The data must be of the DOUBLE type.
      c varchar,
      primary key(a)
    ) with (
      type = 'redis',
      mode = 'sortedset',
      host = '${redisHost}', -- For example, '127.0.0.1'.
      port = '${redisPort}', -- For example, '6379'.
      dbNum = '${dbNum}', -- The default value is 0.
      ignoreDelete = 'true' -- Specifies whether to delete the previously inserted data when the Retraction message is returned. The default value is false.
    )

Parameters in the WITH clause

Parameter Description Required Value
type The type of a result table. Yes Set the value to redis.
mode The data structure of the ApsaraDB for Redis result table. Valid values:
  • string
  • list
  • set
  • hashmap
  • sortedset
host The endpoint of the ApsaraDB for Redis database. Example: 127.0.0.1.
port The port of the ApsaraDB for Redis database. No Default value: 6379.
dbNum The sequence number of the ApsaraDB for Redis database. Default value: 0.
ignoreDelete Specifies whether to ignore the Retraction message. Valid values: true and false. Default value: false. If this parameter is set to true, the inserted data and the keys of the data are deleted when a Retraction message is received.
password The password that is used to access the ApsaraDB for Redis database. This parameter is empty by default. No permission verification is required.

Field type mapping

The following table describes the mapping between ApsaraDB for Redis data types and Realtime Compute data types. We recommend that you declare the mapping in a DDL statement.

ApsaraDB for Redis data type Realtime Compute data type
STRING VARCHAR
SCORE DOUBLE
Note The SCORE type in ApsaraDB for Redis databases applies to the SORTEDSET type, which is a sorted set. You must manually set a score of the DOUBLE type for each value and then sort the values based on their scores.

Example

The following sample code creates an ApsaraDB for Redis result table in a Realtime Compute job.
CREATE TABLE random_stream (
  v VARCHAR, 
  p VARCHAR) with (
    type = 'random'
);

create table resik_output (
  a VARCHAR,
  b VARCHAR,
  primary key(a) 
) with (
  type = 'redis',
  mode = 'string',
  host = '<yourRedisHost>',
  password = '<yourRedisPassword>'
);

INSERT INTO resik_output 
SELECT v, p
FROM random_stream;