Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify sql dialect when creating spark dataframe from JDBC?

I'm having an issue reading data via custom JDBC with Spark. How would I go about about overriding the sql dialect inferred via jdbc url?

The database in question is vitess (https://github.com/youtube/vitess) which runs a mysql variant, so I want to specify a mysql dialect. The jdbc url begins with jdbc:vitess/

Otherwise the DataFrameReader is inferring a default dialect which uses """ as a quote identifier. As a result, queries via spark.read.jdbc get sent as

Select 'id', 'col2', col3', 'etc' from table

which selects the string representations instead of the column values instead of

Select id, col2, col3, etc from table

like image 714
Smith Avatar asked Oct 21 '25 16:10

Smith


1 Answers

Maybe it's too late. But answer will be next:

Create your custom dialect, as I did for ClickHouse database(my jdbc connection url looks like this jdbc:clickhouse://localhost:8123)

 private object ClickHouseDialect extends JdbcDialect {
    //override here quoting logic as you wish
    override def quoteIdentifier(colName: String): String = colName

    override def canHandle(url: String): Boolean = url.startsWith("jdbc:clickhouse")
  }

And register it somewhere in your code, like this:

JdbcDialects.registerDialect(ClickHouseDialect)
like image 61
Aleksander Melnichnikov Avatar answered Oct 26 '25 22:10

Aleksander Melnichnikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!