成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Flink實時計算Pv、Uv的幾種方法

開發 前端
KeyedStream可以轉換為WindowedStream,DataStream不能直接轉換為WindowedStream,WindowedStream可以直接轉換為DataStream。各種流之間雖然不能相互直接轉換,但是都可以通過先轉換為DataStream,再轉換為其它流的方法來實現。

[[403901]]

本文轉載自微信公眾號「Java大數據與數據倉庫」,作者柯少爺。轉載本文請聯系Java大數據與數據倉庫公眾號。

實時統計pv、uv是再常見不過的大數據統計需求了,前面出過一篇SparkStreaming實時統計pv,uv的案例,這里用Flink實時計算pv,uv。

我們需要統計不同數據類型每天的pv,uv情況,并且有如下要求.

  • 每秒鐘要輸出最新的統計結果;
  • 程序永遠跑著不會停,所以要定期清理內存里的過時數據;
  • 收到的消息里的時間字段并不是按照順序嚴格遞增的,所以要有一定的容錯機制;
  • 訪問uv并不一定每秒鐘都會變化,重復輸出對IO是巨大的浪費,所以要在uv變更時在一秒內輸出結果,未變更時不輸出;

Flink數據流上的類型和操作

DataStream是flink流處理最核心的數據結構,其它的各種流都可以直接或者間接通過DataStream來完成相互轉換,一些常用的流直接的轉換關系如圖:

可以看出,DataStream可以與KeyedStream相互轉換,KeyedStream可以轉換為WindowedStream,DataStream不能直接轉換為WindowedStream,WindowedStream可以直接轉換為DataStream。各種流之間雖然不能相互直接轉換,但是都可以通過先轉換為DataStream,再轉換為其它流的方法來實現。

在這個計算pv,uv的需求中就主要用到DataStream、KeyedStream以及WindowedStream這些數據結構。

這里需要用到window和watermark,使用窗口把數據按天分割,使用watermark可以通過“水位”來定期清理窗口外的遲到數據,起到清理內存的作用。

業務代碼

我們的數據是json類型的,含有date,version,guid這3個字段,在實時統計pv,uv這個功能中,其它字段可以直接丟掉,當然了在離線數據倉庫中,所有有含義的業務字段都是要保留到hive當中的。其它相關概念就不說了,會專門介紹,這里直接上代碼吧。

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" 
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
  5.     <modelVersion>4.0.0</modelVersion> 
  6.  
  7.     <groupId>com.ddxygq</groupId> 
  8.     <artifactId>bigdata</artifactId> 
  9.     <version>1.0-SNAPSHOT</version> 
  10.  
  11.     <properties> 
  12.         <scala.version>2.11.8</scala.version> 
  13.         <flink.version>1.7.0</flink.version> 
  14.         <pkg.name>bigdata</pkg.name
  15.     </properties> 
  16.  
  17.     <dependencies> 
  18.         <dependency> 
  19.             <groupId>org.apache.flink</groupId> 
  20.             <artifactId>flink-scala_2.11</artifactId> 
  21.             <version>{flink.version}</version> 
  22.   </dependency> 
  23.         <dependency> 
  24.             <groupId>org.apache.flink</groupId> 
  25.             <artifactId>flink-streaming-scala_2.11</artifactId> 
  26.             <version>flink.version</version> 
  27.   </dependency> 
  28.    
  29.         <dependency> 
  30.             <groupId>org.apache.flink</groupId> 
  31.             <artifactId>flink-streaming-java_2.11</artifactId> 
  32.             <version>{flink.version}</version> 
  33.         </dependency> 
  34.         <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-connector-kafka-0.8 --> 
  35.         <dependency> 
  36.             <groupId>org.apache.flink</groupId> 
  37.             <artifactId>flink-connector-kafka-0.10_2.11</artifactId> 
  38.             <version>flink.version</version> 
  39.   </dependency> 
  40.  
  41.     <build> 
  42.         <!--測試代碼和文件--> 
  43.         <!--<testSourceDirectory>{basedir}/src/test</testSourceDirectory>--> 
  44.         <finalName>basedir/src/test</testSourceDirectory>−−><finalName>{pkg.name}</finalName> 
  45.         <sourceDirectory>src/main/java</sourceDirectory> 
  46.         <resources> 
  47.             <resource> 
  48.                 <directory>src/main/resources</directory> 
  49.                 <includes> 
  50.                     <include>*.properties</include> 
  51.                     <include>*.xml</include> 
  52.                 </includes> 
  53.                 <filtering>false</filtering> 
  54.             </resource> 
  55.         </resources> 
  56.         <plugins> 
  57.             <!-- 跳過測試插件--> 
  58.             <plugin> 
  59.                 <groupId>org.apache.maven.plugins</groupId> 
  60.                 <artifactId>maven-surefire-plugin</artifactId> 
  61.                 <configuration> 
  62.                     <skip>true</skip> 
  63.                 </configuration> 
  64.             </plugin> 
  65.             <!--編譯scala插件--> 
  66.             <plugin> 
  67.                 <groupId>org.scala-tools</groupId> 
  68.                 <artifactId>maven-scala-plugin</artifactId> 
  69.                 <version>2.15.2</version> 
  70.                 <executions> 
  71.                     <execution> 
  72.                         <goals> 
  73.                             <goal>compile</goal> 
  74.                             <goal>testCompile</goal> 
  75.                         </goals> 
  76.                     </execution> 
  77.                 </executions> 
  78.             </plugin> 
  79.         </plugins> 
  80.     </build> 
  81. </project> 

主要代碼,主要使用scala開發:

  1. package com.ddxygq.bigdata.flink.streaming.pvuv 
  2.  
  3. import java.util.Properties 
  4.  
  5. import com.alibaba.fastjson.JSON 
  6. import org.apache.flink.runtime.state.filesystem.FsStateBackend 
  7. import org.apache.flink.streaming.api.CheckpointingMode 
  8. import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor 
  9. import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment} 
  10. import org.apache.flink.streaming.api.windowing.time.Time 
  11. import org.apache.flink.streaming.api.windowing.triggers.ContinuousProcessingTimeTrigger 
  12. import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer010 
  13. import org.apache.flink.streaming.util.serialization.SimpleStringSchema 
  14. import org.apache.flink.streaming.api.scala.extensions._ 
  15. import org.apache.flink.api.scala._ 
  16.  
  17. /** 
  18.   * @ Author: keguang 
  19.   * @ Date: 2019/3/18 17:34 
  20.   * @ version: v1.0.0 
  21.   * @ description:  
  22.   */ 
  23. object PvUvCount { 
  24.   def main(args: Array[String]): Unit = { 
  25.     val env = StreamExecutionEnvironment.getExecutionEnvironment 
  26.  
  27.     // 容錯 
  28.     env.enableCheckpointing(5000) 
  29.     env.getCheckpointConfig.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE) 
  30.     env.setStateBackend(new FsStateBackend("file:///D:/space/IJ/bigdata/src/main/scala/com/ddxygq/bigdata/flink/checkpoint/flink/tagApp")) 
  31.  
  32.     // kafka 配置 
  33.     val ZOOKEEPER_HOST = "hadoop01:2181,hadoop02:2181,hadoop03:2181" 
  34.     val KAFKA_BROKERS = "hadoop01:9092,hadoop02:9092,hadoop03:9092" 
  35.     val TRANSACTION_GROUP = "flink-count" 
  36.     val TOPIC_NAME = "flink" 
  37.     val kafkaProps = new Properties() 
  38.     kafkaProps.setProperty("zookeeper.connect", ZOOKEEPER_HOST) 
  39.     kafkaProps.setProperty("bootstrap.servers", KAFKA_BROKERS) 
  40.     kafkaProps.setProperty("group.id", TRANSACTION_GROUP) 
  41.  
  42.     // watrmark 允許數據延遲時間 
  43.     val MaxOutOfOrderness = 86400 * 1000L 
  44.      
  45.     // 消費kafka數據 
  46.     val streamData: DataStream[(String, String, String)] = env.addSource( 
  47.       new FlinkKafkaConsumer010[String](TOPIC_NAME, new SimpleStringSchema(), kafkaProps) 
  48.     ).assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor[String](Time.milliseconds(MaxOutOfOrderness)) { 
  49.       override def extractTimestamp(element: String): Long = { 
  50.         val t = JSON.parseObject(element) 
  51.         val time = JSON.parseObject(JSON.parseObject(t.getString("message")).getString("decrypted_data")).getString("time"
  52.         time.toLong 
  53.       } 
  54.     }).map(x => { 
  55.       var date = "error" 
  56.       var guid = "error" 
  57.       var helperversion = "error" 
  58.       try { 
  59.         val messageJsonObject = JSON.parseObject(JSON.parseObject(x).getString("message")) 
  60.         val datetime = messageJsonObject.getString("time"
  61.         date = datetime.split(" ")(0) 
  62.         // hour = datetime.split(" ")(1).substring(0, 2) 
  63.         val decrypted_data_string = messageJsonObject.getString("decrypted_data"
  64.         if (!"".equals(decrypted_data_string)) { 
  65.           val decrypted_data = JSON.parseObject(decrypted_data_string) 
  66.           guid = decrypted_data.getString("guid").trim 
  67.           helperversion = decrypted_data.getString("helperversion"
  68.         } 
  69.       } catch { 
  70.         case e: Exception => { 
  71.           println(e) 
  72.         } 
  73.       } 
  74.       (date, helperversion, guid) 
  75.     }) 
  76.     // 這上面是設置watermark并解析json部分 
  77.     // 聚合窗口中的數據,可以研究下applyWith這個方法和OnWindowedStream這個類 
  78.     val resultStream = streamData.keyBy(x => { 
  79.       x._1 + x._2 
  80.     }).timeWindow(Time.days(1)) 
  81.       .trigger(ContinuousProcessingTimeTrigger.of(Time.seconds(1))) 
  82.       .applyWith(("", List.empty[Int], Set.empty[Int], 0L, 0L))( 
  83.         foldFunction = { 
  84.           case ((_, list, set, _, 0), item) => { 
  85.             val date = item._1 
  86.             val helperversion = item._2 
  87.             val guid = item._3 
  88.             (date + "_" + helperversion, guid.hashCode +: list, set + guid.hashCode, 0L, 0L) 
  89.           } 
  90.         } 
  91.         , windowFunction = { 
  92.           case (key, window, result) => { 
  93.             result.map { 
  94.               case (leixing, list, set, _, _) => { 
  95.                 (leixing, list.sizeset.size, window.getStart, window.getEnd) 
  96.               } 
  97.             } 
  98.           } 
  99.         } 
  100.       ).keyBy(0) 
  101.       .flatMapWithState[(String, IntInt, Long, Long),(IntInt)]{ 
  102.       case ((key, numpv, numuv, beginend), curr) => 
  103.  
  104.         curr match { 
  105.           case Some(numCurr) if numCurr == (numuv, numpv) => 
  106.             (Seq.empty, Some((numuv, numpv))) //如果之前已經有相同的數據,則返回空結果 
  107.           case _ => 
  108.             (Seq((key, numpv, numuv, beginend)), Some((numuv, numpv))) 
  109.         } 
  110.     } 
  111.  
  112.     // 最終結果 
  113.     val resultedStream = resultStream.map(x => { 
  114.       val keys = x._1.split("_"
  115.       val date = keys(0) 
  116.       val helperversion = keys(1) 
  117.       (date, helperversion, x._2, x._3) 
  118.     }) 
  119.  
  120.     resultedStream.print() 
  121.     env.execute("PvUvCount"
  122.  
  123.   } 

使用List集合的size保存pv,使用Set集合的size保存uv,從而達到實時統計pv,uv的目的。

這里用了幾個關鍵的函數:

applyWith:里面需要的參數,初始狀態變量,和foldFunction ,windowFunction ;

存在的問題

顯然,當數據量很大的時候,這個List集合和Set集合會很大,并且這里的pv是否可以不用List來存儲,而是通過一個狀態變量,不斷做累加,對應操作就是更新狀態來完成。

改進版

使用了一個計數器來存儲pv的值。

  1. packagecom.ddxygq.bigdata.flink.streaming.pvuv 
  2.  
  3. import java.util.Properties 
  4.  
  5. import com.alibaba.fastjson.JSON 
  6. import org.apache.flink.api.common.accumulators.IntCounter 
  7. import org.apache.flink.runtime.state.filesystem.FsStateBackend 
  8. import org.apache.flink.streaming.api.CheckpointingMode 
  9. import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor 
  10. import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment} 
  11. import org.apache.flink.streaming.api.windowing.time.Time 
  12. import org.apache.flink.streaming.api.windowing.triggers.ContinuousProcessingTimeTrigger 
  13. import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer010 
  14. import org.apache.flink.streaming.util.serialization.SimpleStringSchema 
  15. import org.apache.flink.streaming.api.scala.extensions._ 
  16. import org.apache.flink.api.scala._ 
  17. import org.apache.flink.core.fs.FileSystem 
  18.  
  19. object PvUv2 { 
  20.   def main(args: Array[String]): Unit = { 
  21.     val env = StreamExecutionEnvironment.getExecutionEnvironment 
  22.  
  23.     // 容錯 
  24.     env.enableCheckpointing(5000) 
  25.     env.getCheckpointConfig.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE) 
  26.     env.setStateBackend(new FsStateBackend("file:///D:/space/IJ/bigdata/src/main/scala/com/ddxygq/bigdata/flink/checkpoint/streaming/counter")) 
  27.  
  28.     // kafka 配置 
  29.     val ZOOKEEPER_HOST = "hadoop01:2181,hadoop02:2181,hadoop03:2181" 
  30.     val KAFKA_BROKERS = "hadoop01:9092,hadoop02:9092,hadoop03:9092" 
  31.     val TRANSACTION_GROUP = "flink-count" 
  32.     val TOPIC_NAME = "flink" 
  33.     val kafkaProps = new Properties() 
  34.     kafkaProps.setProperty("zookeeper.connect", ZOOKEEPER_HOST) 
  35.     kafkaProps.setProperty("bootstrap.servers", KAFKA_BROKERS) 
  36.     kafkaProps.setProperty("group.id", TRANSACTION_GROUP) 
  37.  
  38.     // watrmark 允許數據延遲時間 
  39.     val MaxOutOfOrderness = 86400 * 1000L 
  40.  
  41.     val streamData: DataStream[(String, String, String)] = env.addSource( 
  42.       new FlinkKafkaConsumer010[String](TOPIC_NAME, new SimpleStringSchema(), kafkaProps) 
  43.     ).assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor[String](Time.milliseconds(MaxOutOfOrderness)) { 
  44.       override def extractTimestamp(element: String): Long = { 
  45.         val t = JSON.parseObject(element) 
  46.         val time = JSON.parseObject(JSON.parseObject(t.getString("message")).getString("decrypted_data")).getString("time"
  47.         time.toLong 
  48.       } 
  49.     }).map(x => { 
  50.       var date = "error" 
  51.       var guid = "error" 
  52.       var helperversion = "error" 
  53.       try { 
  54.         val messageJsonObject = JSON.parseObject(JSON.parseObject(x).getString("message")) 
  55.         val datetime = messageJsonObject.getString("time"
  56.         date = datetime.split(" ")(0) 
  57.         // hour = datetime.split(" ")(1).substring(0, 2) 
  58.         val decrypted_data_string = messageJsonObject.getString("decrypted_data"
  59.         if (!"".equals(decrypted_data_string)) { 
  60.           val decrypted_data = JSON.parseObject(decrypted_data_string) 
  61.           guid = decrypted_data.getString("guid").trim 
  62.           helperversion = decrypted_data.getString("helperversion"
  63.         } 
  64.       } catch { 
  65.         case e: Exception => { 
  66.           println(e) 
  67.         } 
  68.       } 
  69.       (date, helperversion, guid) 
  70.     }) 
  71.  
  72.     val resultStream = streamData.keyBy(x => { 
  73.       x._1 + x._2 
  74.     }).timeWindow(Time.days(1)) 
  75.       .trigger(ContinuousProcessingTimeTrigger.of(Time.seconds(1))) 
  76.       .applyWith(("", new IntCounter(), Set.empty[Int], 0L, 0L))( 
  77.         foldFunction = { 
  78.           case ((_, cou, set, _, 0), item) => { 
  79.             val date = item._1 
  80.             val helperversion = item._2 
  81.             val guid = item._3 
  82.             cou.add(1) 
  83.             (date + "_" + helperversion, cou, set + guid.hashCode, 0L, 0L) 
  84.           } 
  85.         } 
  86.         , windowFunction = { 
  87.           case (key, window, result) => { 
  88.             result.map { 
  89.               case (leixing, cou, set, _, _) => { 
  90.                 (leixing, cou.getLocalValue, set.size, window.getStart, window.getEnd) 
  91.               } 
  92.             } 
  93.           } 
  94.         } 
  95.       ).keyBy(0) 
  96.       .flatMapWithState[(String, IntInt, Long, Long),(IntInt)]{ 
  97.       case ((key, numpv, numuv, beginend), curr) => 
  98.  
  99.         curr match { 
  100.           case Some(numCurr) if numCurr == (numuv, numpv) => 
  101.             (Seq.empty, Some((numuv, numpv))) //如果之前已經有相同的數據,則返回空結果 
  102.           case _ => 
  103.             (Seq((key, numpv, numuv, beginend)), Some((numuv, numpv))) 
  104.         } 
  105.     } 
  106.  
  107.     // 最終結果 
  108.     val resultedStream = resultStream.map(x => { 
  109.       val keys = x._1.split("_"
  110.       val date = keys(0) 
  111.       val helperversion = keys(1) 
  112.       (date, helperversion, x._2, x._3) 
  113.     }) 
  114.  
  115.     val resultPath = "D:\\space\\IJ\\bigdata\\src\\main\\scala\\com\\ddxygq\\bigdata\\flink\\streaming\\pvuv\\result" 
  116.     resultedStream.writeAsText(resultPath, FileSystem.WriteMode.OVERWRITE) 
  117.     env.execute("PvUvCount"
  118.  
  119.   } 

改進

其實這里還是需要set保存uv,難免對內存有壓力,如果我們的集群不大,為了節省開支,我們可以使用外部媒介,如hbase的rowkey唯一性、redis的set數據結構,都是可以達到實時、快速去重的目的。

參考資料

https://flink.sojb.cn/dev/event_time.htm

lhttp://wuchong.me/blog/2016/05/20/flink-internals-streams-and-operations-on-streams

https://segmentfault.com/a/1190000006235690

 

責任編輯:武曉燕 來源: Java大數據與數據倉庫
相關推薦

2021-06-03 08:10:30

SparkStream項目Uv

2021-11-01 13:11:45

FlinkPvUv

2021-03-10 08:22:47

FlinktopN計算

2021-07-16 10:55:45

數倉一體Flink SQL

2016-12-28 14:27:24

大數據Apache Flin搜索引擎

2015-08-31 14:27:52

2022-12-29 09:13:02

實時計算平臺

2015-07-31 10:35:18

實時計算

2016-10-16 13:48:54

多維分析 UVPV

2019-06-27 09:12:43

FlinkStorm框架

2009-09-18 12:29:55

2010-05-17 15:17:06

MySQL常用操作

2025-03-05 08:40:00

RedisJava開發

2019-11-21 09:49:29

架構運維技術

2017-09-26 09:35:22

2015-10-09 13:42:26

hbase實時計算

2010-06-03 08:55:43

LINQ

2013-08-21 11:31:21

iPhone圖片方法

2021-03-10 14:04:10

大數據計算技術

2010-01-22 14:46:25

C++語言
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美日韩在线一区二区三区 | a看片 | 欧美综合国产精品久久丁香 | 91久久久久久久久久久久久 | 国产欧美日韩二区 | 欧美日韩一区在线观看 | 国产精品久久一区二区三区 | www.日本在线 | 婷婷成人在线 | 日日操夜夜干 | 一级做a毛片| 精品亚洲一区二区三区 | 久久久久无码国产精品一区 | 午夜精品久久久久久久99黑人 | 国产成人免费视频网站高清观看视频 | 成人美女免费网站视频 | 午夜视频免费在线观看 | 亚洲精品视频免费观看 | 午夜天堂 | 精品国产乱码久久久久久88av | 亚洲电影免费 | 亚洲免费片 | 国产 日韩 欧美 在线 | 久久久精 | 精品一区二区三区中文字幕 | 天天夜干 | 国产精品久久一区二区三区 | 国产九九九| av网站免费在线观看 | 国产精品久久久99 | 五月天天丁香婷婷在线中 | 亚洲精品久久久久久下一站 | 午夜精品久久久久久久久久久久久 | 四虎海外 | 成人午夜免费在线视频 | 国产91九色 | 国产在线精品一区二区三区 | 日韩综合网 | av网站免费 | 中文字幕 在线观看 | 日韩欧美国产精品综合嫩v 一区中文字幕 |