Maven依赖

  1. <!--添加spark对hive的支持的jar包-->
  2. <dependency>
  3. <groupId>org.apache.spark</groupId>
  4. <artifactId>spark-hive_2.11</artifactId>
  5. <version>2.1.1</version>
  6. </dependency>

hive-site.xml配置文件

  1. <?xml version="1.0" ?>
  2. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
  3. <configuration>
  4. <property>
  5. <name>javax.jdo.option.ConnectionURL</name>
  6. <!-- 这个库是元数据所在的库-->
  7. <value>jdbc:mysql://zjj101:3306/hive?createDatabaseIfNotExist=true</value>
  8. <description>JDBC connect string for a JDBC metastore</description>
  9. </property>
  10. <property>
  11. <name>javax.jdo.option.ConnectionDriverName</name>
  12. <value>com.mysql.jdbc.Driver</value>
  13. <description>Driver class name for a JDBC metastore</description>
  14. </property>
  15. <property>
  16. <name>javax.jdo.option.ConnectionUserName</name>
  17. <value>root</value>
  18. <description>username to use against metastore database</description>
  19. </property>
  20. <property>
  21. <name>javax.jdo.option.ConnectionPassword</name>
  22. <value>root</value>
  23. <description>password to use against metastore database</description>
  24. </property>
  25. </configuration>

代码

  1. package com.hive
  2. import org.apache.spark.sql.SparkSession
  3. object HiveRead {
  4. def main(args: Array[String]): Unit = {
  5. val spark: SparkSession = SparkSession
  6. .builder()
  7. .master("local[*]")
  8. .appName("HiveRead")
  9. // 添加支持外置hive
  10. .enableHiveSupport()
  11. .getOrCreate()
  12. // spark.sql("show databases").show
  13. spark.sql("use gmall").show
  14. // spark.sql("show tables").show
  15. spark.sql("select * from emp").show
  16. spark.close()
  17. }
  18. }

控制台打印结果

  1. ++
  2. ||
  3. ++
  4. ++
  5. +---+----+
  6. | id|name|
  7. +---+----+
  8. | 1| aaa|
  9. | 2| bbb|
  10. | 3| ccc|
  11. +---+----+