使用 Spark 将数据插入 Microsoft SQL 服务器

insert data into Microsoft SQL server using Spark

我正在尝试使用以下 Jdbc 方法使用 spark 将数据插入 sql 服务器。

选项 1:

    prop.put("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
dataf.write.mode(org.apache.spark.sql.SaveMode.Append).jdbc(url,table_name, prop) 

Table 已创建。追加新的 data.Job 错误,出现以下异常

Exception in thread "main"

com.microsoft.sqlserver.jdbc.SQLServerException: CREATE TABLE permission denied in database

问题是:Why create table permission is required for appending the data?

选项2:

    prop.put("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils.saveTable(dataf, url, table_name, prop) 

以上命令从 spark-shell 开始工作。当在 scala 代码中使用相同的代码并与依赖项一起打包时,出现以下异常

Exception in thread "main" java.sql.SQLException: No suitable driver at java.sql.DriverManager.getDriver(DriverManager.java:315)

我尝试设置驱动程序 class-path 和执行程序 class-path 以及 --jars 仍然没有成功。在 driver-classpath 和 --jars 中包含 sqljdbc4.jar。 已将 sqljdbc4.jar 复制到所有工作节点,但仍然没有成功。

对此有什么想法吗?

经过大量的搜索和测试,我找到了答案。它可能对某人有用。

Option 1: This is because of bug in spark 1.5.X. the same was resolved in 1.6.x and later. Because of the bug, It always try to create a new table.

Option2: This causes because , driver name on classpath given priority than properties we are passing as argument. Workaround for this is to create connection and then invoke savetable.

 workaround if you are using spark 1.5.x or lower.
      JdbcUtils.createConnection(url, prop)
       JdbcUtils.saveTable()