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. Python源文件的编码

    Python源文件的编码 入门教程->2.2.1 源文件的字符编码 参考官网:https://docs.python.org/zh-cn/3.9/tutorial/interpreter.htm ...

  2. 用ksweb+Android做服务器,搭建WordPress博客环境

    旧的安卓手机不要仍,安装上ksweb给wordpress做服务器,隔壁小孩都馋哭了. 为了能在自己的安卓手机服务器上写博客,首先我们来了解一下WordPress和ksweb: WordPress是使用 ...

  3. 线程基础知识06 synchronized---使用javap查看相关指令

    1 示例-简单同步代码块 public class SychTest9 { public static void main(String[] args) { Object o = new Object ...

  4. 亲测有效! Super PhotoCut Pro 超级抠图工具 V2.8.8 for mac 破解版

    亲测有效! Super PhotoCut Pro 超级抠图工具 V2.8.8 for mac  破解版 Super PhotoCut 是一款专业的,非常易于使用的图片抠图工具.它能够准确地覆盖你想要去 ...

  5. js程序

    JavaScript 程序 计算机程序是由计算机"执行"的一系列"指令". 在编程语言中,这些编程指令被称为语句. JavaScript 程序就是一系列的编程语 ...

  6. aop中的名词解释

    aop中的名词解释 aop spring Joinpoint(连接点) 目标对象中所有可以增强的方法叫做连接点 Pointcut(切入点) 目标对象中要增强的的方法 Advice(通知/增强) 增强的 ...

  7. @Transactional千万不要这样用!!踩坑了你都可能发现不了!!!

    前阵子接手了一段同事之前的代码,里面用到了@Transactional注解,了解Spring的小伙伴肯定知道,@Transactional是Spring提供的一种控制事务管理的快捷手段.但是我这段程序 ...

  8. Iceberg 数据治理及查询加速实践

    数据治理 Flink 实时写入 Iceberg 带来的问题 在实时数据源源不断经过 Flink 写入的 Iceberg 的过程中,Flink 通过定时的 Checkpoint 提交 snapshot ...

  9. QFileDialog实现同时选择文件和文件夹,确认取消按钮英文问题解决方法

    如下图所示,需求是同时能够选择文件或者文件夹,但是QFileDialog文件窗口类要么只能选文件,要么只能选文件夹,无法同时去选择文件和文件夹: 要实现这样的需求,封装了一个类,实现同时选择文件和文件 ...

  10. gdbOF阅读笔记

    前言 今天阅读了一本说明书,<gdbOF: A Debugging Tool for OpenFOAM> 受himryangzz视频启发去读相关内容,在此对himryangzz表示感谢 希 ...