<bdo id='VKQ14'></bdo><ul id='VKQ14'></ul>
  • <legend id='VKQ14'><style id='VKQ14'><dir id='VKQ14'><q id='VKQ14'></q></dir></style></legend>

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

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

      1. 在 Apache Spark 中连接到 SQLite

        时间:2023-08-22
          <tbody id='OxeNC'></tbody>
          <tfoot id='OxeNC'></tfoot>

              • <bdo id='OxeNC'></bdo><ul id='OxeNC'></ul>

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

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

                  本文介绍了在 Apache Spark 中连接到 SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想对 SQLite 数据库中的所有表运行自定义函数.该功能或多或少相同,但取决于单个表的架构.此外,表及其模式仅在运行时才知道(调用程序时使用指定数据库路径的参数).

                  I want to run a custom function on all tables in a SQLite database. The function is more or less the same, but depends on the schema of the individual table. Also, the tables and their schemata are only known at runtime (the program is called with an argument that specifies the path of the database).

                  这是我目前所拥有的:

                  val conf = new SparkConf().setAppName("MyApp")
                  val sc = new SparkContext(conf)
                  val sqlContext = new org.apache.spark.sql.SQLContext(sc)
                  
                  // somehow bind sqlContext to DB
                  
                  val allTables = sqlContext.tableNames
                  
                  for( t <- allTables) {
                      val df = sqlContext.table(t)
                      val schema = df.columns
                      sqlContext.sql("SELECT * FROM " + t + "...").map(x => myFunc(x,schema))
                  }
                  

                  我目前发现的唯一提示需要提前知道表格,在我的场景中不是这样的:

                  The only hint I found so far needs to know the table in advance, which is not the case in my scenario:

                  val tableData = 
                    sqlContext.read.format("jdbc")
                      .options(Map("url" -> "jdbc:sqlite:/path/to/file.db", "dbtable" -> t))
                      .load()
                  

                  我使用的是 xerial sqlite jdbc 驱动程序.那么我怎样才能只连接到一个数据库,而不是一个表呢?

                  I am using the xerial sqlite jdbc driver. So how can I conntect solely to a database, not to a table?

                  使用 Beryllium 的答案作为开始,我将代码更新为:

                  Using Beryllium's answer as a start I updated my code to this:

                  val sqlContext = new org.apache.spark.sql.SQLContext(sc)
                  
                  val metaData = sqlContext.read.format("jdbc")
                      .options(Map("url" -> "jdbc:sqlite:/path/to/file.db",
                                   "dbtable" -> "(SELECT * FROM sqlite_master) AS t")).load()
                  
                  val myTableNames = metaData.select("tbl_name").distinct()
                  
                  for (t <- myTableNames) {
                      println(t.toString)
                  
                      val tableData = sqlContext.table(t.toString)
                  
                      for (record <- tableData.select("*")) {
                          println(record)
                      }
                  }
                  

                  至少我可以在运行时读取表名,这对我来说是一个巨大的进步.但是我看不懂表格.我两个都试了

                  At least I can read the table names at runtime which is a huge step forward for me. But I can't read the tables. I tried both

                  val tableData = sqlContext.table(t.toString)
                  

                  val tableData = sqlContext.read.format("jdbc")
                      .options(Map("url" -> "jdbc:sqlite:/path/to/file.db",
                                   "dbtable" -> t.toString)).load()
                  

                  在循环中,但在这两种情况下,我都会收到 NullPointerException.虽然我可以打印表名,但似乎我无法连接到它们.

                  in the loop, but in both cases I get a NullPointerException. Although I can print the table names it seems I cannot connect to them.

                  最后但并非最不重要的一点是,我总是收到 SQLITE_ERROR: Connection is closed 错误.它看起来与此问题中描述的问题相同:SQLITE_ERROR: 当通过 JDBC 从 Spark 连接到 SQLite 数据库时,连接被关闭

                  Last but not least I always get an SQLITE_ERROR: Connection is closed error. It looks to be the same issue described in this question: SQLITE_ERROR: Connection is closed when connecting from Spark via JDBC to SQLite database

                  推荐答案

                  您可以尝试两种选择

                  • 在您的 Spark 作业中打开一个单独的普通 JDBC 连接
                  • 从 JDBC 元数据中获取表名
                  • 将这些融入您的 for 理解

                  您可以将查询指定为 dbtable 参数的值.在语法上,这个查询必须看起来"像一个表,所以它必须包含在一个子查询中.

                  You can specify a query as the value for the dbtable argument. Syntactically this query must "look" like a table, so it must be wrapped in a sub query.

                  在该查询中,从数据库中获取元数据:

                  In that query, get the meta data from the database:

                  val df = sqlContext.read.format("jdbc").options(
                    Map(
                      "url" -> "jdbc:postgresql:xxx",
                      "user" -> "x",
                      "password" -> "x",
                      "dbtable" -> "(select * from pg_tables) as t")).load()
                  

                  此示例适用于 PostgreSQL,您必须将其调整为适用于 SQLite.

                  This example works with PostgreSQL, you have to adapt it for SQLite.

                  更新

                  似乎JDBC驱动程序只支持迭代一个结果集.无论如何,当您使用 collect() 来具体化表名列表时,以下代码段应该可以工作:

                  It seems that the JDBC driver only supports to iterate over one result set. Anyway, when you materialize the list of table names using collect(), then the following snippet should work:

                  val myTableNames = metaData.select("tbl_name").map(_.getString(0)).collect()
                  
                  for (t <- myTableNames) {
                    println(t.toString)
                  
                    val tableData = sqlContext.read.format("jdbc")
                      .options(
                        Map(
                          "url" -> "jdbc:sqlite:/x.db",
                          "dbtable" -> t)).load()
                  
                    tableData.show()
                  }
                  

                  这篇关于在 Apache Spark 中连接到 SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Pyspark DataFrameWriter jdbc 函数的 ignore 选项是忽略整 下一篇:在“GROUP BY"中重用选择表达式的结果;条款

                  相关文章

                  最新文章

                  <legend id='Diorr'><style id='Diorr'><dir id='Diorr'><q id='Diorr'></q></dir></style></legend>

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

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

                    <tfoot id='Diorr'></tfoot>
                  1. <small id='Diorr'></small><noframes id='Diorr'>