<i id='QdQiP'><tr id='QdQiP'><dt id='QdQiP'><q id='QdQiP'><span id='QdQiP'><b id='QdQiP'><form id='QdQiP'><ins id='QdQiP'></ins><ul id='QdQiP'></ul><sub id='QdQiP'></sub></form><legend id='QdQiP'></legend><bdo id='QdQiP'><pre id='QdQiP'><center id='QdQiP'></center></pre></bdo></b><th id='QdQiP'></th></span></q></dt></tr></i><div id='QdQiP'><tfoot id='QdQiP'></tfoot><dl id='QdQiP'><fieldset id='QdQiP'></fieldset></dl></div>

        <small id='QdQiP'></small><noframes id='QdQiP'>

      1. <legend id='QdQiP'><style id='QdQiP'><dir id='QdQiP'><q id='QdQiP'></q></dir></style></legend>
      2. <tfoot id='QdQiP'></tfoot>

          <bdo id='QdQiP'></bdo><ul id='QdQiP'></ul>

        如何在 jdbc 数据源中使用 dbtable 选项的子查询?

        时间:2023-08-22
          <bdo id='QYDiF'></bdo><ul id='QYDiF'></ul>
            <tbody id='QYDiF'></tbody>

          <i id='QYDiF'><tr id='QYDiF'><dt id='QYDiF'><q id='QYDiF'><span id='QYDiF'><b id='QYDiF'><form id='QYDiF'><ins id='QYDiF'></ins><ul id='QYDiF'></ul><sub id='QYDiF'></sub></form><legend id='QYDiF'></legend><bdo id='QYDiF'><pre id='QYDiF'><center id='QYDiF'></center></pre></bdo></b><th id='QYDiF'></th></span></q></dt></tr></i><div id='QYDiF'><tfoot id='QYDiF'></tfoot><dl id='QYDiF'><fieldset id='QYDiF'></fieldset></dl></div>

          <small id='QYDiF'></small><noframes id='QYDiF'>

            1. <legend id='QYDiF'><style id='QYDiF'><dir id='QYDiF'><q id='QYDiF'></q></dir></style></legend>
                <tfoot id='QYDiF'></tfoot>

                  本文介绍了如何在 jdbc 数据源中使用 dbtable 选项的子查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想使用 Spark 处理来自 JDBC 源的一些数据.但是首先,我想在JDBC端运行一些查询来过滤列和连接表,而不是从JDBC读取原始表,并将查询结果作为表加载到Spark SQL中.

                  I want to use Spark to process some data from a JDBC source. But to begin with, instead of reading original tables from JDBC, I want to run some queries on the JDBC side to filter columns and join tables, and load the query result as a table in Spark SQL.

                  以下加载原始 JDBC 表的语法适用于我:

                  The following syntax to load raw JDBC table works for me:

                  df_table1 = sqlContext.read.format('jdbc').options(
                      url="jdbc:mysql://foo.com:3306",
                      dbtable="mydb.table1",
                      user="me",
                      password="******",
                      driver="com.mysql.jdbc.Driver" # mysql JDBC driver 5.1.41
                  ).load() 
                  df_table1.show() # succeeded
                  

                  根据 Spark 文档(我使用的是 PySpark 1.6.3):

                  According to Spark documentation (I'm using PySpark 1.6.3):

                  dbtable:应该读取的 JDBC 表.请注意,任何有效的可以在 SQL 查询的 FROM 子句中使用.例如,而不是完整的表,您也可以在括号中使用子查询.

                  dbtable: The JDBC table that should be read. Note that 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.

                  所以只是为了实验,我尝试了一些简单的方法:

                  So just for experiment, I tried something simple like this:

                  df_table1 = sqlContext.read.format('jdbc').options(
                      url="jdbc:mysql://foo.com:3306",
                      dbtable="(SELECT * FROM mydb.table1) AS table1",
                      user="me",
                      password="******",
                      driver="com.mysql.jdbc.Driver"
                  ).load() # failed
                  

                  它抛出了以下异常:

                  com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table1 WHERE 1=0' at line 1
                  

                  我还尝试了其他一些语法变体(添加/删除括号、删除as"子句、切换大小写等),但都没有成功.那么正确的语法是什么?在哪里可以找到更详细的语法文档?此外,错误消息中这个奇怪的WHERE 1 = 0"来自哪里?谢谢!

                  I also tried a few other variations of the syntax (add / remove parentheses, remove 'as' clause, switch case, etc) without any luck. So what would be the correct syntax? Where can I find more detailed documentation for the syntax? Besides, where does this weird "WHERE 1=0" in error message come from? Thanks!

                  推荐答案

                  对于在 Spark SQL 中使用 sql 查询从 JDBC 源读取数据,您可以尝试如下操作:

                  For reading data from JDBC source using sql query in Spark SQL, you can try something like this:

                  val df_table1 = sqlContext.read.format("jdbc").options(Map(
                      ("url" -> "jdbc:postgresql://localhost:5432/mydb"),
                      ("dbtable" -> "(select * from table1) as table1"),
                      ("user" -> "me"),
                      ("password" -> "******"),
                      ("driver" -> "org.postgresql.Driver"))
                  ).load()
                  

                  我用 PostgreSQL 试过了.可以根据MySQL修改.

                  I tried it using PostgreSQL. You can modify it according to MySQL.

                  这篇关于如何在 jdbc 数据源中使用 dbtable 选项的子查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:spark从mysql并行读取数据 下一篇:在 Spark 中找不到适合 jdbc 的驱动程序

                  相关文章

                  最新文章

                1. <tfoot id='nVwM8'></tfoot>
                2. <legend id='nVwM8'><style id='nVwM8'><dir id='nVwM8'><q id='nVwM8'></q></dir></style></legend>

                    <bdo id='nVwM8'></bdo><ul id='nVwM8'></ul>
                3. <small id='nVwM8'></small><noframes id='nVwM8'>

                      <i id='nVwM8'><tr id='nVwM8'><dt id='nVwM8'><q id='nVwM8'><span id='nVwM8'><b id='nVwM8'><form id='nVwM8'><ins id='nVwM8'></ins><ul id='nVwM8'></ul><sub id='nVwM8'></sub></form><legend id='nVwM8'></legend><bdo id='nVwM8'><pre id='nVwM8'><center id='nVwM8'></center></pre></bdo></b><th id='nVwM8'></th></span></q></dt></tr></i><div id='nVwM8'><tfoot id='nVwM8'></tfoot><dl id='nVwM8'><fieldset id='nVwM8'></fieldset></dl></div>