Redis Result Table

Function

DLI outputs the Flink job output data to Redis. Redis is a key-value storage system that supports multiple types of data structures. It can be used in scenarios such as caching, event publish/subscribe, and high-speed queuing. Redis supports direct read/write of strings, hashes, lists, queues, and sets. Redis works with in-memory datasets and provides persistence. For more information about Redis, visit https://redis.io/.

Prerequisites

Precautions

Syntax

1
2
3
4
5
6
7
8
9
create table dwsSink (
  attr_name attr_type 
  (',' attr_name attr_type)* 
  (','PRIMARY KEY (attr_name) NOT ENFORCED)
)
with (
  'connector' = 'redis',
  'host' = ''
);

Parameters

Table 1 Parameter description

Parameter

Mandatory

Default Value

Data Type

Description

connector

Yes

None

String

Connector to be used. Set this parameter to redis.

host

Yes

None

String

Redis connector address.

port

No

6379

Integer

Redis connector port.

password

No

None

String

Redis authentication password.

namespace

No

None

String

Redis key namespace.

For example, if the value is set to "person" and the key is "jack", the value in the Redis is person:jack.

delimiter

No

:

String

Delimiter between the Redis key and namespace.

data-type

No

hash

String

Redis data type. Available values are as follows:

  • hash
  • list
  • set
  • sorted-set
  • string

For details about the constraints, see Constraints on data-type.

schema-syntax

No

fields

String

Redis schema semantics. Available values are as follows:

  • fields: applicable to all data types. This value indicates that multiple fields can be set and the value of each field is read when data is written.
  • fields-scores: applicable to sorted-set data, indicating that each field is read as an independent score.
  • array: applicable to list, set, and sorted-set data.
  • array-scores: applicable to sorted-set data.
  • map: applicable to hash and sorted-set data.

For details about the constraints, see Constraints on schema-syntax.

deploy-mode

No

standalone

String

Deployment mode of the Redis cluster. The value can be standalone, master-replica, or cluster. The default value is standalone.

For details about the setting, see the instance type description of the Redis cluster.

retry-count

No

5

Integer

Number of attempts to connect to the Redis cluster.

connection-timeout-millis

No

10000

Integer

Maximum timeout for connecting to the Redis cluster.

commands-timeout-millis

No

2000

Integer

Maximum time for waiting for a completion response.

rebalancing-timeout-millis

No

15000

Integer

Sleep time when the Redis cluster fails.

default-score

No

0

Double

Default score when data-type is sorted-set.

ignore-retraction

No

false

Boolean

Whether to ignore Retract messages.

skip-null-values

No

true

Boolean

Whether null values will be skipped. If this parameter is false, null will be assigned for null values.

key-ttl-mode

No

no-ttl

String

Whether the Redis sink TTL function will be enabled. The value can be no-ttl, expire-msec, expire-at-date or expire-at-timestamp.

  • no-ttl: No expiration time is set.
  • expire-msec: validity period of the key. The parameter is a long string, in milliseconds.
  • expire-at-date: Date and time when the key expires. The value is in UTC time format.
  • expire-at-timestamp: Timestamp when the key expires.

key-ttl

No

None

String

Supplementary parameter of key-ttl-mode. Available values are as follows:

  • If key-ttl-mode is no-ttl, this parameter does not need to be configured.
  • If key-ttl-mode is expire-msec, set this parameter to a string that can be parsed into the Long type. For example, 5000 indicates that the key will expire in 5000 ms.
  • If key-ttl-mode is expire-at-date, set this parameter to a date.
  • If key-ttl-mode is expire-at-timestamp, set this parameter to a timestamp, in milliseconds. For example, 1679385600000 indicates that the expiration time is 2023-03-21 16:00:00.

Example

In this example, data is read from the Kafka data source and written to the Redis result table. The procedure is as follows:

  1. Create an enhanced datasource connection in the VPC and subnet where Redis locates, and bind the connection to the required Flink elastic resource pool.
  2. Set Redis security groups and add inbound rules to allow access from the Flink queue. Test the connectivity using the Redis address. If the connection is successful, the datasource is bound to the queue. Otherwise, the binding fails.
  3. Create a Flink OpenSource SQL job. Enter the following job script and submit the job.
    When you create a job, set Flink Version to 1.12 on the Running Parameters tab. Select Save Job Log, and specify the OBS bucket for saving job logs. Change the values of the parameters in bold as needed in the following script.
    CREATE TABLE orders (
      order_id string,
      order_channel string,
      order_time string,
      pay_amount double,
      real_pay double,
      pay_time string,
      user_id string,
      user_name string,
      area_id string
    ) WITH (
      'connector' = 'kafka',
      'topic' = '<yourTopic>',
      'properties.bootstrap.servers' = '<yourKafka>:<port>',
      'properties.group.id' = '<yourGroupId>',
      'scan.startup.mode' = 'latest-offset',
      'format' = 'json'
    );
    --In the following redisSink table, data-type is set to default value hash, schema-syntax is fields, and order_id is defined as the primary key. Therefore, the value of this field is used as the Redis key.
    CREATE TABLE redisSink (
      order_id string,
      order_channel string,
      order_time string,
      pay_amount double,
      real_pay double,
      pay_time string,
      user_id string,
      user_name string,
      area_id string,
      primary key (order_id) not enforced
    ) WITH (
      'connector' = 'redis',
      'host' = '<yourRedis>',
      'password' = '<yourPassword>',
      'deploy-mode' = 'master-replica',
      'schema-syntax' = 'fields'
    );
    
    insert into redisSink select * from orders;
  4. Connect to the Kafka cluster and insert the following test data into Kafka:
    {"order_id":"202103241000000001", "order_channel":"webShop", "order_time":"2021-03-24 10:00:00", "pay_amount":"100.00", "real_pay":"100.00", "pay_time":"2021-03-24 10:02:03", "user_id":"0001", "user_name":"Alice", "area_id":"330106"}
    
    {"order_id":"202103241606060001", "order_channel":"appShop", "order_time":"2021-03-24 16:06:06", "pay_amount":"200.00", "real_pay":"180.00", "pay_time":"2021-03-24 16:10:06", "user_id":"0001", "user_name":"Alice", "area_id":"330106"}
  5. Run the following commands in Redis and view the result:
    • Obtain the result whose key is 202103241606060001.

      Run following command:

      HGETALL 202103241606060001
      Command output:
       1) "user_id"
       2) "0001"
       3) "user_name"
       4) "Alice"
       5) "pay_amount"
       6) "200.0"
       7) "real_pay"
       8) "180.0"
       9) "order_time"
      10) "2021-03-24 16:06:06"
      11) "area_id"
      12) "330106"
      13) "order_channel"
      14) "appShop"
      15) "pay_time"
      16) "2021-03-24 16:10:06"
    • Obtain the result whose key is 202103241000000001.

      Run following command:

      HGETALL 202103241000000001
      Command output:
       1) "user_id"
       2) "0001"
       3) "user_name"
       4) "Alice"
       5) "pay_amount"
       6) "100.0"
       7) "real_pay"
       8) "100.0"
       9) "order_time"
      10) "2021-03-24 10:00:00"
      11) "area_id"
      12) "330106"
      13) "order_channel"
      14) "webShop"
      15) "pay_time"
      16) "2021-03-24 10:02:03"

FAQ