从Spark Scala查询SQL Server - 如何?

问题描述 投票:0回答:1

环境:Spark 1.6与Scala,Cloudera SQL Server 2012,版本11.0

我试图从Spark查询SQLServer。

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)
}

我的prom.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>

我没有下载任何jar文件;不要将jar安装到maven存储库,也不要将jar安装到类路径。我的Hadoop集群没有连接到Internet。在创建maven包之后,我尝试使用提交

spark-submit --class ConnTest /Hadoopshare/tmp/sqldb-1.0-SNAPSHOT.jar

错误:

Exception in thread "main" java.sql.SQLException: No suitable driver 

谢谢 侯赛因

sql-server scala apache-spark jdbc maven-2
1个回答
0
投票

这应该添加到您的代码中:

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()

希望这应该有效。

© www.soinside.com 2019 - 2024. All rights reserved.