package mobvista.dmp.datasource.id_mapping

import com.datastax.spark.connector.{SomeColumns, _}
import mobvista.dmp.common.CommonSparkJob
import mobvista.dmp.datasource.id_mapping.Constant.Result
import mobvista.dmp.util.PropertyUtil
import org.apache.commons.cli.{BasicParser, Options}
import org.apache.spark.sql.SparkSession

/**
 * @package: mobvista.dmp.datasource.id_mapping
 * @author: wangjf
 * @date: 2021/11/30
 * @time: 7:51 下午
 * @email: jinfeng.wang@mobvista.com
 */
class IDMappingWrite extends CommonSparkJob with Serializable {
  def commandOptions(): Options = {
    val options = new Options()
    options.addOption("date", true, "date")
    options
  }

  override protected def run(args: Array[String]): Int = {

    val parser = new BasicParser()
    val options = commandOptions()
    val commandLine = parser.parse(options, args)
    val date = commandLine.getOptionValue("date")

    val spark: SparkSession = SparkSession.builder()
      .appName(s"IDMappingWrite.$date")
      .config("spark.rdd.compress", "true")
      .config("spark.io.compression.codec", "snappy")
      .config("spark.sql.orc.filterPushdown", "true")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .config("spark.cassandra.connection.host", PropertyUtil.getProperty("ip.properties", "aws_host"))
      .config("spark.cassandra.connection.port", "9042")
      .config("spark.cassandra.query.retry.count", "3")
      .config("spark.cassandra.connection.compression", "LZ4")
      .config("spark.cassandra.input.consistency.level", "LOCAL_ONE")
      .config("spark.cassandra.output.consistency.level", "LOCAL_ONE")
      .config("spark.cassandra.connection.remoteConnectionsPerExecutor", "16")
      .config("spark.cassandra.connection.localConnectionsPerExecutor", "8")
      .config("spark.cassandra.input.fetch.sizeInRows", "4096")
      .config("spark.cassandra.concurrent.reads", "4096")
      .config("spark.cassandra.output.concurrent.writes", "32")
      .config("spark.cassandra.output.batch.grouping.buffer.size", "2048")
      .config("spark.cassandra.output.batch.size.bytes", "2048")
      .config("spark.cassandra.connection.keepAliveMS", "60000")
      .config("spark.cassandra.auth.username", "chMjFHCD5J7NrXk3gKE8")
      .config("spark.cassandra.auth.password", "mAQRjmcTMtYRx6iz43FM")
      .enableHiveSupport()
      .getOrCreate()

    try {

      val keyspace = "one_service"
      val tableName = "id_mapping"
      val columns = SomeColumns("device_id", "device_type", "one_id")
      val sql =
        s"""
           |SELECT device_id, device_type, one_id, update_date FROM ads.ads_device_id_mapping
           |  WHERE dt = '${date}' AND `type` = 'result' AND source = 'cn'
           |""".stripMargin

      val df = spark.sql(sql)
        .rdd
        .map(row => {
          Result(row.getAs[String]("device_id"), row.getAs[String]("device_type"), row.getAs[String]("one_id"),
            row.getAs[String]("update_date"))
        })
      df.saveToCassandra(keyspace, tableName, columns)

    } finally {
      if (spark != null) {
        spark.stop()
      }
    }
    0
  }
}

object IDMappingWrite {
  def main(args: Array[String]): Unit = {
    new IDMappingWrite().run(args)
  }
}
