关闭

[置顶] Spark 1.6 (Java) 问题汇总

标签: 解决方案大数据 Spark
433人阅读 评论(0) 收藏 举报
分类:

一、通过SparkSQL读取Oracle时报找不到Oracle JDBC包 (java.lang.ClassNotFoundException:oracle.jdbc.driver.OracleDriver)


说明

程序是本地通过Java将Spark提交给集群运行,但是集群上面没有Oracle JDBC包


解决方法:

官网对于集群运行JDBC的说明:

Troubleshooting

  • The JDBC driver class must be visible to the primordial class loader on the client session and on all executors. This is because Java’s DriverManager class does a security check that results in it ignoring all drivers not visible to the primordial class loader when one goes to open a connection. One convenient way to do this is to modify compute_classpath.sh on all worker nodes to include your driver JARs.

但是在/etc/profile文件中添加jar路径并没有效果

最终找到方法需要在代码中指定Worker的jar依赖,当然jar包已经上传到每台Worker上,代码如下:

SparkConf conf = new SparkConf().setAppName("Test").setMaster("spark://master2:7077");
		<span style="font-size:14px;color:#ff0000;">conf.set("spark.driver.extraClassPath", "/data/ojdbc14-10.2.0.3.0.jar");
	       conf.set("spark.executor.extraClassPath", "/data/ojdbc14-10.2.0.3.0.jar");</span>

		JavaSparkContext sparkContext = new JavaSparkContext(conf);
		sparkContext.hadoopConfiguration().set("fs.defaultFS", "hdfs://mycluster");
		sparkContext.hadoopConfiguration().set("dfs.nameservices", "mycluster");
		sparkContext.hadoopConfiguration().set("dfs.ha.namenodes.mycluster", "nn1,nn2");
		sparkContext.hadoopConfiguration().set("dfs.namenode.rpc-address.mycluster.nn1", "xx.xx.xx.xxx:8020");
		sparkContext.hadoopConfiguration().set("dfs.namenode.rpc-address.mycluster.nn2", "xx.xx.xx.xxx:8020");
		sparkContext.hadoopConfiguration().set("dfs.client.failover.proxy.provider.mycluster","org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");

		
		SQLContext sqlContext = new SQLContext(sparkContext);
		Map<String, String> options = new HashMap<String, String>();
		options.put("url", "jdbc:oracle:thin:mioclub/mioclub3728@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=myrac-scan.mic.com.tw)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=myrac.mic.com.tw)))");
		options.put("dbtable", "mio_login_transfer");
		options.put("driver", "oracle.jdbc.driver.OracleDriver");
		DataFrame jdbcDF = sqlContext.read().format("jdbc"). options(options).load();
		Row a  = jdbcDF.first();
		
		DataFrame d2 = jdbcDF.limit(10);
		d2.rdd().saveAsTextFile("/Test/sql.txt");



1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15071次
    • 积分:384
    • 等级:
    • 排名:千里之外
    • 原创:28篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章分类