1.x 版本使用SparkContext

    1. import org.apache.spark.{SparkConf, SparkContext}
    2. import org.apache.spark.sql.SparkSession
    3. import org.apache.spark.sql.hive.HiveContext
    4. val conf = new SparkConf().set("spark.hadoop.validateOutputSpecs", "false").setAppName("mlx_feature_process")
    5. val sc = new SparkContext(conf)
    6. val predictSample = sc.textFile(...).map(r=>{PredictResult(r)})
    7. val sqlCtx = new HiveContext(sc)
    8. import sqlCtx.implicits._
    9. predictSample.toDF().registerTempTable("predict_table")
    10. val tableName = "predict_sample_result_"+model
    11. sqlCtx.sql("set hive.exec.dynamic.partition=true")
    12. sqlCtx.sql("set hive.exec.dynamic.partition.mode=nostrick")
    13. sqlCtx.sql("use ba_dealrank")
    14. sqlCtx.sql(createTableSql(tableName))
    15. sqlCtx.sql(
    16. s"""
    17. |alter table $tableName drop if exists partition(dt=$date)
    18. |""".stripMargin)
    19. sqlCtx.sql(
    20. s"""
    21. |insert into $tableName partition(dt=$date)
    22. |select globalid,strategy,userid,item_id,item_type,
    23. |pctr,pcvr,pay_label,click_label, ts, hour, minute
    24. |from predict_table""".stripMargin)