java 调用spark sqlserver,使用spark sql在sqlserver上执行查询

I am trying to get the row count and column count of all the tables in a schema in sql server using spark sql.

when I execute below query using sqoop, it's giving me the correct results.

sqoop eval --connect "jdbc:sqlserver://;database=" \

--username= --password= \

--query """SELECT

ta.name TableName ,

pa.rows RowCnt,

COUNT(ins.COLUMN_NAME) ColCnt FROM .sys.tables ta INNER JOIN

.sys.partitions pa ON pa.OBJECT_ID = ta.OBJECT_ID INNER JOIN

.sys.schemas sc ON ta.schema_id = sc.schema_id join

.INFORMATION_SCHEMA.COLUMNS ins on ins.TABLE_SCHEMA =sc.name and ins.TABLE_NAME=ta.name

WHERE ta.is_ms_shipped = 0 AND pa.index_id IN (1,0) and sc.name ='' GROUP BY sc.name, ta.name, pa.rows order by

TableName"""

But when I try to execute the same query from spark sql, I am getting an error that "com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'WHERE'"

Please help me out, if anyone has an idea about this error.

Below is the spark sql command I executed

spark-shell --jars "/var/lib/sqoop/sqljdbc42.jar"

sqlContext.read.format("jdbc").option("url", "jdbc:sqlserver://;database=;user=;password=").option("dbtable", """(SELECT

ta.name TableName ,pa.rows RowCnt,

COUNT(ins.COLUMN_NAME) ColCnt FROM .sys.tables ta INNER JOIN

.sys.partitions pa ON pa.OBJECT_ID = ta.OBJECT_ID INNER JOIN

.sys.schemas sc ON ta.schema_id = sc.schema_id join

.INFORMATION_SCHEMA.COLUMNS ins on ins.TABLE_SCHEMA =sc.name and ins.TABLE_NAME=ta.name

WHERE ta.is_ms_shipped = 0 AND pa.index_id IN (1,0) and sc.name ='' GROUP BY sc.name,ta.name, pa.rows)""").option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver").load()

expected output:

TableName, RowCnt, ColCnt

table A, 62, 30

table B, 3846, 76

解决方案

The problem in your Spark SQL command is with the dbTable option.

dbTable accepts anything that is valid in a FROM clause of a SQL query can be used. For example, instead of a full table you could also use a subquery in parentheses. However, when using subqueries in parentheses, it should have an alias. Thus your command should be modified as,

sqlContext

.read

.format("jdbc")

.option("url", "jdbc:sqlserver://;database=;user=;password=")

.option("dbtable",

"""(SELECT

ta.name TableName ,

pa.rows RowCnt,

COUNT(ins.COLUMN_NAME) ColCnt

FROM .sys.tables ta

INNER JOIN

.sys.partitions pa

ON pa.OBJECT_ID = ta.OBJECT_ID

INNER JOIN

.sys.schemas sc

ON ta.schema_id = sc.schema_id

JOIN

.INFORMATION_SCHEMA.COLUMNS ins

ON ins.TABLE_SCHEMA = sc.name and ins.TABLE_NAME = ta.name

WHERE ta.is_ms_shipped = 0

AND pa.index_id IN (1,0)

AND sc.name =''

GROUP BY sc.name,ta.name, pa.rows) as TEMP""")

.option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver")

.load()

Just a hunch. Hope this helps!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值