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

标签 aws-glue mssql-jdbc

我想使用 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/

相关文章:

java - JBoss Wildfly应用程序服务器无法连接到SQL Server

sql-server - 如何在 IntelliJ IDEA 12 中使用 MS SQL Server 集成身份验证?

amazon-web-services - aws 胶中的transformation_ctx 有什么用?

java - SQL Server Jdbc 驱动程序无法使用 TLS1.2 连接到数据库

amazon-web-services - 如何将 AWS Glue 作业的输出返回到调用 Step Function 工作流?

python - AWS胶: How to read jdbc source via spark object in SCALA.

java - 如何使用 spring jdbc 存储过程将用户定义的数据类型变量的值从 java 传递到 MS SQL

sql-server - 将触发表上的更新将是递归的

amazon-web-services - AWS Glue + Athena/Hive 是替代复杂 SQL 查询的正确选择吗?

amazon-web-services - 如何使用glue启动AWS cloudformation堆栈?