如何在Spark/Scala解释器(REPL)中使用JDBC?

如何在Spark/Scala解释器(REPL)中使用JDBC?,scala,jdbc,apache-spark,Scala,Jdbc,Apache Spark,我试图访问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\

我试图访问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.
添加了“C:\spark\sqljdbc41.jar”。您的新类路径是:
“;;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-rdbms-3.2.1.jar;;C:\spark\sqljdbc41.jar”
重播:导入java.sql_
错误:
在编写时:
阶段:jvm
库版本:2.10.4版
编译器版本:2.10.4版
重构参数:
typer的最后一个树:应用(构造函数$read)
符号:类$read中的构造函数$read(标志:)
符号定义:def():$line10.$read
tpe:$line10.$read
符号所有者:构造函数$read->class$read->package$line10
上下文所有者:类iwC->package$line10
==封闭模板或块==
模板(//val:,tree.tpe=$line10.iwC)
“java.lang.Object”、“scala.Serializable”//parents
瓦尔德夫(
私有的
"_"
)
...
==树的扩展类型==
TypeRef(TypeSymbol(类$read扩展可序列化))
编译期间未捕获的异常:java.lang.AssertionError
java.lang.AssertionError:断言失败:尝试在“C:\Users\Username\AppData\Local\Temp\spark-28055904-e7d2-4052-9354-ae3769266cb4”中查找“$line10”,但它不是目录
那个条目似乎已经破坏了编译器。要我重播吗
您的会话?我可以重新运行除最后一行之外的每一行。
我可以用这个驱动程序运行Scala程序,一切都很好


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

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

C:\spark> .\bin\spark-shell --jars sqljdbc41.jar
在本例中,我已将jar文件复制到C:\spark文件夹中

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