spark java 写入mysql,无法使用JDBC将Spark数据集写入数据库

I need to write my spark dataset to oracle database table. I am using dataset write method with append mode. But getting analysis exception,

when the spark job was triggered on cluster using spark2-submit command.

I have read the json file, flattened it and set into a dataset as abcDataset.

Spark Version - 2

Oracle Database

JDBC Driver - oracle.jdbc.driver.OracleDriver

Programming Language - Java

Dataset abcDataset= dataframe.select(col('abc').....{and other columns};

Properties dbProperties = new Properties();

InputStream is = SparkReader.class.getClassLoader().getResourceAsStream("dbProperties.yaml");

dbProperties.load(is);

String jdbcUrl = dbProperties.getProperty("jdbcUrl");

dbProperties.put("driver","oracle.jdbc.driver.OracleDriver");

String where = "USER123.PERSON";

abcDataset.write().format("org.apache.spark.sql.execution.datasources.jdbc.DefaultSource").option("driver", "oracle.jdbc.driver.OracleDriver").mode("append").jdbc(jdbcUrl, where, dbProperties);

Expected - to write into database but getting the error below -

org.apache.spark.sql.AnalysisException: Multiple sources found for jdbc (org.apache.spark.sql.execution.datasources.jdbc.JdbcRelationProvider, org.apache.spark.sql.execution.datasources.jdbc.DefaultSource), please specify the fully qualified class name.;

at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:670)

Do we need to set any additional property in spark submit command, as i am running this on cluster, or any step is missing ?

解决方案

You need to use either abcDataset.write.jdbc or abcDataset.write.format("jdbc") when you are writing via jdbc from spark to rdbms.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值