gpt4 book ai didi

aws-glue - AWSglueContext读取不允许sql查询

转载 作者:行者123 更新时间:2023-12-01 16:53:51 25 4
gpt4 key购买 nike

我想使用 AWS 胶水作业从 Mysql 实例读取过滤后的数据。由于粘合 jdbc 连接不允许我下推谓词,因此我尝试在代码中显式创建 jdbc 连接。

我想使用 jdbc 连接对 Mysql 数据库运行带有 where 子句的选择查询,如下所示

import com.amazonaws.services.glue.GlueContext
import org.apache.spark.SparkContext
import org.apache.spark.sql.SparkSession


object TryMe {

def main(args: Array[String]): Unit = {
val sc: SparkContext = new SparkContext()
val glueContext: GlueContext = new GlueContext(sc)
val spark: SparkSession = glueContext.getSparkSession

// Read data into a DynamicFrame using the Data Catalog metadata
val t = glueContext.read.format("jdbc").option("url","jdbc:mysql://serverIP:port/database").option("user","username").option("password","password").option("dbtable","select * from table1 where 1=1").option("driver","com.mysql.jdbc.Driver").load()

}
}

由于错误而失败

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 'select * from table1 where 1=1 WHERE 1=0' at line 1

这不应该起作用吗?如何使用 JDBC 连接检索过滤后的数据而不将整个表读入数据框?

最佳答案

我认为出现问题是因为您没有使用括号中的查询并提供别名。在我看来,它应该类似于以下示例:

 val t = glueContext.read.format("jdbc").option("url","jdbc:mysql://serverIP:port/database").option("user","username").option("password","password").option("dbtable","(select * from table1 where 1=1) as t1").option("driver","com.mysql.jdbc.Driver").load()

有关 SQL 数据源中的参数的更多信息:

https://spark.apache.org/docs/latest/sql-data-sources-jdbc.html

说到Glue以及Glue提供的框架,还有“push_down_predicate”选项,但我只在基于S3的数据源上使用过这个选项。我认为除了 S3 和非分区数据之​​外,它不适用于其他来源。

https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-partitions.html

关于aws-glue - AWSglueContext读取不允许sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54094382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com