gpt4 book ai didi

apache-spark-sql - 在 azure 数据 block 中查询 sql server 表

转载 作者:行者123 更新时间:2023-12-04 09:15:14 26 4
gpt4 key购买 nike

我正在使用以下代码查询 sql server 表 hr.employee 在我使用 azure databricks 的 azure sql server 数据库中。我是 Spark sql 的新手,并试图一步一步地学习细微差别。
Azure Databricks:

%scala
val jdbcHostname = dbutils.widgets.get("hostName")
val jdbcPort = 1433
val jdbcDatabase = dbutils.widgets.get("database")
val jdbcUrl = s"jdbc:sqlserver://${jdbcHostname}:${jdbcPort};database=${jdbcDatabase}"

import java.util.Properties
val connectionProperties = new Properties()

connectionProperties.put("user", s"${jdbcUsername}")
connectionProperties.put("password", s"${jdbcPassword}")

%scala
val driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
connectionProperties.setProperty("Driver", driverClass)

%scala
val employee = spark.read.jdbc(jdbcUrl, "hr.Employee", connectionProperties)

%scala
spark.sql("select * from employee")

%sql
select * from employee

employee.select("col1","col2").show()
我收到以下错误。不知道我做错了什么。也尝试了几种变体,但到目前为止没有运气。
错误:
';' expected but integer literal found.

command-922779590419509:26: error: not found: value %
%sql

command-922779590419509:27: error: not found: value select
select * from employee

command-922779590419509:27: error: not found: value from
select * from employee

command-922779590419509:16: error: not found: value %
%scala

最佳答案

您可以查看以下步骤以使用其 JDBC 驱动程序查询 SQL Server:
注:在整个教程中,我使用的是“Scala”源代码。
第一步:检查 JDBC 驱动程序是否可用

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
第 2 步:创建 JDBC URL
val jdbcHostname = "<hostname>"
val jdbcPort = 1433
val jdbcDatabase = "<database>"

// Create the JDBC URL without passing in the user and password parameters.
val jdbcUrl = s"jdbc:sqlserver://${jdbcHostname}:${jdbcPort};database=${jdbcDatabase}"

// Create a Properties() object to hold the parameters.
import java.util.Properties
val connectionProperties = new Properties()

connectionProperties.put("user", s"${jdbcUsername}")
connectionProperties.put("password", s"${jdbcPassword}")
第 3 步:检查与 SQLServer 数据库的连接
val driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
connectionProperties.setProperty("Driver", driverClass)
第四步:从 JDBC 读取数据
val employees_table = spark.read.jdbc(jdbcUrl, "employees", connectionProperties)
第五步:从数据库表中读取模式
employees_table.printSchema
第六步:您可以对此 JDBC 表运行查询:
display(employees_table.select("age", "salary").groupBy("age").avg("salary"))
这是供您引用的屏幕截图:
enter image description here
enter image description here
enter image description here
========> 更新答案 <================
使用 SQL 查询:
第一步:要从 Python 或 Scala 中的 DataFrame 创建全局表:
dataFrame.write.saveAsTable("<table-name>")
enter image description here
** Step2:** 现在运行 SQL 查询
spark.sql("select * from employees_table")

%sql

select * from employees_table
enter image description here

关于apache-spark-sql - 在 azure 数据 block 中查询 sql server 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63255827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com