Bootstrap

Spark查询Hive表

Scala版本:2.10

Spark版本:1.6.0

1、引入依赖

<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-hive_2.10</artifactId>
    <version>1.6.0</version>
</dependency>

2、代码示例

import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.{SparkConf, SparkContext}

object Test {
  def main(args: Array[String]): Unit = {
    val conf=new SparkConf().setAppName("Test")
    val sc=new SparkContext(conf)
    val hc=new HiveContext(sc)

    val runSql="select phone,province,city from dwd.t_test_info where phone='13305500002' and month='201901'"
    val result=hc.sql(runSql).map(line=>{
      val phone=line.get(0)
      val province=line.get(1)
      val city=line.get(2)
      (phone,province,city)
    })
    result.repartition(1).saveAsTextFile("/tmp/result")
  }

 

;