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/.
CREATE TABLE redisSink ( order_id string, order_channel string, order_time double, pay_amount STRING, real_pay double, pay_time string, user_id double, user_name string, area_id double, primary key (order_id) not enforced ) WITH ( 'connector' = 'redis', 'host' = 'RedisIP', 'password' = 'RedisPassword', 'data-type' = 'sorted-set', 'deploy-mode' = 'master-replica', 'schema-syntax' = 'fields-scores' );
CREATE TABLE redisSink ( order_id string, arrayField Array<String>, arrayScore array<double>, primary key (order_id) not enforced ) WITH ( 'connector' = 'redis', 'host' = 'RedisIP', 'password' = 'RedisPassword', 'data-type' = 'sorted-set', "default-score" = '3', 'deploy-mode' = 'master-replica', 'schema-syntax' = 'array-scores' );
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' = '' ); |
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:
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 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.
|
key-ttl |
No |
None |
String |
Supplementary parameter of key-ttl-mode. Available values are as follows:
|
In this example, data is read from the Kafka data source and written to the Redis result table. The procedure is as follows:
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;
{"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"}
Run following command:
HGETALL 202103241606060001
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"
Run following command:
HGETALL 202103241000000001
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"
A: This is because the input data contains duplicate data. Deduplication is performed in the Redis set, and the number of records in the result decreases.
org.apache.flink.table.api.ValidationException: SQL validation failed. From line 1, column 40 to line 1, column 105: Parameters must be of the same type
A: The array type is used. However, the types of fields in the array are different. You need to ensure that the types of fields in the array in Redis are the same.
org.apache.flink.addons.redis.core.exception.RedisConnectorException: Wrong Redis schema for 'map' syntax: There should be a key (possibly) and 1 MAP non-key column.
A: When schema-syntax is map, the table creation statement in Flink can contain only one non-primary key column, and the column type must be map.
org.apache.flink.addons.redis.core.exception.RedisConnectorException: Wrong Redis schema for 'array' syntax: There should be a key (possibly) and 1 ARRAY non-key column.
A: When schema-syntax is array, the table creation statement in Flink can contain only one non-primary key column, and the column type must be array.
A: schema-syntax is used to process special types, such as map and array.
A: When fields is used, the field name in Flink is used as the Redis field of the hash data type, and the value of that field is used as the value of the hash data type in Redis. When map is used, the field key in Flink is used as the Redis field of the hash data type, and the value of that field is used as the value of the hash data type in Redis. The following is an example:
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' = 'kafkaTopic', 'properties.bootstrap.servers' = 'KafkaAddress1:KafkaPort,KafkaAddress2:KafkaPort', 'properties.group.id' = 'GroupId', 'scan.startup.mode' = 'latest-offset', 'format' = 'json' ); CREATE TABLE redisSink ( order_id string, maptest Map<string, String>, primary key (order_id) not enforced ) WITH ( 'connector' = 'redis', 'host' = 'RedisIP', 'password' = 'RedisPassword', 'deploy-mode' = 'master-replica', 'schema-syntax' = 'fields' ); insert into redisSink select order_id, Map[user_id, area_id] from orders;
{"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"}
1) "maptest" 2) "{0001=330106}"
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' = 'kafkaTopic', 'properties.bootstrap.servers' = 'KafkaAddress1:KafkaPort,KafkaAddress2:KafkaPort', 'properties.group.id' = 'GroupId', 'scan.startup.mode' = 'latest-offset', 'format' = 'json' ); CREATE TABLE redisSink ( order_id string, maptest Map<string, String>, primary key (order_id) not enforced ) WITH ( 'connector' = 'redis', 'host' = 'RedisIP', 'password' = 'RedisPassword', 'deploy-mode' = 'master-replica', 'schema-syntax' = 'map' ); insert into redisSink select order_id, Map[user_id, area_id] from orders;
{"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"}
1) "0001" 2) "330106"
A: The setting to fields or array does not result in different results. The only difference is that in the Flink table creation statement. fields can be multiple fields. However, array requires that the field is of the array type and the data types in the array must be the same. Therefore, fields are more flexible.
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' = 'kafkaTopic', 'properties.bootstrap.servers' = 'KafkaAddress1:KafkaPort,KafkaAddress2:KafkaPort', 'properties.group.id' = 'GroupId', 'scan.startup.mode' = 'latest-offset', 'format' = 'json' ); 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' = 'RedisIP', 'password' = 'RedisPassword', 'data-type' = 'list', 'deploy-mode' = 'master-replica', 'schema-syntax' = 'fields' ); insert into redisSink select * from orders;
{"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"}
Run the following command in Redis:
LRANGE 202103241000000001 0 8
1) "webShop" 2) "2021-03-24 10:00:00" 3) "100.0" 4) "100.0" 5) "2021-03-24 10:02:03" 6) "0001" 7) "Alice" 8) "330106"
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' = 'kafkaTopic', 'properties.bootstrap.servers' = 'KafkaAddress1:KafkaPort,KafkaAddress2:KafkaPort', 'properties.group.id' = 'GroupId', 'scan.startup.mode' = 'latest-offset', 'format' = 'json' ); CREATE TABLE redisSink ( order_id string, arraytest Array<String>, primary key (order_id) not enforced ) WITH ( 'connector' = 'redis', 'host' = 'RedisIP', 'password' = 'RedisPassword', 'data-type' = 'list', 'deploy-mode' = 'master-replica', 'schema-syntax' = 'array' ); insert into redisSink select order_id, array[order_channel,order_time,pay_time,user_id,user_name,area_id] from orders;
{"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"}
1) "webShop" 2) "2021-03-24 10:00:00" 3) "2021-03-24 10:02:03" 4) "0001" 5) "Alice" 6) "330106"