pom文件中 添加如下 相关依赖
<dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-hive_2.11</artifactId> <version>${spark.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql --> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.11</artifactId> <version>${spark.version}</version> </dependency>
<dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.3</version> </dependency>
resources 文件下 添加 hive-site.xml ,内容如下:
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://192.168.137.251:3306/hive?createDatabaseIfNotExist=true</value> </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.jdbc.Driver</value> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>root</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>oracle</value> </property> <property> <name>hive.cli.print.current.db</name> <value>true</value> </property> <property> <name>hive.cli.print.header</name> <value>true</value> </property> </configuration>
=== 测试代码如下 ===
package com.spark.sql import java.io.File import org.apache.spark.sql.SparkSession /** * Created by 92421 on 2018/4/5. */ object ReadHiveJionOracle { def main(args: Array[String]): Unit = { val warehouseLocation = new File("spark-warehouse").getAbsolutePath val format = "yyyy-MM-dd'T'HH:mm:ssz" val spark = SparkSession.builder() .master("local[2]") .appName("ReadHiveJionOracle") .config("spark.sql.warehouse.dir",warehouseLocation).enableHiveSupport().getOrCreate() import spark.sql import spark.implicits val hiveDF =spark.table("default.emp").createOrReplaceTempView("emp") val oracleDriverUrl = "jdbc:oracle:thin:@//192.168.137.251:1521/devdb" val oracleDF = Map("url" -> oracleDriverUrl, "user" -> "scott", "password" -> "tiger", "dbtable" -> "dept", "driver" -> "oracle.jdbc.driver.OracleDriver") val jdbcDF1 = spark.read.options(oracleDF).format("jdbc").load.createOrReplaceTempView("dept") val resultDF=spark.sql("select * from emp e join dept d on e.deptno = d.deptno " + "where e.comm is not null" ).show() spark.stop() } }