我正在尝试使用spark sql获取sql server模式中所有表的行数和列数。
当我使用sqoop执行以下查询时,它会给我正确的结果。
sqoop eval --connect "jdbc:sqlserver://<hostname>;database=<dbname>" \
--username=<username> --password=<pwd> \
--query """SELECT
ta.name TableName ,
pa.rows RowCnt,
COUNT(ins.COLUMN_NAME) ColCnt FROM <db>.sys.tables ta INNER JOIN
<db>.sys.partitions pa ON pa.OBJECT_ID = ta.OBJECT_ID INNER JOIN
<db>.sys.schemas sc ON ta.schema_id = sc.schema_id join
<db>.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 ='<schema>' GROUP BY sc.name, ta.name, pa.rows order by
TableName"""
但是,当我尝试从spark sql执行相同的查询时,出现一个错误“ com.microsoft.sqlserver.jdbc.SQLServerException:关键字'WHERE'附近的语法不正确” 如果有人对此错误有任何想法,请帮助我。
下面是我执行的spark sql命令 spark-shell --jars“ /var/lib/sqoop/sqljdbc42.jar”
sqlContext.read.format("jdbc").option("url", "jdbc:sqlserver://<hostname>;database=<dbname>;user=<user>;password=<pwd>").option("dbtable", """(SELECT
ta.name TableName ,pa.rows RowCnt,
COUNT(ins.COLUMN_NAME) ColCnt FROM <db>.sys.tables ta INNER JOIN
<db>.sys.partitions pa ON pa.OBJECT_ID = ta.OBJECT_ID INNER JOIN
<db>.sys.schemas sc ON ta.schema_id = sc.schema_id join
<db>.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 ='<schema>' GROUP BY sc.name,ta.name, pa.rows)""").option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver").load()
预期输出:
TableName,RowCnt,ColCnt
表A,62、30
表B,3846,76
答案 0 :(得分:4)
Spark SQL命令中的问题与dbTable
选项有关。
dbTable
接受可以使用SQL查询的FROM
子句中有效的任何内容。例如,除了完整表之外,您还可以在括号中使用子查询。但是,在括号中使用子查询时,它应该有一个别名。因此,您的命令应修改为
sqlContext
.read
.format("jdbc")
.option("url", "jdbc:sqlserver://<hostname>;database=<dbname>;user=<user>;password=<pwd>")
.option("dbtable",
"""(SELECT
ta.name TableName ,
pa.rows RowCnt,
COUNT(ins.COLUMN_NAME) ColCnt
FROM <db>.sys.tables ta
INNER JOIN
<db>.sys.partitions pa
ON pa.OBJECT_ID = ta.OBJECT_ID
INNER JOIN
<db>.sys.schemas sc
ON ta.schema_id = sc.schema_id
JOIN
<db>.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 ='<schema>'
GROUP BY sc.name,ta.name, pa.rows) as TEMP""")
.option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
.load()
只是预感。希望这会有所帮助!