首先在你的pom中添加如下依赖
<dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.3</version> </dependency>
之后准备sparksql容器对象
val spark = SparkSession .builder() .master("local[*]") .appName("ReadOracle") .config("spark.driver.cores","1") .getOrCreate()
拉取是调用read方法
val oracleDF = spark.read .format("jdbc") .option("url","jdbc:oracle:thin:@192.168.65.123:1521:数据库名字") .option("dbtable", "ds.task_db") .option("user", "root") .option("password", "root") .option("driver", "oracle.jdbc.driver.OracleDriver") .load();
注意在拉取之后如果使用DF时报错无法装换DataSet,那就手动导入如下类
import org.apache.spark.sql.Dataset 或者全导进来 import org.apache.spark.sql._