Redis Source Table

Function

Create a source stream to obtain data from Redis as input for jobs.

Prerequisites

Precautions

Syntax

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
create table dwsSource (
  attr_name attr_type 
  (',' attr_name attr_type)* 
  (',' watermark for rowtime_column_name as watermark-strategy_expression)
  ,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.

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 (for details, see Precautions and FAQ):

  • fields: applicable to all data types
  • fields-scores: applicable to sorted-set data
  • 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.

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.

scan-keys-count

No

1000

Integer

Number of data records read in each scan.

default-score

No

0

Double

Default score when data-type is sorted-set.

deserialize-error-policy

No

fail-job

Enum

Policy of how to process a data parsing failure. Available values are as follows:

  • fail-job: Fail the job.
  • skip-row: Skip the current data.
  • null-field: Set the current data to null.

skip-null-values

No

true

Boolean

Whether null values will be skipped.

Example

In this example, data is read from the DCS Redis data source and written to the Print 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. Run the following commands on the Redis client to insert data into different keys and store the data in hash format:
    HMSET redisSource 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
    
    HMSET redisSource1 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
    
    HMSET redisSource2 order_id 202103251202020001 order_channel miniAppShop order_time "2021-03-25 12:02:02" pay_amount 60.00 real_pay 60.00 pay_time "2021-03-25 12:03:00" user_id 0002 user_name Bob area_id 330110
  4. Create a Flink OpenSource SQL job. Enter the following job script to read data in hash format from Redis.
    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 redisSource (
      redisKey string,
      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 (redisKey) not enforced  --Obtains the key value from Redis.
    ) WITH (
      'connector' = 'redis',
      'host' = 'RedisIP',
      'password' = 'RedisPassword',
      'data-type' = 'hash',
      'deploy-mode' = 'master-replica'
    );
    
    CREATE TABLE printSink (
      redisKey string,
      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' = 'print'
    );
    
    insert into printSink select * from redisSource;
  5. Perform the following operations to view the data result in the taskmanager.out file:
    1. Log in to the DLI console. In the navigation pane, choose Job Management > Flink Jobs.
    2. Click the name of the corresponding Flink job, choose Run Log, click OBS Bucket, and locate the folder of the log you want to view according to the date.
    3. Go to the folder of the date, find the folder whose name contains taskmanager, download the taskmanager.out file, and view result logs.

    The data result is as follows:

    +I(redisSource1,202103241606060001,appShop,2021-03-24 16:06:06,200.0,180.0,2021-03-24 16:10:06,0001,Alice,330106)
    +I(redisSource,202103241000000001,webShop,2021-03-24 10:00:00,100.0,100.0,2021-03-24 10:02:03,0001,Alice,330106)
    +I(redisSource2,202103251202020001,miniAppShop,2021-03-25 12:02:02,60.0,60.0,2021-03-25 12:03:00,0002,Bob,330110)

FAQ