从Azure Databricks笔记本将R数据帧写入AzureSQL DB

时间:2019-05-13 08:39:29

标签: r azure-sql-database rodbc azure-databricks

我正在尝试从Azure Databricks上的R笔记本连接到Azure SQL DB。我收到以下错误: first argument is not an open RODBC channel In addition: Warning messages: 1: In odbcDriverConnect("Driver={ODBC Driver 17 for SQL Server};Server=tcp:########.database.windows.net,1433;Database=########;Uid=####@#######;Pwd=#########;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;") : [RODBC] ERROR: state 01000, code 0, message [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found 2: In odbcDriverConnect("Driver={ODBC Driver 17 for SQL Server};Server=tcp:########.database.windows.net,1433;Database=########;Uid=####@#######;Pwd=#########;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;") : ODBC connection failed

这是我的连接字符串:

library(RODBC)
dbhandle <- odbcDriverConnect("Driver={ODBC Driver 17 for SQLServer};Server=tcp:########.database.windows.net,1433;Database=########;Uid=#### 
@#######;Pwd=#########;Encrypt=yes;TrustServerCertificate=no;Connection 
Timeout=30;")

Query - res <- sqlQuery(dbhandle, 'SELECT * FROM table_name') `

有人可以告诉我问题出在哪里。或一些可能的替代方法,可用于从Azure Databricks笔记本将数据帧写入Azure SQL DB。

2 个答案:

答案 0 :(得分:0)

由于Azure Databricks本身不包含ODBC驱动程序,因此在建立连接之前,您需要安装ODBC驱动程序。

作为单独的块,在单独的单元格中运行以下shell脚本,以安装驱动程序:

%sh
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

sudo apt-get update

sudo ACCEPT_EULA=Y apt-get -q -y install msodbcsql17

答案 1 :(得分:0)

在您的Databricks R笔记本中

library(SparkR)

#replace<..> with your connection strings
Conn <- "jdbc:sqlserver://<myserver>.database.windows.net:1433;database=<mydatabase>;user=<database_user>;password=<password>"

#Convert a R dataframe to a Spark dataframe
df <- as.DataFrame(df)

write.jdbc(df, Conn, "SQLtablename", mode = "overwrite")