|NO.Z.00036|——————————|BigDataEnd|——|Hadoop&实时数仓.V16|——|项目.v16|ADS层处理|读取数据明细分析结果下沉到Redis|

一、ADS层处理
### --- ADS层梳理

~~~     ADS(Application Data Store 应用数据层)。
~~~     基于DW数据,整合汇总成主题域的服务数据,用于提供后续的业务查询等。
### --- 从数据明细层读分析结果数据到clickhouse、redis、druid等

二、编程实现
### --- 编程实现样例类:CityOrder

package modes

case class CityOrder(city:String, province:String, totalMoney:Double, totalCount:Int)
### --- 编程实现ADS数据分析写入到redis中 

package ads

import java.util

import modes.CityOrder
import org.apache.flink.configuration.Configuration
import org.apache.flink.streaming.api.functions.sink.{RichSinkFunction, SinkFunction}
import redis.clients.jedis.Jedis

/**
 * 自定义下沉器,下沉结果数据到Redis中
 */
class MySinkToRedis extends RichSinkFunction[Tuple2[CityOrder, Long]] {

  var jedis: Jedis = null

  override def open(parameters: Configuration): Unit = {
    jedis = new Jedis("hadoop03", 6379, 6000)
    //    jedis.auth("123456")
    jedis.select(0)
  }

  //(CityOrder(青岛市,山东省,8178.2,1),1608282220000)
  override def invoke(value: (CityOrder, Long), context: SinkFunction.Context[_]): Unit = {
//    println("value:" + value)
    if (!jedis.isConnected) {
      jedis.connect()
    }
   /* val str: String = "totalmoney:" + value._1.totalMoney.toString + "totalCount:" + value._1.totalCount.toString + "time:" + value._2.toString
    jedis.set(value._1.province + value._1.city.toString, str)*/

    val map = new util.HashMap[String, String]()
    map.put("totalMoney", value._1.totalMoney.toString)
    map.put("totalCount", value._1.totalCount.toString)
    map.put("time", value._2.toString)

    //    jedis.set("a","aa")
    //    print(value._1.totalMoney.toString + "totalCount",value._1.totalCount.toString + value._2.toString)
    if (!map.isEmpty) {
      println(value._1.province + value._1.city.toString + map.size() + map.get("totalMoney") + map.get("totalCount") + map.get("time"))
      try {

        jedis.hset(value._1.province + value._1.city.toString, map)
        map.clear()
      } catch {
        case e: Exception => print(e)
      }
    }
    //    jedis.hse7
  }

  override def close(): Unit = {
    jedis.close()
  }
}
三、统计结果下沉到redis中


 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(21)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示