首页 > 大数据 > 数据分析 > spark with hive
//需要spark-hive project
package hgs.spark.hive
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.hadoop.hive.conf.HiveConfUtil
import org.apache.spark.sql.SparkSession
//注:需要将hive-site.xml放到当前工程的classpath下面
object SparkWithHive {
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("hive").setMaster("local")
val context = new SparkContext(conf)
val builder = SparkSession.builder()
.appName("hiveApp")
.config("spark.sql.warehouse.dir","hdfs://bigdata00:9000/user/hive/warehouse/")
.enableHiveSupport()
.getOrCreate()
import builder.implicits
//test库下面的test表 ssql.show()可以打印表的信息
val ssql = builder.sql("select a.id,b.name,b.age+10 bigage from test.test a,test.test1 b where a.id = b.id")
//这里转换为rdd进行操作,转换为三元的tuple, List((1,hgs,26), (2,wd,24), (3,zz,25), (4,cm,24))
val list = ssql.rdd.map(x=>(x(0),x(1),x(2))).collect()
println(list.toList)
context.stop()
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31506529/viewspace-2221049/ ,如需转载,请注明出处,否则将追究法律责任。