You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@spark.apache.org by "Gianluca Salvo (JIRA)" <ji...@apache.org> on 2015/10/20 15:55:27 UTC

[jira] [Reopened] (SPARK-11173) Cannot save data via MSSQL JDBC

     [ https://issues.apache.org/jira/browse/SPARK-11173?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Gianluca Salvo reopened SPARK-11173:
------------------------------------

Hi,
I suppose I've found the problem: I downloaded the source code and followed the stack trace excpetion. The exception starts in DataFrameWriter.scala, row 275 because it's trying to create a table that already exists.
{code}
if (!tableExists) {
        val schema = JdbcUtils.schemaString(df, url)
        val sql = s"CREATE TABLE $table ($schema)"
        conn.prepareStatement(sql).executeUpdate()
      }
{code}
In the same file, at row 256, it's invoked 
{code}
var tableExists = JdbcUtils.tableExists(conn, table).
{code}
In JdbcUtils.scala, the function tableExists uses the following code to check the presence of the table
{code}
Try(conn.prepareStatement(s"SELECT 1 FROM $table LIMIT 1").executeQuery().next()).isSuccess
{code}
_This code is not valid in SQL Server that implements *TOP* to limit the number of rows_. 
May you change the test query in:
{code}
select 1 from $table where 1=0
{code}
You have an exception if the table does not exist and you don't touch the data but you're compliant with more RDMBS.

Thanks,

Best regards

> Cannot save data via MSSQL JDBC
> -------------------------------
>
>                 Key: SPARK-11173
>                 URL: https://issues.apache.org/jira/browse/SPARK-11173
>             Project: Spark
>          Issue Type: Bug
>          Components: Java API, PySpark, SQL
>    Affects Versions: 1.5.1
>         Environment: Windows 7 sp1 x64, java version "1.8.0_60", Spark 1.5.1, hadoop 2.6, microsoft jdbc 4.2, pyspark
>            Reporter: Gianluca Salvo
>              Labels: patch
>
> Hello,
> I'm experiencing an issue in writing dataframe via JBDC. My code is
> {code:title=Example.python|borderStyle=solid}
> from pyspark import SparkContext
> from pyspark.sql import SQLContext
> import sys
> sc=SparkContext(appName="SQL Query")
> sqlctx=SQLContext(sc)
> serverName="SQLIPAddress"
> serverPort="SQL Port"
> serverUsername="username"
> serverPassword="password"
> serverDatabase="database"
> #########################################################################
> connString="jdbc:sqlserver://{SERVER}:{PORT};user={USER};password={PASSWORD};databasename={DATABASENAME}"
> connString=connString.format(SERVER=serverName,PORT=serverPort,USER=serverUsername,PASSWORD=serverPassword,DATABASENAME=serverDatabase)
> df=sqlctx.read.format("jdbc").options(url=connString,dbtable="(select * from TestTable) as test_Table").load()
> df.show()
> try:
>     df.write.jdbc(connString,"Test_Target","append")
>     print("saving completed")
> except:
>     print("Error in saving data",sys.exc_info()[0])
> sc.stop()
> {code}
> Even if i specify *append*, the code throws an exception saying it is trying to create the table *Test_Target* but the table is already present.
> If I target the script to MariaDB, all is fine
> {code:title=New Connection string|borderStyle=solid}
> connString="jdbc:mysql://{SERVER}:{PORT}/{DATABASENAME}?user={USER}&password={PASSWORD}";
> {code}
> The problem seems to be the Microsoft JDBC driver. Can you suggest or implement same workaround?
> Best regards



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@spark.apache.org
For additional commands, e-mail: issues-help@spark.apache.org