sqlcontext不接受端口号

时间:2017-03-27 11:24:04

标签: scala apache-spark jdbc apache-spark-sql

, 我有两个代码,打算连接到数据库,并在屏幕上抛出表格内容。

第一个代码

import java.util.Properties
import org.apache.spark.SparkContext
import org.apache.spark.SparkConf
import org.apache.spark.sql.SQLContext
import sqlContext.implicits._


val conf = new SparkConf().setAppName("tets_HIVE4").setMaster("local").set("spark.executor.memory","1g").set("spark.driver.allowMultipleContexts", "true");
val sc = new SparkContext(conf)
val sqlContext = new SQLContext(sc)
var props = new java.util.Properties()
props.setProperty("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
 val jdbcDF = sqlContext.read.format("jdbc").options(Map("url" -> "jdbc:sqlserver://server-IP:port;databaseName=db_name;user=uid;password=pwd","dbtable" -> "tbl_name")).load()

jdbcDF.show(5)
exit()

第二个代码

import org.apache.spark.SparkContext
import org.apache.spark.SparkConf
import org.apache.spark.sql.SQLContext
import sqlContext.implicits._
import java.util.Properties

val conf = new SparkConf().setAppName("test").setMaster("local").set("spark.driver.allowMultipleContexts", "true");
val sc = new SparkContext(conf)
val sqlContext = new SQLContext(sc)
val df = sqlContext.read.format("jdbc").option("url","jdbc:sqlserver://server-IP/db_name:port").option("driver","com.microsoft.sqlserver.jdbc.SQLServerDriver").option("dbtable","tbl_name").option("user","uid").option("password","uid").load()

df.show(5)
exit()

第一个代码工作正常,但第二个代码是第二个代码,我得到以下错误:;

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host server_IP/db_name port "port-num" has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

我很确定端口号是正确的,因为第一个代码具有相同的端口,并且它的工作正常。所以,这让我得出一个结论,即我在第二个代码中出现了一些语法错误。我试图将端口号设置为.option(" port"," port_num"),但没有工作。然后我尝试将端口视为.option(" url"," jdbc:sqlserver:// server-IP / db_name:port"),即使这不起作用。

所以,我有以下问题::

1)除了语法位置之外,两个代码之间的基本区别是什么?我们应该何时使用它们以及什么情况?

2)第二段代码中提到端口号的正确语法是什么?

1 个答案:

答案 0 :(得分:1)

你在两个截然不同的地方db_name,根据https://msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx,第二个是非法的。试试.option("url","jdbc:sqlserver://server-IP:port").option("databaseName", "db_name")