WeafareStat.scala 12.2 KB
Newer Older
高雅喆's avatar
高雅喆 committed
1 2 3 4
package com.gmei

import java.io.Serializable

王志伟's avatar
王志伟 committed
5
import org.apache.spark.sql.{SaveMode}
高雅喆's avatar
高雅喆 committed
6 7
import org.apache.log4j.{Level, Logger}
import scopt.OptionParser
王志伟's avatar
王志伟 committed
8
import com.gmei.lib.AbstractParams
高雅喆's avatar
高雅喆 committed
9

高雅喆's avatar
高雅喆 committed
10
object WeafareStat {
高雅喆's avatar
高雅喆 committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

  Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
  Logger.getLogger("org.apache.eclipse.jetty.server").setLevel(Level.OFF)

  case class Params(env: String = "dev"
                   ) extends AbstractParams[Params] with Serializable

  val defaultParams = Params()

  val parser = new OptionParser[Params]("Feed_EDA") {
    head("WeafareStat")
    opt[String]("env")
      .text(s"the databases environment you used")
      .action((x, c) => c.copy(env = x))
    note(
      """
        |For example, the following command runs this app on a tidb dataset:
        |
        | spark-submit --class com.gmei.WeafareStat ./target/scala-2.11/feededa-assembly-0.1.jar \
      """.stripMargin +
        s"|   --env ${defaultParams.env}"
    )
  }


  def main(args: Array[String]): Unit = {
    parser.parse(args, defaultParams).map { param =>
      GmeiConfig.setup(param.env)
      val spark_env = GmeiConfig.getSparkSession()
      val sc = spark_env._2

王志伟's avatar
王志伟 committed
42 43 44 45
//      val ti = new TiContext(sc)
//      ti.tidbMapTable(dbName = "eagle",tableName = "diary_video")
//      ti.tidbMapTable(dbName = "jerry_prod",tableName = "data_feed_click")
//      ti.tidbMapTable(dbName = "eagle",tableName = "feed_diary_boost")
高雅喆's avatar
高雅喆 committed
46 47


48
      import sc.implicits._
高雅喆's avatar
高雅喆 committed
49 50
      val stat_date = GmeiConfig.getMinusNDate(1)
      println(stat_date)
高雅喆's avatar
高雅喆 committed
51 52 53
      val video_cids = sc.sql(
        s"""
           |select distinct(cid_id) as cid_id
王志伟's avatar
王志伟 committed
54
           |from jerry_prod.data_feed_click
高雅喆's avatar
高雅喆 committed
55
           |where cid_type = 'diary'
王志伟's avatar
王志伟 committed
56
           |and  cid_id  in (select cid from eagle.diary_video where stat_date='${stat_date}')
高雅喆's avatar
高雅喆 committed
57
           |and stat_date ='${stat_date}'
高雅喆's avatar
高雅喆 committed
58 59
         """.stripMargin
      )
王志伟's avatar
王志伟 committed
60
//      video_cids.show()
高雅喆's avatar
高雅喆 committed
61 62 63 64 65
      video_cids.createOrReplaceTempView("tmp1")

      val txt_cids = sc.sql(
        s"""
           |select distinct(cid_id) as cid_id
王志伟's avatar
王志伟 committed
66
           |from jerry_prod.data_feed_click
高雅喆's avatar
高雅喆 committed
67
           |where cid_type = 'diary'
王志伟's avatar
王志伟 committed
68
           |and  cid_id  not in (select cid from eagle.diary_video where stat_date='${stat_date}')
高雅喆's avatar
高雅喆 committed
69
           |and stat_date ='${stat_date}'
高雅喆's avatar
高雅喆 committed
70 71
         """.stripMargin
      )
王志伟's avatar
王志伟 committed
72
//      txt_cids.show()
高雅喆's avatar
高雅喆 committed
73 74
      txt_cids.createOrReplaceTempView("tmp2")

高雅喆's avatar
高雅喆 committed
75 76
      val partition_date = stat_date.replace("-","")
      println(partition_date)
77
      val video_meigou_count = sc.sql(
高雅喆's avatar
高雅喆 committed
78
        s"""
79
           |select '${stat_date}' as stat_date, count(page_name) as video_meigou_count
高雅喆's avatar
高雅喆 committed
80 81
           |from online.bl_hdfs_page_view_updates pv inner join tmp1
           |on pv.referrer_id = tmp1.cid_id
高雅喆's avatar
高雅喆 committed
82
           |where pv.partition_date = '${partition_date}'
高雅喆's avatar
高雅喆 committed
83 84 85 86
           |and pv.page_name='welfare_detail'
           |and pv.referrer='diary_detail'
         """.stripMargin
      )
王志伟's avatar
王志伟 committed
87
//      video_meigou_count.show()
高雅喆's avatar
高雅喆 committed
88

89
      val txt_meigou_count = sc.sql(
高雅喆's avatar
高雅喆 committed
90
        s"""
91
           |select '${stat_date}' as stat_date, count(page_name) as txt_meigou_count
高雅喆's avatar
高雅喆 committed
92 93
           |from online.bl_hdfs_page_view_updates pv inner join tmp2
           |on pv.referrer_id = tmp2.cid_id
高雅喆's avatar
高雅喆 committed
94
           |where pv.partition_date = '${partition_date}'
高雅喆's avatar
高雅喆 committed
95 96 97 98
           |and pv.page_name='welfare_detail'
           |and pv.referrer='diary_detail'
         """.stripMargin
      )
王志伟's avatar
王志伟 committed
99
//      txt_meigou_count.show()
100 101 102 103

      val video_clk_count = sc.sql(
        s"""
           |select '${stat_date}' as stat_date, count(cid_id) as video_clk_count
王志伟's avatar
王志伟 committed
104
           |from jerry_prod.data_feed_click
105
           |where cid_type = 'diary'
王志伟's avatar
王志伟 committed
106
           |and  cid_id  in (select cid from eagle.diary_video where stat_date='${stat_date}')
高雅喆's avatar
高雅喆 committed
107
           |and stat_date='${stat_date}'
108 109
         """.stripMargin
      )
王志伟's avatar
王志伟 committed
110
//      video_clk_count.show()
111 112 113 114

      val txt_clk_count = sc.sql(
        s"""
           |select '${stat_date}' as stat_date, count(cid_id) as txt_clk_count
王志伟's avatar
王志伟 committed
115
           |from jerry_prod.data_feed_click
116
           |where cid_type = 'diary'
王志伟's avatar
王志伟 committed
117
           |and  cid_id not in (select cid from eagle.diary_video where stat_date='${stat_date}')
118 119 120
           |and stat_date='${stat_date}'
         """.stripMargin
      )
王志伟's avatar
王志伟 committed
121
//      txt_clk_count.show()
122

王志伟's avatar
王志伟 committed
123 124
      val video_count = sc.sql(
        s"""
王志伟's avatar
王志伟 committed
125
           |select '${stat_date}' as stat_date,count(distinct(cid)) as video_count
王志伟's avatar
王志伟 committed
126
           |from eagle.diary_video where stat_date='${stat_date}'
王志伟's avatar
王志伟 committed
127 128
         """.stripMargin
      )
王志伟's avatar
王志伟 committed
129
//      video_count.show()
王志伟's avatar
王志伟 committed
130

王志伟's avatar
王志伟 committed
131 132 133 134 135 136 137
      val vlog_meigou_clk_count = sc.sql(
        s"""
           |select '${stat_date}' as stat_date,count(page_name) as vlog_meigou_clk_num
           |from online.bl_hdfs_page_view_updates
           |where partition_date='${partition_date}'
           |and page_name='welfare_detail'
           |and referrer='diary_detail'
王志伟's avatar
王志伟 committed
138
           |and referrer_id in (select distinct(diary_id) from eagle.feed_diary_boost)
王志伟's avatar
王志伟 committed
139 140
         """.stripMargin
      )
王志伟's avatar
王志伟 committed
141
//      vlog_meigou_clk_count.show()
王志伟's avatar
王志伟 committed
142 143 144 145 146


      val vlog_clk_count = sc.sql(
        s"""
           |select '${stat_date}' as stat_date,count(cid_id) as vlog_clk_num
王志伟's avatar
王志伟 committed
147
           |from jerry_prod.data_feed_click
王志伟's avatar
王志伟 committed
148 149
           |where stat_date='${stat_date}'
           |and  cid_type = 'diary'
王志伟's avatar
王志伟 committed
150
           |and cid_id in (select distinct(diary_id) from eagle.feed_diary_boost)
王志伟's avatar
王志伟 committed
151 152
         """.stripMargin
      )
王志伟's avatar
王志伟 committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
//      vlog_clk_count.show()

      //日记本转化美购
      //1.日记本到美购转化数
      val diary_meigou_count = sc.sql(
        s"""
           |select '${stat_date}' as stat_date, count(page_name) as diary_meigou_count
           |from online.bl_hdfs_page_view_updates
           |where partition_date = '${partition_date}'
           |and page_name='welfare_detail'
           |and referrer='diary_detail'
         """.stripMargin
      )
      //2.日记本点击数
      val diary_clk = sc.sql(
        s"""
           |select '${stat_date}' as stat_date,count(cl_id) as diary_clk
           |from online.tl_hdfs_maidian_view
           |where action = 'on_click_diary_card'
           |and cl_id != "NULL"
           |and partition_date='${partition_date}'
       """.stripMargin
      )



      //3.日记本曝光数
      val diary_expoure=sc.sql(
        s"""
           |select '${stat_date}' as stat_date,count(cl_id) as diary_expoure
           |from online.ml_community_exposure_detail
           |where business_type = "diary"
           |and cl_id != "NULL"
           |and partition_date='${partition_date}'
       """.stripMargin
      )
王志伟's avatar
王志伟 committed
189 190


191 192 193
      val result = video_clk_count.join(video_meigou_count,"stat_date")
        .join(txt_clk_count,"stat_date")
        .join(txt_meigou_count,"stat_date")
王志伟's avatar
王志伟 committed
194
        .join(video_count,"stat_date")
王志伟's avatar
王志伟 committed
195 196
        .join(vlog_meigou_clk_count,"stat_date")
        .join(vlog_clk_count,"stat_date")
王志伟's avatar
王志伟 committed
197 198 199
        .join(diary_meigou_count,"stat_date")
        .join(diary_clk,"stat_date")
        .join(diary_expoure,"stat_date")
王志伟's avatar
王志伟 committed
200

201

王志伟's avatar
王志伟 committed
202 203 204
      val result1 = result.withColumn("video_meigou_rate",result.col("video_meigou_count")/result.col("video_clk_count"))
      val result2 = result1.withColumn("txt_meigou_rate",result.col("txt_meigou_count")/result.col("txt_clk_count"))
      val result3 = result2.withColumn("vlog_meigou_rate",result.col("vlog_meigou_clk_num")/result.col("vlog_clk_num"))
王志伟's avatar
王志伟 committed
205 206
      val result4=result3.withColumn("diary_meigou_rate",result.col("diary_meigou_count")/result.col("diary_clk"))
      val result5 =result4.withColumn("diary_expoure_meigou_rate",result.col("diary_meigou_count")/result.col("diary_expoure"))
207

王志伟's avatar
王志伟 committed
208
      result5.show()
王志伟's avatar
王志伟 committed
209 210 211 212
//      GmeiConfig.writeToJDBCTable(result5, "diary_meigou_cvr", SaveMode.Append)
      println("开始写入")
      GmeiConfig.writeToJDBCTable("jerry.jdbcuri",result5, table="diary_meigou_cvr",SaveMode.Append)
      println("写入完成")
高雅喆's avatar
高雅喆 committed
213 214 215 216 217 218 219 220 221


      sc.stop()

    }
  }


}
高雅喆's avatar
高雅喆 committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251





object NdDataInput {

  Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
  Logger.getLogger("org.apache.eclipse.jetty.server").setLevel(Level.OFF)

  case class Params(env: String = "dev"
                   ) extends AbstractParams[Params] with Serializable

  val defaultParams = Params()

  val parser = new OptionParser[Params]("Feed_EDA") {
    head("WeafareStat")
    opt[String]("env")
      .text(s"the databases environment you used")
      .action((x, c) => c.copy(env = x))
    note("winter is coming")
  }


  def main(args: Array[String]): Unit = {
    parser.parse(args, defaultParams).map { param =>
      GmeiConfig.setup(param.env)
      val spark_env = GmeiConfig.getSparkSession()
      val sc = spark_env._2

王志伟's avatar
王志伟 committed
252 253 254 255
//      val ti = new TiContext(sc)
//      ti.tidbMapTable(dbName = "jerry_prod", tableName = "nd_data_meigou_cid")
//      ti.tidbMapTable(dbName = "jerry_prod", tableName = "data_feed_click")
//      ti.tidbMapTable(dbName = "eagle", tableName = "feed_diary_boost")
高雅喆's avatar
高雅喆 committed
256

高雅喆's avatar
高雅喆 committed
257 258
      val date8 = GmeiConfig.getMinusNDate(70)
      val result00 = sc.sql(
高雅喆's avatar
高雅喆 committed
259 260
        s"""
           |SELECT
高雅喆's avatar
高雅喆 committed
261
           | split(service_id,'\\\\|')[1] as sid,split(cid,'\\\\|')[1] as cid
王志伟's avatar
王志伟 committed
262
           |FROM jerry_prod.nd_data_meigou_cid
高雅喆's avatar
高雅喆 committed
263 264 265
           |where stat_date > '${date8}'
     """.stripMargin
      )
高雅喆's avatar
高雅喆 committed
266 267 268
      result00.createOrReplaceTempView("tmp1")
      result00.show()
      println(result00.count())
高雅喆's avatar
高雅喆 committed
269

高雅喆's avatar
高雅喆 committed
270 271 272 273 274 275
      val yesteday = GmeiConfig.getMinusNDate(1).replace("-","")
      val result01 = sc.sql(
        s"""
           |select a.sid as sid, a.cid as cid, b.tag_id as ctag_id, c.level1_id as clevel1_id
           |from tmp1 a
           |left join online.tl_hdfs_diary_tags_view b on a.cid=b.diary_id
高雅喆's avatar
高雅喆 committed
276
           |left join online.bl_tag_hierarchy_detail c on b.tag_id=c.id
高雅喆's avatar
高雅喆 committed
277 278 279 280 281 282 283
           |where b.partition_date='${yesteday}'
           |and c.partition_date='${yesteday}'
         """.stripMargin
      )
      result01.createOrReplaceTempView("tmp2")
      result01.show()
      println(result01.count())
高雅喆's avatar
高雅喆 committed
284

高雅喆's avatar
高雅喆 committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
      val result02 = sc.sql(
        s"""
           |select a.sid as sid, a.cid as cid, a.ctag_id as ctag_id, a.clevel1_id as clevel1_id,
           | b.tag_id as stag_id, c.level1_id as slevel1_id
           |from tmp2 a
           |left join online.tl_meigou_servicetag_view b on a.sid=b.service_id
           |left join online.bl_tag_hierarchy_detail c on b.tag_id=c.id
           |where b.partition_date='${yesteday}'
           |and c.partition_date='${yesteday}'
         """.stripMargin
      )
      result02.createOrReplaceTempView("tmp3")
      result02.show()
      println(result02.count())


高雅喆's avatar
高雅喆 committed
301
      val tidb_input = sc.sql(
高雅喆's avatar
高雅喆 committed
302 303 304 305 306 307
        s"""
           |select sid as service_id,cid
           |from tmp3
           |where clevel1_id = slevel1_id
         """.stripMargin
      )
高雅喆's avatar
高雅喆 committed
308 309
      tidb_input.show()
      println(tidb_input.count())
高雅喆's avatar
高雅喆 committed
310

高雅喆's avatar
高雅喆 committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    }
  }
}




object ServiceStat {

  Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
  Logger.getLogger("org.apache.eclipse.jetty.server").setLevel(Level.OFF)

  case class Params(env: String = "dev"
                   ) extends AbstractParams[Params] with Serializable

  val defaultParams = Params()
高雅喆's avatar
高雅喆 committed
327

高雅喆's avatar
高雅喆 committed
328 329 330 331 332 333 334
  val parser = new OptionParser[Params]("Feed_EDA") {
    head("WeafareStat")
    opt[String]("env")
      .text(s"the databases environment you used")
      .action((x, c) => c.copy(env = x))
    note("winter is coming")
  }
高雅喆's avatar
高雅喆 committed
335

高雅喆's avatar
高雅喆 committed
336

高雅喆's avatar
高雅喆 committed
337 338 339 340 341 342
  def main(args: Array[String]): Unit = {
    parser.parse(args, defaultParams).map { param =>
      GmeiConfig.setup(param.env)
      val spark_env = GmeiConfig.getSparkSession()
      val sc = spark_env._2

王志伟's avatar
王志伟 committed
343 344 345 346
//      val ti = new TiContext(sc)
//      ti.tidbMapTable(dbName = "jerry_prod", tableName = "nd_data_meigou_cid")
//      ti.tidbMapTable(dbName = "jerry_prod", tableName = "data_feed_click")
//      ti.tidbMapTable(dbName = "eagle", tableName = "feed_diary_boost")
高雅喆's avatar
高雅喆 committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

      val result00 = sc.sql(
        s"""
           |select a.cl_id as device_id,
           |COALESCE(a.params['diary_id'], a.params['business_id'], 0) as diary_id,
           |c.level1_id as level1_id
           |from online.tl_hdfs_maidian_view a
           |left join online.tl_hdfs_diary_tags_view b on COALESCE(a.params['diary_id'], a.params['business_id'], 0)=b.diary_id
           |left join online.bl_tag_hierarchy_detail c on b.tag_id=c.id
           |where a.partition_date > "20181112"
           |and a.action="on_click_diary_card"
           |and a.params["page_name"]="home"
           |and a.cl_id != "NULL"
           |and b.partition_date="20181119"
           |and c.partition_date="20181119"
     """.stripMargin
      )
高雅喆's avatar
高雅喆 committed
364
      result00.collect.foreach(println)
高雅喆's avatar
高雅喆 committed
365

高雅喆's avatar
高雅喆 committed
366 367
    }
  }
高雅喆's avatar
高雅喆 committed
368
}