Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 从Spark Scala查询SQL Server-如何?_Sql Server_Scala_Apache Spark_Jdbc_Maven 2 - Fatal编程技术网

Sql server 从Spark Scala查询SQL Server-如何?

Sql server 从Spark Scala查询SQL Server-如何?,sql-server,scala,apache-spark,jdbc,maven-2,Sql Server,Scala,Apache Spark,Jdbc,Maven 2,Env:Scala的Spark 1.6,Cloudera SQL Server 2012,11.0版 我正在尝试从Spark查询SQL Server object ConnTest extends App { val conf = new SparkConf() val sc = new SparkContext(conf.setAppName("Spark Ingestion").setMaster("local[*]")) val sqlc

Env:Scala的Spark 1.6,Cloudera SQL Server 2012,11.0版

我正在尝试从Spark查询SQL Server

object ConnTest extends App {
  val conf = new SparkConf()
  val sc = new SparkContext(conf.setAppName("Spark Ingestion").setMaster("local[*]"))
  val sqlcontext = new SQLContext(sc)

    val prop=new Properties()
    val url2="jdbc:sqlserver://xxx.xxx.xxx:1511;user=username;password=mypassword;database=SessionMonitor"
    prop.setProperty("user","username")
    prop.setProperty("password","mypassword")
    val test=sqlcontext.read.jdbc(url2,"Service",prop)

  val dd = sqlcontext.sql("select count(*) as TOT from Service")
  dd.foreach(println)
}
我的pom.xml有依赖项-

<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>6.1.0.jre8</version>
        </dependency>
错误:

线程主java.sql.SQLException中的异常:没有合适的驱动程序


这应添加到您的代码中:

prop.setProperty("driver" , "com.mysql.jdbc.Driver")
在我的例子中,我使用了这个,它完全工作得很好:

val jdbcDF = sqlContext.read
      .format("jdbc")
      .option("driver" , "com.mysql.jdbc.Driver")
      .option("url", "jdbc:mysql://<<>Servername>:3306/<<DatabaseName>>")
      .option("dbtable", "(SELECT id, name FROM partner) tmp")
      .option("user", "username")
      .option("password", "******")
      .load()

希望这能起作用。

您需要使用DependencySSQL jdbc构建jar,因为您的集群没有internet连接来下载jar。或者您可以将此jar移动到集群中,并执行maven installval jdbcDF=sqlContext.read.formatjdbc.optiondriver,com.microsoft.sqlserver.jdbc.SQLServerDriver.optionurl,jdbc:mysql://:3306;>。optiondbtable,选择id,来自合作伙伴tmp的名称。optionuser,username.optionpassword,*********.load:这取决于我的情况。我在你的情况下使用了mysql,所以你使用的是MSsql。toofrellik-这个问题特别问如何使用SQL Server,而不是mysql
val jdbcDF = sqlContext.read
      .format("jdbc")
      .option("driver" , "com.mysql.jdbc.Driver")
      .option("url", "jdbc:mysql://<<>Servername>:3306/<<DatabaseName>>")
      .option("dbtable", "(SELECT id, name FROM partner) tmp")
      .option("user", "username")
      .option("password", "******")
      .load()