EsmmData.scala 21.4 KB
Newer Older
高雅喆's avatar
高雅喆 committed
1 2 3 4 5 6 7 8 9
package com.gmei


import java.io.Serializable

import org.apache.spark.sql.{SaveMode, TiContext}
import org.apache.log4j.{Level, Logger}
import scopt.OptionParser
import com.gmei.lib.AbstractParams
高雅喆's avatar
高雅喆 committed
10
import org.apache.spark.sql.functions.lit
高雅喆's avatar
高雅喆 committed
11

12 13
import scala.util.Try

高雅喆's avatar
高雅喆 committed
14 15 16 17 18 19 20


object EsmmData {

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

21 22
  case class Params(env: String = "dev",
                    date: String = GmeiConfig.getMinusNDate(1)
高雅喆's avatar
高雅喆 committed
23 24 25 26 27 28 29 30 31
                   ) extends AbstractParams[Params] with Serializable

  val defaultParams = Params()

  val parser = new OptionParser[Params]("Feed_EDA") {
    head("EsmmData")
    opt[String]("env")
      .text(s"the databases environment you used")
      .action((x, c) => c.copy(env = x))
32 33 34
    opt[String]("date")
        .text(s"the date you used")
        .action((x,c) => c.copy(date = x))
高雅喆's avatar
高雅喆 committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    note(
      """
        |For example, the following command runs this app on a tidb dataset:
        |
        | spark-submit --class com.gmei.EsmmData ./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

      val ti = new TiContext(sc)
      ti.tidbMapTable(dbName = "eagle",tableName = "src_mimas_prod_api_diary_tags")
高雅喆's avatar
高雅喆 committed
55
      ti.tidbMapTable(dbName = "eagle",tableName = "src_zhengxing_api_tag")
高雅喆's avatar
高雅喆 committed
56 57
      ti.tidbMapTable(dbName = "jerry_prod",tableName = "data_feed_click")
      ti.tidbMapTable(dbName = "jerry_prod",tableName = "data_feed_exposure")
高雅喆's avatar
高雅喆 committed
58
      ti.tidbMapTable(dbName = "jerry_test", tableName = "esmm_train_data")
高雅喆's avatar
高雅喆 committed
59 60


高雅喆's avatar
高雅喆 committed
61
      val max_stat_date = sc.sql(
高雅喆's avatar
高雅喆 committed
62
        s"""
高雅喆's avatar
高雅喆 committed
63
           |select max(stat_date) from esmm_train_data
高雅喆's avatar
高雅喆 committed
64
         """.stripMargin
65
      )
高雅喆's avatar
高雅喆 committed
66 67 68
      val max_stat_date_str = max_stat_date.collect().map(s => s(0).toString).head
      println("max_stat_date_str",max_stat_date_str)
      println("param.date",param.date)
高雅喆's avatar
高雅喆 committed
69
      if (max_stat_date_str != param.date){
高雅喆's avatar
高雅喆 committed
70 71
        val stat_date = param.date
        println(stat_date)
72 73 74 75 76 77 78 79 80 81
//        val imp_data = sc.sql(
//          s"""
//             |select distinct stat_date,device_id,city_id as ucity_id,
//             |  cid_id,diary_service_id
//             |from data_feed_exposure
//             |where cid_type = 'diary'
//             |and stat_date ='${stat_date}'
//         """.stripMargin
//        )

高雅喆's avatar
高雅喆 committed
82 83
        val imp_data = sc.sql(
          s"""
84 85
             |select * from
             |(select stat_date,device_id,city_id as ucity_id,cid_id,diary_service_id
高雅喆's avatar
高雅喆 committed
86 87 88
             |from data_feed_exposure
             |where cid_type = 'diary'
             |and stat_date ='${stat_date}'
89
             |group by stat_date,device_id,city_id,cid_id,diary_service_id having count(*) > 1) a
高雅喆's avatar
高雅喆 committed
90
         """.stripMargin
高雅喆's avatar
高雅喆 committed
91 92 93 94 95 96 97 98 99 100 101 102 103
        )
        //      imp_data.show()
        //      println("imp_data.count()")
        //      println(imp_data.count())


        val clk_data = sc.sql(
          s"""
             |select distinct stat_date,device_id,city_id as ucity_id,
             |  cid_id,diary_service_id
             |from data_feed_click
             |where cid_type = 'diary'
             |and stat_date ='${stat_date}'
高雅喆's avatar
高雅喆 committed
104
         """.stripMargin
高雅喆's avatar
高雅喆 committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        )
        //      clk_data.show()
        //      println("clk_data.count()")
        //      println(clk_data.count())



        val imp_data_filter = imp_data.except(clk_data).withColumn("y",lit(0)).withColumn("z",lit(0))
        //      imp_data_filter.createOrReplaceTempView("imp_data_filter")
        //      imp_data_filter.show()
        //      println("imp_data_filter.count()")
        //      println(imp_data_filter.count())


        val stat_date_not = stat_date.replace("-","")
        val cvr_data = sc.sql(
          s"""
             |select distinct
             |  from_unixtime(unix_timestamp(partition_date ,'yyyyMMdd'), 'yyyy-MM-dd') as stat_date,
             |  cl_id as device_id,city_id as ucity_id,
             |  params["referrer_id"] as cid_id,params["business_id"] as diary_service_id
             |from online.tl_hdfs_maidian_view
             |where action='page_view'
             |and partition_date ='${stat_date_not}'
             |and params['page_name'] = 'welfare_detail'
             |and params['referrer'] = 'diary_detail'
         """.stripMargin
        )
高雅喆's avatar
高雅喆 committed
133

高雅喆's avatar
高雅喆 committed
134 135 136 137 138
        val cvr_data_filter = cvr_data.withColumn("y",lit(1)).withColumn("z",lit(1))
        //      cvr_data_filter.createOrReplaceTempView("cvr_data_filter")
        //      cvr_data_filter.show()
        //      println("cvr_data_filter.count()")
        //      println(cvr_data_filter.count())
高雅喆's avatar
高雅喆 committed
139 140


高雅喆's avatar
高雅喆 committed
141

高雅喆's avatar
高雅喆 committed
142 143 144 145 146
        val clk_data_filter =clk_data.except(cvr_data).withColumn("y",lit(1)).withColumn("z",lit(0))
        //      clk_data_filter.createOrReplaceTempView("clk_data_filter")
        //      clk_data_filter.show()
        //      println("clk_data_filter.count()")
        //      println(clk_data_filter.count())
高雅喆's avatar
高雅喆 committed
147 148


高雅喆's avatar
高雅喆 committed
149 150 151 152 153
        val union_data = imp_data_filter.union(clk_data_filter).union(cvr_data_filter)
        union_data.createOrReplaceTempView("union_data")
        //      union_data.show()
        //      println("union_data.count()")
        //      println(union_data.count())
高雅喆's avatar
高雅喆 committed
154 155


156

高雅喆's avatar
高雅喆 committed
157 158 159 160 161 162 163 164 165
        val union_data_clabel = sc.sql(
          s"""
             |select a.stat_date,a.device_id,a.ucity_id,a.cid_id,a.diary_service_id,a.y,a.z,
             |  c.level1_id as clevel1_id
             |from union_data a
             |left join online.tl_hdfs_diary_tags_view b on a.cid_id=b.diary_id
             |left join online.bl_tag_hierarchy_detail c on b.tag_id=c.id
             |where b.partition_date='${stat_date_not}'
             |and c.partition_date='${stat_date_not}'
高雅喆's avatar
高雅喆 committed
166
         """.stripMargin
高雅喆's avatar
高雅喆 committed
167 168 169 170 171 172 173 174 175 176 177 178 179
        )
        union_data_clabel.createOrReplaceTempView("union_data_clabel")
        //      union_data_clabel.show()

        val union_data_slabel = sc.sql(
          s"""
             |select a.stat_date,a.device_id,a.ucity_id,a.cid_id,a.diary_service_id,a.y,a.z,a.clevel1_id,
             |  c.level1_id as slevel1_id
             |from union_data_clabel a
             |left join online.tl_meigou_servicetag_view b on a.diary_service_id=b.service_id
             |left join online.bl_tag_hierarchy_detail c on b.tag_id=c.id
             |where b.partition_date='${stat_date_not}'
             |and c.partition_date='${stat_date_not}'
高雅喆's avatar
高雅喆 committed
180
         """.stripMargin
高雅喆's avatar
高雅喆 committed
181 182 183 184 185 186 187 188 189 190 191 192 193
        )
        union_data_slabel.createOrReplaceTempView("union_data_slabel")
        //      union_data_slabel.show()


        val union_data_ccity_name = sc.sql(
          s"""
             |select a.stat_date,a.device_id,a.ucity_id,a.cid_id,a.diary_service_id,a.y,a.z,a.clevel1_id,a.slevel1_id,
             |  c.name as ccity_name
             |from union_data_slabel a
             |left join src_mimas_prod_api_diary_tags b on a.cid_id=b.diary_id
             |left join src_zhengxing_api_tag c on b.tag_id=c.id
             | where c.tag_type=4
高雅喆's avatar
高雅喆 committed
194
         """.stripMargin
高雅喆's avatar
高雅喆 committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        )
        union_data_ccity_name.createOrReplaceTempView("union_data_ccity_name")
        //      union_data_ccity_name.show()

        val union_data_scity_id = sc.sql(
          s"""
             |select a.stat_date,a.device_id,a.ucity_id,a.cid_id,a.diary_service_id,a.y,a.z,a.clevel1_id,a.slevel1_id,a.ccity_name,
             |  d.city_id as scity_id
             |from union_data_ccity_name a
             |left join online.tl_meigou_service_view b on a.diary_service_id=b.id
             |left join online.tl_hdfs_doctor_view c on b.doctor_id=c.id
             |left join online.tl_hdfs_hospital_view d on c.hospital_id=d.id
             |where b.partition_date='${stat_date_not}'
             |and c.partition_date='${stat_date_not}'
             |and d.partition_date='${stat_date_not}'
高雅喆's avatar
高雅喆 committed
210
         """.stripMargin
高雅喆's avatar
高雅喆 committed
211 212 213
        )
        //      union_data_scity_id.createOrReplaceTempView("union_data_scity_id")
        union_data_scity_id.show()
高雅喆's avatar
高雅喆 committed
214
        GmeiConfig.writeToJDBCTable("jdbc:mysql://10.66.157.22:4000/jerry_test?user=root&password=3SYz54LS9#^9sBvC&rewriteBatchedStatements=true",union_data_scity_id, table="esmm_train_data",SaveMode.Append)
高雅喆's avatar
高雅喆 committed
215 216 217 218

      } else {
        println("esmm_train_data already have param.date data")
      }
高雅喆's avatar
高雅喆 committed
219 220 221 222 223 224 225

      sc.stop()

    }
  }

}
高雅喆's avatar
高雅喆 committed
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 252 253 254 255 256 257 258 259 260 261 262 263 264 265



object EsmmPredData {

  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("EsmmData")
    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.EsmmData ./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

      val ti = new TiContext(sc)
      ti.tidbMapTable(dbName = "eagle",tableName = "src_mimas_prod_api_diary_tags")
      ti.tidbMapTable(dbName = "eagle",tableName = "src_zhengxing_api_tag")
      ti.tidbMapTable(dbName = "jerry_prod",tableName = "data_feed_exposure")
266
      ti.tidbMapTable(dbName = "jerry_prod",tableName = "data_feed_click")
267 268 269 270
      ti.tidbMapTable("jerry_prod", "nd_device_cid_similarity_matrix")
      ti.tidbMapTable("eagle","ffm_diary_queue")
      ti.tidbMapTable("eagle","search_queue")
      ti.tidbMapTable(dbName = "jerry_test",tableName = "esmm_train_data")
271
      ti.tidbMapTable("eagle","biz_feed_diary_queue")
高雅喆's avatar
高雅喆 committed
272

273
      import sc.implicits._
高雅喆's avatar
高雅喆 committed
274

275
      val yesteday_have_seq = GmeiConfig.getMinusNDate(1)
276

277
      //nearby_data
278
      val raw_data = sc.sql(
279
        s"""
高雅喆's avatar
高雅喆 committed
280
           |select concat(tmp1.device_id,",",tmp1.city_id) as device_city, tmp1.merge_queue from
高雅喆's avatar
高雅喆 committed
281
           |(select device_id,if(city_id='world','worldwide',city_id) city_id,similarity_cid as merge_queue from nd_device_cid_similarity_matrix
282
           |union
高雅喆's avatar
高雅喆 committed
283
           |select device_id,if(city_id='world','worldwide',city_id) city_id,native_queue as merge_queue from ffm_diary_queue
284
           |union
高雅喆's avatar
高雅喆 committed
285
           |select device_id,city_id,search_queue as merge_queue from search_queue) as tmp1
286
           |where tmp1.device_id in (select distinct device_id from data_feed_click where stat_date='${yesteday_have_seq}')
287
         """.stripMargin
288
      )
高雅喆's avatar
高雅喆 committed
289
      raw_data.show()
高雅喆's avatar
高雅喆 committed
290

高雅喆's avatar
高雅喆 committed
291

292 293
      val raw_data1 = raw_data.rdd.groupBy(_.getAs[String]("device_city")).map {
        case (device_city, cid_data) =>
高雅喆's avatar
高雅喆 committed
294 295
          val device_id = Try(device_city.split(",")(0)).getOrElse("")
          val city_id = Try(device_city.split(",")(1)).getOrElse("")
296
          val cids = Try(cid_data.toSeq.map(_.getAs[String]("merge_queue").split(",")).flatMap(_.zipWithIndex).sortBy(_._2).map(_._1).distinct.take(500).mkString(",")).getOrElse("")
297 298
          (device_id,city_id ,s"$cids")
      }.filter(_._3!="").toDF("device_id","city_id","merge_queue")
高雅喆's avatar
高雅喆 committed
299
      raw_data1.createOrReplaceTempView("raw_data1")
300
      println("nearby_device_count",raw_data1.count())
高雅喆's avatar
高雅喆 committed
301 302 303

      val raw_data2 = sc.sql(
        s"""
304
           |select device_id,city_id as ucity_id,explode(split(merge_queue, ',')) as cid_id from raw_data1
高雅喆's avatar
高雅喆 committed
305
         """.stripMargin
306
      ).withColumn("label",lit(1))
高雅喆's avatar
高雅喆 committed
307
      raw_data2.createOrReplaceTempView("raw_data2")
308
      println("nearby_explode_count",raw_data2.count())
高雅喆's avatar
高雅喆 committed
309

310 311 312

      // native_data
      val native_data = sc.sql(
高雅喆's avatar
高雅喆 committed
313
        s"""
314
           |select distinct a.device_id,a.city_id,b.native_queue from data_feed_click a
高雅喆's avatar
高雅喆 committed
315 316
           |left join (select if(city_id='world','worldwide',city_id) city_id,native_queue from biz_feed_diary_queue) b
           |on a.city_id = b.city_id
317
           |where a.stat_date='${yesteday_have_seq}' and b.native_queue != ""
高雅喆's avatar
高雅喆 committed
318
         """.stripMargin
319 320 321
      )
      native_data.createOrReplaceTempView("native_data")
      println("native_device_count",native_data.count())
322

323 324 325
      val native_data1 = sc.sql(
        s"""
           |select device_id,city_id as ucity_id,
326
           |explode(split(split(native_queue, concat(',',split(native_queue,',')[500]))[0],',')) as cid_id
327 328 329 330 331
           |from native_data
         """.stripMargin
      ).withColumn("label",lit(0))
      native_data1.createOrReplaceTempView("native_data1")
      println("native_explode_count",native_data1.count())
332 333


高雅喆's avatar
高雅喆 committed
334

335 336 337 338 339
      //union
      val union_data = sc.sql(
        s"""
           |select device_id,ucity_id,cid_id,label from native_data1
           |union
高雅喆's avatar
高雅喆 committed
340
           |select device_id,ucity_id,cid_id,label from raw_data2
341 342 343 344
         """.stripMargin
      )
      union_data.createOrReplaceTempView("raw_data")
      println("union_count",union_data.count())
345

高雅喆's avatar
高雅喆 committed
346

347
      //join feat
348
      val yesteday = GmeiConfig.getMinusNDate(1).replace("-","")
高雅喆's avatar
高雅喆 committed
349 350 351 352
      val sid_data = sc.sql(
        s"""
           |select distinct
           |  from_unixtime(unix_timestamp(partition_date ,'yyyyMMdd'), 'yyyy-MM-dd') as stat_date,
353
           |  a.device_id,a.ucity_id,a.cid_id,a.label, b.service_id as diary_service_id
高雅喆's avatar
高雅喆 committed
354 355 356 357 358
           |from raw_data a
           |left join online.ml_community_diary_updates b on a.cid_id = b.diary_id
           |where b.partition_date = '${yesteday}'
         """.stripMargin
      )
高雅喆's avatar
高雅喆 committed
359 360
//      sid_data.show()
      println(sid_data.count())
高雅喆's avatar
高雅喆 committed
361

高雅喆's avatar
高雅喆 committed
362 363
      val sid_data_label = sid_data.withColumn("y",lit(0)).withColumn("z",lit(0))
      sid_data_label.createOrReplaceTempView("union_data")
高雅喆's avatar
高雅喆 committed
364 365


高雅喆's avatar
高雅喆 committed
366 367
      val union_data_clabel = sc.sql(
        s"""
368
           |select a.stat_date,a.device_id,a.ucity_id,a.cid_id,a.label,a.diary_service_id,a.y,a.z,
高雅喆's avatar
高雅喆 committed
369 370 371 372 373 374 375 376 377 378
           |  c.level1_id as clevel1_id
           |from union_data a
           |left join online.tl_hdfs_diary_tags_view b on a.cid_id=b.diary_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
      )
      union_data_clabel.createOrReplaceTempView("union_data_clabel")
      //      union_data_clabel.show()
高雅喆's avatar
高雅喆 committed
379

高雅喆's avatar
高雅喆 committed
380 381
      val union_data_slabel = sc.sql(
        s"""
382
           |select a.stat_date,a.device_id,a.ucity_id,a.cid_id,a.label,a.diary_service_id,a.y,a.z,a.clevel1_id,
高雅喆's avatar
高雅喆 committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396
           |  c.level1_id as slevel1_id
           |from union_data_clabel a
           |left join online.tl_meigou_servicetag_view b on a.diary_service_id=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
      )
      union_data_slabel.createOrReplaceTempView("union_data_slabel")
      //      union_data_slabel.show()


      val union_data_ccity_name = sc.sql(
        s"""
397
           |select a.stat_date,a.device_id,a.ucity_id,a.cid_id,a.label,a.diary_service_id,a.y,a.z,a.clevel1_id,a.slevel1_id,
高雅喆's avatar
高雅喆 committed
398 399 400 401 402 403 404 405 406
           |  c.name as ccity_name
           |from union_data_slabel a
           |left join src_mimas_prod_api_diary_tags b on a.cid_id=b.diary_id
           |left join src_zhengxing_api_tag c on b.tag_id=c.id
           | where c.tag_type=4
         """.stripMargin
      )
      union_data_ccity_name.createOrReplaceTempView("union_data_ccity_name")
      //      union_data_ccity_name.show()
高雅喆's avatar
高雅喆 committed
407

高雅喆's avatar
高雅喆 committed
408 409
      val union_data_scity_id = sc.sql(
        s"""
410
           |select a.stat_date,a.device_id,a.ucity_id,a.cid_id,a.label,a.diary_service_id,a.y,a.z,a.clevel1_id,a.slevel1_id,a.ccity_name,
高雅喆's avatar
高雅喆 committed
411 412 413 414 415 416 417 418 419 420 421
           |  d.city_id as scity_id
           |from union_data_ccity_name a
           |left join online.tl_meigou_service_view b on a.diary_service_id=b.id
           |left join online.tl_hdfs_doctor_view c on b.doctor_id=c.id
           |left join online.tl_hdfs_hospital_view d on c.hospital_id=d.id
           |where b.partition_date='${yesteday}'
           |and c.partition_date='${yesteday}'
           |and d.partition_date='${yesteday}'
         """.stripMargin
      )
      //      union_data_scity_id.createOrReplaceTempView("union_data_scity_id")
高雅喆's avatar
高雅喆 committed
422
      println(union_data_scity_id.count())
高雅喆's avatar
高雅喆 committed
423
      GmeiConfig.writeToJDBCTable("jdbc:mysql://10.66.157.22:4000/jerry_test?user=root&password=3SYz54LS9#^9sBvC&rewriteBatchedStatements=true",union_data_scity_id, table="esmm_pre_data",SaveMode.Overwrite)
高雅喆's avatar
高雅喆 committed
424 425 426 427




高雅喆's avatar
高雅喆 committed
428 429 430 431 432 433 434 435
      sc.stop()

    }
  }

}


高雅喆's avatar
高雅喆 committed
436
object GetDiaryPortrait {
高雅喆's avatar
高雅喆 committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

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

  case class Params(env: String = "dev",
                    date: String = GmeiConfig.getMinusNDate(1)
                   ) extends AbstractParams[Params] with Serializable

  val defaultParams = Params()

  val parser = new OptionParser[Params]("Feed_EDA") {
    head("EsmmData")
    opt[String]("env")
      .text(s"the databases environment you used")
      .action((x, c) => c.copy(env = x))
    opt[String]("date")
      .text(s"the date you used")
      .action((x,c) => c.copy(date = x))
    note(
      """
        |For example, the following command runs this app on a tidb dataset:
        |
        | spark-submit --class com.gmei.EsmmData ./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

      val ti = new TiContext(sc)
      ti.tidbMapTable(dbName = "jerry_prod",tableName = "data_feed_click")

高雅喆's avatar
高雅喆 committed
476
      val stat_date = param.date.replace("-","")
高雅喆's avatar
高雅喆 committed
477

高雅喆's avatar
高雅喆 committed
478 479
      val diary_tag = sc.sql(
        s"""
高雅喆's avatar
高雅喆 committed
480
           |select c.diary_id,
高雅喆's avatar
高雅喆 committed
481 482 483
           |		concat_ws(';',collect_set(cast(c.level1_id as string))) as level1_ids,
           |		concat_ws(';',collect_set(cast(c.level2_id as string))) as level2_ids,
           |		concat_ws(';',collect_set(cast(c.level3_id as string))) as level3_ids from
高雅喆's avatar
高雅喆 committed
484
           |	(select a.diary_id,b.level1_id,b.level2_id,b.level3_id
高雅喆's avatar
高雅喆 committed
485 486
           |		from online.tl_hdfs_diary_tags_view a
           |		left join online.bl_tag_hierarchy_detail b
高雅喆's avatar
高雅喆 committed
487
           |		on a.tag_id = b.id
高雅喆's avatar
高雅喆 committed
488 489
           |		where a.partition_date = '${stat_date}'
           |		and b.partition_date = '${stat_date}') c
高雅喆's avatar
高雅喆 committed
490
           |	group by c.diary_id
高雅喆's avatar
高雅喆 committed
491 492 493
         """.stripMargin
      )
      diary_tag.show()
高雅喆's avatar
高雅喆 committed
494 495
      println(diary_tag.count())

高雅喆's avatar
高雅喆 committed
496
      GmeiConfig.writeToJDBCTable(diary_tag,"diary_feat",SaveMode.Overwrite)
高雅喆's avatar
高雅喆 committed
497 498


高雅喆's avatar
高雅喆 committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
      sc.stop()

    }
  }

}

object GetDevicePortrait {

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

  case class Params(env: String = "dev",
                    date: String = GmeiConfig.getMinusNDate(1)
                   ) extends AbstractParams[Params] with Serializable

  val defaultParams = Params()

  val parser = new OptionParser[Params]("Feed_EDA") {
    head("EsmmData")
    opt[String]("env")
      .text(s"the databases environment you used")
      .action((x, c) => c.copy(env = x))
    opt[String]("date")
      .text(s"the date you used")
      .action((x,c) => c.copy(date = x))
    note(
      """
        |For example, the following command runs this app on a tidb dataset:
        |
        | spark-submit --class com.gmei.EsmmData ./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

      val ti = new TiContext(sc)
      ti.tidbMapTable(dbName = "jerry_prod",tableName = "data_feed_click")
      ti.tidbMapTable(dbName = "jerry_prod",tableName = "diary_feat")

      val stat_date = param.date.replace("-","")

      val device_search_tag = sc.sql(
        s"""
高雅喆's avatar
高雅喆 committed
551
           |select c.device_id,c.stat_date,c.level1_id,count(c.level1_id) as level1_count
高雅喆's avatar
高雅喆 committed
552 553 554
           |from (select
           |		a.cl_id as device_id,a.partition_date as stat_date,
           |		COALESCE(a.params['diary_id'], a.params['business_id'], 0) as cid_id,
高雅喆's avatar
高雅喆 committed
555
           |		b.level1_ids as level1_id
高雅喆's avatar
高雅喆 committed
556 557 558 559
           |	from online.tl_hdfs_maidian_view a
           |	left join diary_feat b
           |	on COALESCE(a.params['diary_id'], a.params['business_id'], 0) = b.diary_id
           |	where
高雅喆's avatar
高雅喆 committed
560
           |    b.level1_ids is not null and
高雅喆's avatar
高雅喆 committed
561
           |		a.partition_date = '${stat_date}'
高雅喆's avatar
高雅喆 committed
562
           |		and (a.action = 'on_click_diary_card' or (a.action="full_stack_click_video_card_full_screen_play" and a.params["card_type"]="diary"))) c
高雅喆's avatar
高雅喆 committed
563
           |group by c.device_id,c.level1_id,c.stat_date
高雅喆's avatar
高雅喆 committed
564 565 566 567 568
         """.stripMargin
      )
      device_search_tag.show()
      println(device_search_tag.count())

高雅喆's avatar
高雅喆 committed
569 570
      device_search_tag.createOrReplaceTempView("tag_count")

高雅喆's avatar
高雅喆 committed
571
      val max_count_tag = sc.sql(
高雅喆's avatar
高雅喆 committed
572
      s"""
高雅喆's avatar
高雅喆 committed
573
         |select a.device_id,a.stat_date,a.level1_id,a.level1_count
高雅喆's avatar
高雅喆 committed
574 575 576 577
         |from tag_count a
         |inner join
         |(select device_id,max(level1_count) as max_count from tag_count group by device_id) b
         |on a.level1_count = b.max_count and a.device_id = b.device_id
高雅喆's avatar
高雅喆 committed
578 579
       """.stripMargin
    )
高雅喆's avatar
高雅喆 committed
580 581
      max_count_tag.show()
      println(max_count_tag.count())
高雅喆's avatar
高雅喆 committed
582 583


高雅喆's avatar
高雅喆 committed
584 585 586
//      GmeiConfig.writeToJDBCTable(diary_tag,"diary_feat",SaveMode.Overwrite)


高雅喆's avatar
高雅喆 committed
587 588 589 590 591 592
      sc.stop()

    }
  }

}