Spark SQL - 使用 JDBC 加载数据,使用 SQL 语句,而不是表名 [英] Spark SQL - load data with JDBC using SQL statement, not table name
本文介绍了Spark SQL - 使用 JDBC 加载数据,使用 SQL 语句,而不是表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想我遗漏了一些东西,但不知道是什么.我想使用 SQLContext 和 JDBC 使用特定的 sql 语句加载数据喜欢
I think I am missing something but can't figure what. I want to load data using SQLContext and JDBC using particular sql statement like
select top 1000 text from table1 with (nolock)
where threadid in (
select distinct id from table2 with (nolock)
where flag=2 and date >= '1/1/2015' and userid in (1, 2, 3)
)
我应该使用哪种 SQLContext 方法?我看到的例子总是指定表名和上下边距.
Which method of SQLContext should I use? Examples I saw always specify table name and lower and upper margin.
提前致谢.
推荐答案
您应该将有效的子查询作为 dbtable
参数传递.例如在 Scala 中:
You should pass a valid subquery as a dbtable
argument. For example in Scala:
val query = """(SELECT TOP 1000
-- and the rest of your query
-- ...
) AS tmp -- alias is mandatory*"""
val url: String = ???
val jdbcDF = sqlContext.read.format("jdbc")
.options(Map("url" -> url, "dbtable" -> query))
.load()
<小时>
* Hive 语言手册子查询:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SubQueries
这篇关于Spark SQL - 使用 JDBC 加载数据,使用 SQL 语句,而不是表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文