【问题标题】:How to use JDBC from within the Spark/Scala interpreter (REPL)?如何在 Spark/Scala 解释器 (REPL) 中使用 JDBC?
【发布时间】:2015-02-08 08:11:30
【问题描述】:

我正在尝试在 Spark 的 Scala 解释器中访问数据库,但没有成功。

首先,我导入了 DriverManager,并使用以下命令将我的 SQL Server JDBC 驱动程序添加到类路径中:

scala> import java.sql._
import java.sql._

scala> :cp sqljdbc41.jar

REPL 崩溃并显示长转储消息:

Added 'C:\spark\sqljdbc41.jar'.  Your new classpath is:
";;C:\spark\bin\..\conf;C:\spark\bin\..\lib\spark-assembly-1.1.1-hadoop2.4.0.jar;;C:\spark\bin\..\lib\datanucleus-api-jdo-3.2.1.jar;C:\spark\bin\..\lib\datanucleus-core-3.2.2.jar;C:\spark\bin\..\lib\datanucleus-rdbms-3.2.1.jar;;C:\spark\sqljdbc41.jar"
Replaying: import java.sql._
error:
     while compiling: <console>
        during phase: jvm
     library version: version 2.10.4
    compiler version: version 2.10.4
  reconstructed args:

  last tree to typer: Apply(constructor $read)
              symbol: constructor $read in class $read (flags: <method> <triedcooking>)
   symbol definition: def <init>(): $line10.$read
                 tpe: $line10.$read
       symbol owners: constructor $read -> class $read -> package $line10
      context owners: class iwC -> package $line10

== Enclosing template or block ==

Template( // val <local $iwC>: <notype>, tree.tpe=$line10.iwC
  "java.lang.Object", "scala.Serializable" // parents
  ValDef(
    private
    "_"
    <tpt>
    <empty>
  )
    ...

== Expanded type of tree ==

TypeRef(TypeSymbol(class $read extends Serializable))

uncaught exception during compilation: java.lang.AssertionError
java.lang.AssertionError: assertion failed: Tried to find '$line10' in 'C:\Users\Username\AppData\Local\Temp\spark-28055904-e7d2-4052-9354-ae3769266cb4' but it is not a directory
That entry seems to have slain the compiler.  Shall I replay
your session? I can re-run each line except the last one.

我能够使用驱动程序运行 Scala 程序,并且一切正常。

如何初始化我的 REPL 以允许我通过 JDBC 从 SQL Server 访问数据?

【问题讨论】:

    标签: scala jdbc apache-spark


    【解决方案1】:

    看起来交互式 :cp 命令在 Windows 中不起作用。但我发现,如果我使用以下命令启动 spark shell,则 JDBC 驱动程序已加载并可用:

    C:\spark> .\bin\spark-shell --jars sqljdbc41.jar
    

    在这种情况下,我已将 jar 文件复制到 C:\spark 文件夹中。

    (此外,还可以使用--help 获得帮助以查看启动时可用的其他命令。)

    【讨论】:

      猜你喜欢
      • 2012-04-27
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 2018-02-02
      • 2021-02-03
      • 1970-01-01
      相关资源
      最近更新 更多