EsmmData.scala 24.1 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


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")
265
      ti.tidbMapTable(dbName = "jerry_prod",tableName = "data_feed_click")
266 267 268 269
      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")
270
      ti.tidbMapTable("eagle","biz_feed_diary_queue")
高雅喆's avatar
高雅喆 committed
271

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

274
      val yesteday_have_seq = GmeiConfig.getMinusNDate(1)
275

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

高雅喆's avatar
高雅喆 committed
290

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

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

309 310 311

      // native_data
      val native_data = sc.sql(
高雅喆's avatar
高雅喆 committed
312
        s"""
313
           |select distinct a.device_id,a.city_id,b.native_queue from data_feed_click a
高雅喆's avatar
高雅喆 committed
314 315
           |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
316
           |where a.stat_date='${yesteday_have_seq}' and b.native_queue != ""
高雅喆's avatar
高雅喆 committed
317
         """.stripMargin
318 319 320
      )
      native_data.createOrReplaceTempView("native_data")
      println("native_device_count",native_data.count())
321

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


高雅喆's avatar
高雅喆 committed
333

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

高雅喆's avatar
高雅喆 committed
345

346
      //join feat
347
      val yesteday = GmeiConfig.getMinusNDate(1).replace("-","")
高雅喆's avatar
高雅喆 committed
348 349 350 351
      val sid_data = sc.sql(
        s"""
           |select distinct
           |  from_unixtime(unix_timestamp(partition_date ,'yyyyMMdd'), 'yyyy-MM-dd') as stat_date,
352
           |  a.device_id,a.ucity_id,a.cid_id,a.label, b.service_id as diary_service_id
高雅喆's avatar
高雅喆 committed
353 354 355 356 357
           |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
358 359
//      sid_data.show()
      println(sid_data.count())
高雅喆's avatar
高雅喆 committed
360

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


高雅喆's avatar
高雅喆 committed
365 366
      val union_data_clabel = sc.sql(
        s"""
367
           |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
368 369 370 371 372 373 374 375 376 377
           |  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
378

高雅喆's avatar
高雅喆 committed
379 380
      val union_data_slabel = sc.sql(
        s"""
381
           |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
382 383 384 385 386 387 388 389 390 391 392 393 394 395
           |  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"""
396
           |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
397 398 399 400 401 402 403 404 405
           |  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
406

高雅喆's avatar
高雅喆 committed
407 408
      val union_data_scity_id = sc.sql(
        s"""
409
           |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
410 411 412 413 414 415 416 417 418 419 420
           |  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
421
      println(union_data_scity_id.count())
高雅喆's avatar
高雅喆 committed
422
      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
423 424 425 426




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

    }
  }

}

高雅喆's avatar
高雅喆 committed
434
object GetDiaryPortrait {
高雅喆's avatar
高雅喆 committed
435 436 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

  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
474
      val stat_date = param.date.replace("-","")
高雅喆's avatar
高雅喆 committed
475

高雅喆's avatar
高雅喆 committed
476 477
      val diary_tag = sc.sql(
        s"""
高雅喆's avatar
高雅喆 committed
478
           |select c.diary_id,
高雅喆's avatar
高雅喆 committed
479 480 481
           |		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
482
           |	(select a.diary_id,b.level1_id,b.level2_id,b.level3_id
高雅喆's avatar
高雅喆 committed
483 484
           |		from online.tl_hdfs_diary_tags_view a
           |		left join online.bl_tag_hierarchy_detail b
高雅喆's avatar
高雅喆 committed
485
           |		on a.tag_id = b.id
高雅喆's avatar
高雅喆 committed
486 487
           |		where a.partition_date = '${stat_date}'
           |		and b.partition_date = '${stat_date}') c
高雅喆's avatar
高雅喆 committed
488
           |	group by c.diary_id
高雅喆's avatar
高雅喆 committed
489 490 491
         """.stripMargin
      )
      diary_tag.show()
高雅喆's avatar
高雅喆 committed
492 493
      println(diary_tag.count())

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


高雅喆's avatar
高雅喆 committed
497 498 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
      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")
高雅喆's avatar
高雅喆 committed
544
      import sc.implicits._
高雅喆's avatar
高雅喆 committed
545 546 547 548 549

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

      val device_search_tag = sc.sql(
        s"""
高雅喆's avatar
高雅喆 committed
550
           |select c.device_id,c.stat_date,c.level1_id,count(c.level1_id) as level1_count
高雅喆's avatar
高雅喆 committed
551 552 553
           |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
554
           |		b.level1_ids as level1_id
高雅喆's avatar
高雅喆 committed
555 556 557 558
           |	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
559
           |    b.level1_ids is not null and
高雅喆's avatar
高雅喆 committed
560
           |		a.partition_date = '${stat_date}'
高雅喆's avatar
高雅喆 committed
561
           |		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
562
           |group by c.device_id,c.level1_id,c.stat_date
高雅喆's avatar
高雅喆 committed
563 564 565 566 567
         """.stripMargin
      )
      device_search_tag.show()
      println(device_search_tag.count())

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

高雅喆's avatar
高雅喆 committed
570
      val max_count_tag = sc.sql(
高雅喆's avatar
高雅喆 committed
571
      s"""
高雅喆's avatar
高雅喆 committed
572
         |select a.device_id,a.stat_date,a.level1_id as max_level1_id,a.level1_count as max_level1_count
高雅喆's avatar
高雅喆 committed
573 574 575 576
         |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
577
       """.stripMargin
高雅喆's avatar
高雅喆 committed
578 579 580 581 582 583 584 585
    )
//        .rdd.map(x => (x(0).toString,x(1).toString,x(2).toString,x(3).toString))
//      max_count_tag.foreachPartition(GmeiConfig.updateDeviceFeat)
//
//      max_count_tag.take(10).foreach(println)
//      println(max_count_tag.count())

      //drop duplicates
高雅喆's avatar
高雅喆 committed
586
      val max_count_tag_rdd = max_count_tag.rdd.groupBy(_.getAs[String]("device_id")).map {
高雅喆's avatar
高雅喆 committed
587 588
        case (device_id,data) =>
          val stat_date = data.map(_.getAs[String]("stat_date")).head
高雅喆's avatar
高雅喆 committed
589 590
          val max_level1_id = data.map(_.getAs[String]("max_level1_id")).head.toString
          val max_level1_count = data.map(_.getAs[Long]("max_level1_count")).head.toString
高雅喆's avatar
高雅喆 committed
591 592 593 594 595 596 597
          (device_id,stat_date,max_level1_id,max_level1_count)
      }.filter(_._1!=null)


      max_count_tag_rdd.foreachPartition(GmeiConfig.updateDeviceFeat)
      max_count_tag_rdd.take(10).foreach(println)
      println(max_count_tag_rdd.count())
高雅喆's avatar
高雅喆 committed
598

高雅喆's avatar
高雅喆 committed
599 600 601 602 603
      sc.stop()

    }
  }

高雅喆's avatar
高雅喆 committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
}

object GetLevelCount {

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

  case class Params(env: String = "dev",
                    path: String = null
                   ) 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]("path")
      .text(s"the path you used")
      .action((x,c) => c.copy(path = 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")

高雅喆's avatar
高雅喆 committed
647 648
      val diary_queue = sc.read.json(param.path).rdd.map(x => x(0).toString).distinct().collect().toList.mkString(",")

高雅喆's avatar
高雅喆 committed
649 650 651
      val diary_level1 = sc.sql(
        s"""
           |select diary_id,explode(split(level1_ids,';')) from diary_feat
高雅喆's avatar
高雅喆 committed
652
           |where diary_id in (${diary_queue})
高雅喆's avatar
高雅喆 committed
653 654 655 656 657
         """.stripMargin
      )
      diary_level1.show()
      println(diary_level1.count())

高雅喆's avatar
高雅喆 committed
658
      val level1_count = diary_level1.rdd.map(x => (x(1).toString)).map(level1 => (level1,1)).reduceByKey((a,b) => a+b).map(p => (p._1,p._2)).sortByKey(false,2)
高雅喆's avatar
高雅喆 committed
659 660 661 662 663 664 665
      level1_count.collect()

      sc.stop()

    }
  }

高雅喆's avatar
高雅喆 committed
666
}