您现在的位置是:热点 >>正文
Flink常见维表Join方案,收藏学习开发很有用!
热点823人已围观
简介...
前言
实时数仓,难免会遇到join维表的业务。现总结几种方案,供各位看官选择:
查找关联(同步,异步) 状态编程,预加载数据到状态中,按需取 冷热数据 广播维表 Temporal Table Join Lookup Table Join
其中中间留下两个问题,供大家思考,可留言一起讨论?
查找关联
查找关联就是在主流数据中直接访问外部数据(mysql,redis,impala ...)去根据主键或者某种关键条件去关联取值。
适合: 维表数据量大,但是主数据不大的业务实时计算。
缺点:数据量大的时候,会给外部数据源库带来很大的压力,因为某条数据都需要关联。
同步
访问数据库是同步调用,导致 subtak 线程会被阻塞,影响吞吐量
import com.alibaba.fastjson.{JSON, JSONArray, JSONObject} import com.wang.stream.env.{FlinkStreamEnv, KafkaSourceEnv} import org.apache.flink.api.common.functions.FlatMapFunction import org.apache.flink.api.common.serialization.SimpleStringSchema import org.apache.flink.streaming.api.scala._ import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer import org.apache.flink.util.Collector def analyses(): Unit ={ val env: StreamExecutionEnvironment = FlinkStreamEnv.get() KafkaSourceEnv.getKafkaSourceStream(env,List("test")) .map(JSON.parseObject(_)) .filter(_!=null) .flatMap( new FlatMapFunction[JSONObject,String] { override def flatMap(jSONObject: JSONObject, collector: Collector[String]Fl): Unit = { // 如果topic就一张表,不用区分,如果多张表,可以通过database 与 table 区分,放到下一步去处理 // 表的名字 val databaseName:String = jSONObject.getString("database") // 表的名字 val tableName:String = jSONObject.getString("table") // 数据操作类型 INSERT UPDATE DELETE val operationType:String = jSONObject.getString("type") // 主体数据 val tableData: JSONArray = jSONObject.getJSONArray("data") // old ֵ val old: JSONArray = jSONObject.getJSONArray("old") &nbinsp; // canal json 可能存在批处理出现data数据多条 for (i AsyncIO 可以并发地处理多个请求,很大程度上减少了对 subtask 线程的阻塞。
def analyses(): Unit ={ val env: StreamExecutionEnvironment = FlinkStreamEnv.get() val source: DataStream[String] = KafkaSourceEnv.getKafkaSourceStream(env, List("test")) .map(JSON.parseObject(_)) .filter(_ != null) .flatMap( new FlatMapFunction[JSONObject, String] { override def flatMap(jSONObject: JSONObject, collector: Collector[String]): Unit = { // 如果topic就一张表,不用区分,如果多张表,可以通过database 与 table 区分,放到下一步去处理 // 表的名字 val databaseName: String = jSONObject.getString("database") // 表的名字  k常; val tableName: String = jSONObject.getString("table") // 数据操作类型 INSERT UPDATE DELETE val operationType: String = jSONObject.getString("type") // 主体数据 val tableData: JSONArray = jSONObject.getJSONArray("data") // old ֵ val old: JSONArray = jSONObject.getJSONArray("old") // canal json 可能存在批处理出现data数据多条 for (i 首先把维表数据初始化到state中,设置好更新时间,定时去把维表。
优点:flink 自己维护状态数据,"荣辱与共",不需要频繁链接外部数据源,达到解耦。
缺点:不适合大的维表和变化大的维表。
.keyBy(_._1) .process( new KeyedProcessFunction[String,(String,String,String,String,String), String]{ private var mapState:MapState[String,Map[String,String]] = _ 见维private var first: Boolean = true override def open(parameters: Configuration): Unit = { val config: StateTtlConfig = StateTtlConfig .newBuilder(org.apache.flink.api.common.time.Time.minutes(5)) .setUpdateType(StateTtlConfig.UpdateType.OnReadAndWrite) .setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired) .build() val join = new MapStateDescriptor[String,Map[String,String]]("join",classOf[String],classOf[Map[String,String]]) join.enableTimeToLive(config) mapState = getRuntimeContext.getMapState(join) } override def processElement( in: (String, String, String, String, String), &n表Jbsp; context: KeyedProcessFunction[String, (String, String, String, String, String),String]#Context, collector: Collector[String]): Unit = { // 加载维表 if(first){ first = false val time: Long = System.currentTimeMillis() getSmallDimTableInfo() // 设置好更新时间,定时去把维表 context.timerService().registerProcessingTimeTimer(time + 86400000) } // 数据处理,过来一条条数据,然后按照自己的业务逻辑去取维表的数据即可 // 然后封装 放到collect中 collector.collect(null) &nbsoip; } override def onTimer( timestamp: Long, ctx: KeyedProcessFunction[String, (String, String, String, String, String),String]#OnTimerContext, out: Collector[String]): Unit = { println("触发器执行") mapState.clear() getSmallDimTableInfo() println(mapState) ctx.timerService().registerProcessingTimeTimer(timestamp + 86400000) } def getSmallDimTableInfo(): Unit ={ // 加载 字典数据 val select_dictionary="select dic_code,pre_dictionary_id,dic_name from xxxx" valn方 dictionary: util.List[util.Map[String, AnyRef]] = MysqlUtil.executeQuery(select_dictionary, null) dictionary.foreach(item=>{ mapState.put("dic_dictionary_"+item.get("pre_dictionary_id").toString,item) }) } } ) .filter(_!=null) .addSink( new FlinkKafkaProducer[String]( "", "", new SimpleStringSchema() ) ) v.execute("")
思考下:直接定义一个Map集合这样的优缺点是什么?可以留言说出自己的看法?
冷热数据
思想:先去状态去取,如果没有,去外部查询,同时去存到状态里面。StateTtlConfig 的过期时间可以设置短点。
优点:中庸取值方案,热备常用数据到内存,也避免了数据join相对过多外部数据源。
缺点:也不能一劳永逸解决某些问题,热备数据过多,或者冷数据过大,都会对state 或者 外部数据库造成压力。
.filter(_._1 != null) .keyBy(_._1) .process( new KeyedProcessFunction[String,(String,String,String,String,String), String]{ private var mapState:MapState[String,Map[String,String]] = _ private var first: Boolean = true override def open(parameters: Configuration): Unit = { val config: StateTtlConfig = StateTtlConfi案,g .newBuilder(org.apache.flink.api.common.time.Time.days(1)) .setUpdateType(StateTtlConfig.UpdateType.OnReadAndWrite) .setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired) .build() val join = new MapStateDescriptor[String,Map[String,String]]("join",classOf[String],classOf[Map[String,String]]) join.enableTimeToLive(config) mapState = getRuntimeContext.getMapState(join) } override def processElement( in: (String, String, String, String, String), context: KeyedProcessFunction[String, (String, String, String, String, String),String]#Context, &nbs收藏p; collector: Collector[String]): Unit = { // 数据处理,过来一条条数据,然后按照自己的业务逻辑先去mapState去找,如果没有再去 外部去找 if (mapState.contains("xx_id")){ // 如果存在就取 }else{ // 如果不存在去外部拿,然后放到mapState中 val dim_sql="select dic_code,pre_dictionary_id,dic_name from xxxx where id=xx_id" val dim: util.List[util.Map[String, AnyRef]] = MysqlUtil.executeQuery(dim_sql, null) mapState.put("xx_id",null) } // 然后封装 放到collect中 collector.collect(null) } } ) 广播维表
比如上面提到的字典表,每一个Task都需要这份数据,那么需要join这份数据的时候就可以使用广播维表。
val dimStream=env.addSource(MysqlSource) // 广播状态 val broadcastStateDesc=new MapStateDescriptor[String,String]("broadcast学习state", BasicTypeInfo.STRING_TYPE_INFO, new MapTypeInfo<>(Long.class, Dim.class)) // 广播流 val broadStream=dimStream.broadcast() // 主数据流 val mainConsumer = new FlinkKafkaConsumer[String]("topic", new SimpleStringSchema(), kafkaConfig) val mainStream=env.addSource(mainConsumer) // 广播状态与维度表关联 val connectedStream=mainStream.connect(broadStream).map(..User(id,name)).key(_.1) connectedStream.process(new KeyedBroadcastProcessFunction[String,User,Map[Long,Dim],String] { override def processElement(value: User, ctx: KeyedBroadcastProcessFunction[String,User,Map[Long,Dim],String]#ReadOnlyContext, out: Collector[String]): Unit = { // 取到数据就可以愉快的玩耍了 val state=ctx.getBroadcastState(broadcastStateDesc) xxxxxx } }
「思考:」 如果把维表流也通过实时监控binlog到kafka,当维度数据发生变化时,更新放到状态中,这种方式,是不是更具有时效性呢?
(1)通过canal把变更binlog方式发送到kafka中。
(2)数据流定义成为广播流,广播到数据到主数据流中。
(3)定义一个广播状态存储数据,在主数据进行查找匹配,符合要求则join成功。
Temporal Table Join(FlinkSQL与Flink Table API)
由于维表是一张不断变化的表(静态表只是动态表的一种特例)。那如何 JOIN 一张不断变化的表呢?如果用传统的 JOIN 语法来表达维表 JOIN,是不完整的。因为维表是一直在更新变化的,如果用这个语法那么关联上的是开发哪个时刻的维表呢?我们是不知道的,结果是不确定的。所以 Flink SQL 的维表 JOIN 语法引入了Temporal Table 的标准语法,用来声明关联的是维表哪个时刻的快照。
普通关联会一直保留关联双侧的数据,数据也就会一直膨胀,直到撑爆内存导致任务失败,Temporal Join则可以定期清理过期数据,在合理的内存配置下即可避免内存溢出。
Event Time Temporal Join
语法
SELECT [column_list] FROM table1 [AS ] [LEFT] JOIN table2 FOR SYSTEM_TIME AS OF table1.{ proctime | rowtime } [AS ] ON table1.column-name1 = table2.column-name1
使用事件时间属性(即行时间属性),可以检索过去某个时间点的键值。这允许在一个共同的时间点连接两个表。
举例
假设我们有一个订单表,每个订单都有不同货币的价格。为了将此表正确地规范化为单一货币,每个订单都需要与下订单时的适当货币兑换率相结合。
CREATE TABLE orders ( order_id STRING, price DECIMAL(32,2), currency STRING, order_time TIMESTAMP(3), WATERMARK FOR order_time AS order_time ) WITH (/* ... */); CREATE TABLE currency_rates ( currency STRING, conversion_rate DECIMAL(32, 2), update_time TIMESTAMP(3) METADATA FROM `values.source.timestamp` VIRTUAL WATERMARK FOR update_time AS update_time, PRIMA很有RY KEY(currency) NOT ENFORCED ) WITH ( 'connector' = 'upsert-kafka', /* ... */ ); event-time temporal join需要temporal join条件的等价条件中包含的主键 SELECT order_id, price, currency, conversion_rate, order_time, FROM orders LEFT JOIN currency_rates FOR SYSTEM TIME AS OF orders.order_time ON orders.currency = currency_rates.currency Processing Time Temporal Join
处理时间时态表连接使用处理时间属性将行与外部版本表中键的最新版本相关联。
根据定义,使用processing-time属性,连接将始终返回给定键的最新值。可以将查找表看作是一个简单的HashMap,它存储来自构建端的所有记录。这种连接的强大之处在于,当在Flink中无法将表具体化为动态表时,它允许Flink直接针对外部系统工作。
使用FOR SYSTEM_TIME AS OF table1.proctime表示当左边表的记录与右边的维表join时,只匹配当前处理时间维表所对应的的快照数据。
Lookup Table Join
Lookup Join 通常用于通过连接外部表(维度表)补充信息,要求一个表具有处理时间属性,另一个表使 Lookup Source Connector。
JDBC 连接器可以用在时态表关联中作为一个可 lookup 的 source (又称为维表)。用到的语法是 Temporal Joins 的语法。
s""" |CREATE TABLE users( |id int, |name string, |PRIMARY KEY (id) NOT ENFORCED  用!; |) |WITH ( |'connector' = 'jdbc', |'url' = 'xxxx', |'driver'='$DRIVER_CLASS_NAME', |'table-name'='$tableName', |'lookup.cache.max-rows'='100', |'lookup.cache.ttl'='30s' |) |""".stripMargin s""" |CREATE TABLE car( |`id` bigint , |`user_id` bigint, |`proctime` as PROCTIME() |) |WITH ( | 'connector' = 'kafka', | 'topic' = '$topic',  Fl; | 'scan.startup.mode' = 'latest-offset', | 'properties.bootstrap.servers' = '$KAFKA_SERVICE', | 'properties.group.id' = 'indicator', | 'format' = 'canal-json' |) |""".stripMargin SELECT mc.user_id user_id, count(1) AS `value` FROM car mc inner join users FOR SYSTEM_TIME AS OF mc.proctime as u on mc.user_id=s.id group by mc.user_id 总结
总体来讲,关联维表有四个基础的方式:
(1)查找外部数据源关联
(2)预加载维表关联(内存,状态)
(3)冷热数据储备(算是1和2的结合使用)
(4)维表变更日志关联(广播也好,其他方式的流关联也好)
「同时考虑:」 吞吐量,时效性,外部数据源的负载,内存资源,解耦性等等方面。
四种join方式不存在绝对的一劳永逸,更多的是针对业务场景在各指标上的权衡取舍,因此看官需要结合场景来选择适合的。
Tags:
相关文章
OpenAI、甲骨文与软银联手建五大AI数据中心,投资超4000亿
热点近日,OpenAI、甲骨文(Oracle)与日本软银共同宣布,将在美国建设五个人工智能(AI)数据中心。这项名为 “星际之门”(Stargate)的宏大计划,预计未来三年内的总投资将超过4000亿美元...
【热点】
阅读更多云计算的安全挑战及其解决方案
热点在当今世界中,云计算的安全性是研究人员和专业人员关注的领域。研究领域的许多调查指出,其中一些安全问题以数据泄露的形式出现,而其他一些则涉及访问控制。无论发生什么问题,在选择软件或解决方案时,它都会极大...
【热点】
阅读更多标签: 创维集团 智能家电 新能源业务 现代服务业
热点云梦级3月27日云梦级消息,3月27日,创维集团(00751)发布截至2025年12月31日止年度的经审核业绩公告。报告期内,公司整体营业额为703.24亿元,较上年度的650.13亿元增长8.2%。...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.wilsonart.com/catalogsearch/result/?action=CHECK&q=谷歌霸屏教学【tg:seogoogle987】,XG1
- https://www.parriscues.com/catalogsearch/result/?q=谷歌霸屏教学【tg:seogoogle987】,fn2
- https://www.ushmm.org/search/results/?ID=8s0587Oi8UTv74zPPM4fuhwjNpe2hsEE&q=谷歌霸屏教学【tg:seogoogle987】,igt
- https://www.flickr.com/photos/tags/谷歌霸屏教学【tg:seogoogle987】jEz
- https://www.zisha.com/shop/list.shtml?wap_keyword=谷歌霸屏教学【tg:seogoogle987】,KbU
- https://www.xvideos.com/tags/谷歌霸屏教学【tg:seogoogle987】,4zf
- https://www.pinkpanda.ro/magazin/ingrijirea-tenului?page=87&q=谷歌霸屏教学【tg:seogoogle987】,JZy
- https://www.world-architects.com/en/search?term=谷歌霸屏教学【tg:seogoogle987】,nBs
- https://www.libros.unam.mx/catalogsearch/result/?cat=37&dir=asc&format=20&lit=20&manufacturer=3230&mode=list&order=year_edition&q=谷歌霸屏教学【tg:seogoogle987】,UTH
- https://www.lezhin.com/ko/tags/谷歌霸屏教学【tg:seogoogle987】,OlL
- https://www.mygrandma.com/Items.aspx?search=谷歌霸屏教学【tg:seogoogle987】,AYO
- https://www.safetv.go.kr/base/search/view?searchWord=谷歌霸屏教学【tg:seogoogle987】kn2
- https://www.abbreviations.com/谷歌霸屏教学【tg:seogoogle987】0ol
- https://www.teamlab.art/zh-hans/?submit=谷歌霸屏教学【tg:seogoogle987】,E56
- https://www.whirlpool.com.hk/chinese/catalogsearch/result?cat=30&q=谷歌霸屏教学【tg:seogoogle987】,5Ow
- https://www.lee.com/search?q=谷歌霸屏教学【tg:seogoogle987】,Mb1
- https://www.kingston.gov.uk/site-search/results/?q=谷歌霸屏教学【tg:seogoogle987】,tNf
- https://www.jusoen.com/addreng.asp?p1=谷歌霸屏教学【tg:seogoogle987】gcG
- https://www.youthforum.org/search-results?cx=4f5236218ab5e8ef9&ie=UTF-8&q=谷歌霸屏教学【tg:seogoogle987】,oBK
- https://www.lneg.pt/?cat=140&s=谷歌霸屏教学【tg:seogoogle987】,m9f
- https://www.mexperience.com/?T=1686132112324&s=谷歌霸屏教学【tg:seogoogle987】,OEi
- https://www.maigoo.com/search/?block=&q=谷歌霸屏教学【tg:seogoogle987】,vng
- https://www.lyrics.com/lyrics/kkk3谷歌霸屏教学【tg:seogoogle987】,pSj
- https://www.seforall.org/search?keywords=谷歌霸屏教学【tg:seogoogle987】,QX6
- https://www.ntv.co.jp/3min/search/?action=QUERY&q=谷歌霸屏教学【tg:seogoogle987】,qob
- https://www.repetto.jp/item/search?action=QUERY&q=谷歌霸屏教学【tg:seogoogle987】,Dmz
- https://www.kuaikanmanhua.com/sou/谷歌霸屏教学【tg:seogoogle987】,CSA
- https://www.posten-portal.de/search?query=谷歌霸屏教学【tg:seogoogle987】,Q7h
- https://www.springfieldlibrary.org/library/?s=谷歌霸屏教学【tg:seogoogle987】,tny
- https://www.xbiao.com/search/index?tpf&wd=谷歌霸屏教学【tg:seogoogle987】,MOt
- https://www.safetv.go.kr/base/search/view?searchWord=谷歌霸屏教学【tg:seogoogle987】E0W
- https://www.scivee.tv/?s=谷歌霸屏教学【tg:seogoogle987】,bE3
- https://www.laoairlines.com/?action=QUERY&s=谷歌霸屏教学【tg:seogoogle987】,vF9
- https://www.rhymezone.com/r/rhyme.cgi?typeofrhyme=oldrel&loc=nojs&Word=谷歌霸屏教学【tg:seogoogle987】,upM
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=谷歌霸屏教学【tg:seogoogle987】0Ku
- https://www.signingsavvy.com/sentence/谷歌霸屏教学【tg:seogoogle987】,2bt
- https://www.ng-slo.si/si/iskanje?cat=114&q=谷歌霸屏教学【tg:seogoogle987】,brW
- https://www.usslexington.com/?action=QUERY&s=谷歌霸屏教学【tg:seogoogle987】,PVZ
- https://www.kaskus.co.id/search?q=谷歌霸屏教学【tg:seogoogle987】,cVH
- https://www.samen-maier.at/search/谷歌霸屏教学【tg:seogoogle987】,VRF
- https://www.sic-info.org/en/?s=谷歌霸屏教学【tg:seogoogle987】,9Ta
- https://www.remit.co.jp/vi/?s=谷歌霸屏教学【tg:seogoogle987】8RK
- https://www.maafushidive.com?collections?vendors?q=谷歌霸屏教学【tg:seogoogle987】,FVE
- https://www.nextavenue.org/?action=QUERY&s=谷歌霸屏教学【tg:seogoogle987】,66Q
- https://www.pacific-lifestyle.co.uk/search?sort=alphabetical&term=谷歌霸屏教学【tg:seogoogle987】,O54
- https://www.lindseymedicalsupply.com/search/谷歌霸屏教学【tg:seogoogle987】,VQI
- https://www.zalora.sg/catalog/?cat=112&q=谷歌霸屏教学【tg:seogoogle987】,r2F
- https://www.yidian51.com/taobao/谷歌霸屏教学【tg:seogoogle987】,3jZ
- https://www.supersauna.de/catalogsearch/result/?q=谷歌霸屏教学【tg:seogoogle987】,Sih
- https://www.uctronics.com/catalogsearch/result/?q=谷歌霸屏教学【tg:seogoogle987】,S4A