flink-connector-redis的使用方式和其他连接器几乎一样,除了一些公共的参数外(connector.type, format.type, or update-mode等),还支持以下参数

为了满足业务和数据的多样性,根据connector.data.type来确定写入的数据结构

1.string

取sql的第一个字段为key,第二个字段为value,调用set方法将数据写入,在这里我们使用拼接的string+item_id作为key,价格作为value,在本地单元测试运行结果如下,和数据源完全一致。

tableEnv.sqlUpdate("CREATE TABLE datasink (item_id String,price String) WITH ('connector.data.type' = 'string' ...)");

tableEnv.sqlUpdate("insert into datasink SELECT CONCAT('string',item_id) ,cast(price as string) FROM order_info");
//字段
String[] names = {"pay_hour", "item_id", "price", "total_count"}; //数据
data.add(Row.of("20200312", "1", 13.2D, 2L));
data.add(Row.of("20200312", "2", 12.2D, 2L));
data.add(Row.of("20200312", "3", 11.2D, 2L));
data.add(Row.of("20200312", "3", 21.2D, 2L));

在这里需要约定的是

1.第一个值作为key,第二个值作为value

2.key和value要自己在sql中转成string类型,避免数据损失精度

3.key要自己加好前缀避免和其他人的key发生冲突。

2.list

取sql的第一个字段为key,第二个字段为value,调用lpush方法将数据写入list

tableEnv.sqlUpdate("CREATE TABLE datasink (item_id String,price String) WITH ('connector.data.type' = 'list' ...)");

tableEnv.sqlUpdate("insert into datasink SELECT CONCAT('list',item_id) ,cast(price as string) FROM order_info");

//字段
String[] names = {"pay_hour", "item_id", "price", "total_count"}; //数据
data.add(Row.of("20200312", "1", 13.2D, 2L));
data.add(Row.of("20200312", "2", 12.2D, 2L));
data.add(Row.of("20200312", "3", 11.2D, 2L));
data.add(Row.of("20200312", "3", 21.2D, 2L));

约定如string

3.set

和list基本一致,区别是调用sadd将数据写入set

4.sortedset

调用的方法为zadd,数据保存在serted set中,所以需要每个value的分数用于排序,在这里第一个值为key,第二个值为score且必须是数值类型,第三个值为value

tableEnv.sqlUpdate("CREATE TABLE datasink (item_id_key string,price double,price String) WITH ('connector.data.type' = 'sortedset' ...)");

tableEnv.sqlUpdate("insert into datasink SELECT CONCAT('sortedset',item_id),price,cast(price as string) FROM order_info");

//字段
String[] names = {"pay_hour", "item_id", "price", "total_count"}; //数据
data.add(Row.of("20200312", "1", 13.2D, 2L));
data.add(Row.of("20200312", "2", 12.2D, 2L));
data.add(Row.of("20200312", "3", 11.2D, 2L));
data.add(Row.of("20200312", "3", 21.2D, 2L)); 

在这里需要约定的是

1.第一个值作为key,第二个值作为score且为数值,第三个值作为value

2.key和value要自己在sql中转成string类型,避免数据损失精度

3.key要自己加好前缀避免和其他人的key发生冲突。

5.map

取第一个字段为key,第二个字段为field,第三个字段为value,调用hset方法将数据写入map

tableEnv.sqlUpdate("CREATE TABLE datasink (item_id_key string,item_id String,price String) WITH ('connector.data.type' = 'map' ...)");

tableEnv.sqlUpdate("insert into datasink SELECT CONCAT('map',item_id),item_id ,cast(price as string) FROM order_info");

//字段
String[] names = {"pay_hour", "item_id", "price", "total_count"}; //数据
data.add(Row.of("20200312", "1", 13.2D, 2L));
data.add(Row.of("20200312", "2", 12.2D, 2L));
data.add(Row.of("20200312", "3", 11.2D, 2L));
data.add(Row.of("20200312", "3", 21.2D, 2L));

在这里需要约定的是

1.第一个值作为key,第二个值作为field,第三个值作为value

2.key和value要自己在sql中转成string类型,避免数据损失精度

3.key要自己加好前缀避免和其他人的key发生冲突。

本地测试的完整代码如下

import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.typeutils.RowTypeInfo;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.java.StreamTableEnvironment;
import org.apache.flink.types.Row;
import org.junit.Test; import java.util.ArrayList;
import java.util.List; public class TestRedisSink {
@Test
public void TestSink() throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env); DataStream<Row> ds = env.fromCollection(getTestData()).returns(getTestDataType()); tableEnv.createTemporaryView("order_info", ds); tableEnv.sqlUpdate(" CREATE TABLE datasink (item_id string,price String) WITH (" +
" 'connector.type' = 'redis'," +
" 'connector.cluster.ip' = '127.0.0.1'," +
" 'connector.cluster.port' = '7100,7101,7200,7201,7300,7301'," +
" 'connector.max.timeout.millis' = '1000'," +
" 'connector.max.total' = '1'," +
" 'connector.max.idle' = '1'," +
" 'connector.min.idle' = '1'," +
" 'connector.data.type' = 'string'," +
" 'connector.expire.second' = '600'" +
")");
tableEnv.sqlUpdate("insert into datasink SELECT CONCAT('string',item_id) ,cast(price as string) FROM order_info");
env.execute("test");
} private List<Row> getTestData() {
List<Row> data = new ArrayList<>();
data.add(Row.of("20200312", "1", 13.2D, 2L));
data.add(Row.of("20200312", "2", 12.2D, 2L));
data.add(Row.of("20200312", "3", 11.2D, 2L));
data.add(Row.of("20200312", "3", 21.2D, 2L));
return data;
} private RowTypeInfo getTestDataType() {
TypeInformation<?>[] types = {
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.STRING_TYPE_INFO,
BasicTypeInfo.DOUBLE_TYPE_INFO,
BasicTypeInfo.LONG_TYPE_INFO};
String[] names = {"pay_hour", "item_id", "price", "total_count"}; RowTypeInfo typeInfo = new RowTypeInfo(types, names);
return typeInfo;
}
}

redis sink使用模板

CREATE TABLE datasink (
item_id STRING,
price STRING
) WITH (
'connector.type' = 'redis',
'connector.cluster.ip' = '127.0.0.1',
'connector.cluster.port' = '7100,7101,7200,7201,7300,7301',
'connector.max.timeout.millis' = '60000',
'connector.max.total' = '1',
'connector.max.idle' = '1',
'connector.min.idle' = '1',
'connector.data.type' = 'string',
'connector.expire.second' = '600',
'connector.parallelism' = '2',
'connector.name' = 'redisSink'
)

最后附上on yarn任务图

Flink Table API & SQL 自定义Redis Sink 使用方式的更多相关文章

  1. 【翻译】Flink Table Api & SQL — 自定义 Source & Sink

    本文翻译自官网: User-defined Sources & Sinks  https://ci.apache.org/projects/flink/flink-docs-release-1 ...

  2. Flink Table Api & SQL 翻译目录

    Flink 官网 Table Api & SQL  相关文档的翻译终于完成,这里整理一个安装官网目录顺序一样的目录 [翻译]Flink Table Api & SQL —— Overv ...

  3. 【翻译】Flink Table Api & SQL — SQL客户端Beta 版

    本文翻译自官网:SQL Client Beta  https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/sqlCl ...

  4. 【翻译】Flink Table Api & SQL —— 概念与通用API

    本文翻译自官网:https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/common.html Flink Tabl ...

  5. 【翻译】Flink Table Api & SQL —— 连接到外部系统

    本文翻译自官网:Connect to External Systems  https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev ...

  6. 【翻译】Flink Table Api & SQL — Hive —— Hive 函数

    本文翻译自官网:Hive Functions  https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/hive/h ...

  7. 【翻译】Flink Table Api & SQL —— Overview

    本文翻译自官网:https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/ Flink Table Api & ...

  8. 【翻译】Flink Table Api & SQL —— 数据类型

    本文翻译自官网:https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/types.html Flink Table ...

  9. 【翻译】Flink Table Api & SQL —Streaming 概念 —— 表中的模式匹配 Beta版

    本文翻译自官网:Detecting Patterns in Tables Beta  https://ci.apache.org/projects/flink/flink-docs-release-1 ...

  10. 【翻译】Flink Table Api & SQL —Streaming 概念 —— 查询配置

    本文翻译自官网:Query Configuration  https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/s ...

随机推荐

  1. GPIO-CH32x系列芯片GPIO使用注意事项

    一.特殊IO使用注意事项 芯片型号:CH32F203C8T6.CH32V203C8T6 特殊IO:PC13.PC14.PC15 注意事项说明: 1.PC13~PC15的IO功能受限,速度必须限制在2M ...

  2. 从 Cloud-Native Relational DB 看数据库设计

    论文内容:Amazon Aurora: Design Considerations for HighThroughput Cloud-Native Relational Databases 里面介绍了 ...

  3. docker命令之docker build

    docker命令之docker build 明天要讲docker file的公开课,正好借此机会,整理下docker 命令的专题 语法 docker build [OPTIONS] PATH | UR ...

  4. 在Mariadb中创建数据库-九五小庞

    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品.在存储引擎 ...

  5. Osx10.14升级watchman踩坑记

    背景 使用 watchman 检测文件变化通知非常的好用, 但有些时候会出现 watchman 占用内存和 CPU 特别疯狂, 通过 watch-del 也无济与事, 由于 watchman 的版本 ...

  6. sort()排序以及多个属性数组对象排序(按条件排序)

    原生排序 let arr = [5,2,1,4,9,8] for(let i = 0 ; i < arr.length ; i ++) { for(let j = 0 ; j < arr. ...

  7. 记一次google手机恢复出厂设置到root抓包全过程

    前言 开始因为手机密码忘记了,不想重置,不然找店家root的工具都没了,自己也不会google root的操作,之前听说还挺麻烦的.操作了半天好了,确实是挺麻烦的,做个记录. 一.恢复出厂设置 还原教 ...

  8. 使用NAT网络模式搭建内网,修改IP地址

    使用NAT网络模式搭建内网,修改IP地址 首先说明一下虚拟机的三种联网方式: 桥接模式: 简单来说就是使这台虚拟机成为一台在互联网中的有独立IP的一台新设备和Mac地址(不够都是虚拟的) NAT模式: ...

  9. SpringBoot2.6.x及以上版本整合swagger文档

    SpringBoot的版本更新中引入了一些新的特性,并且Swagger的版本更新也同样引入了很多新的东西,这样就造成了许多配置无法实现一一对应的情况,因此高版本的SpringBoot集成Swagger ...

  10. PostGIS之维数扩展的九交模型

    1. 概述 PostGIS 是PostgreSQL数据库一个空间数据库扩展,它添加了对地理对象的支持,允许在 SQL 中运行空间查询 PostGIS官网:About PostGIS | PostGIS ...