code:

package com.liupu
import org.apache.spark.{ SparkContext, SparkConf }
import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.sql.hive.orc._
object HiveContextLoadCsv {
  def main(args: Array[String]) {
    var sc = new SparkContext()
    val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc)
    val df = hiveContext.read
      .format("com.databricks.spark.csv")
      .option("header", "true")
      .option("inferSchema", "true")
      .load("/tmp/cars.csv")
    val selectedData = df.select("year", "model")
    selectedData.write.format("orc").option("header", "true").save("/tmp/hive_cars")
    hiveContext.sql("create external table hive_cars(year int,model string) stored as orc location '/tmp/hive_cars'")
    hiveContext.sql("show tables").collect().foreach(println)
    hiveContext.sql("select * from hive_cars").collect().foreach(println)
    sc.stop()
  }
}

 

spark submit:

./spark-submit \
--class com.liupu.HiveContextLoadCsv \
--master local[*] \
/home/pl62716/scalaTest.jar

 

相关文章:

  • 2022-02-09
  • 2021-05-28
  • 2022-12-23
  • 2021-05-18
  • 2021-05-21
  • 2021-09-25
  • 2021-08-19
  • 2022-12-23
猜你喜欢
  • 2021-08-06
  • 2021-09-07
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
  • 2021-09-20
  • 2022-12-23
相关资源
相似解决方案