Fix ClickHouse HTTP compression issue (#1449)

- Set default compress_algorithm to 'none' to avoid magic value errors when HTTP compression is enabled on server side
- This resolves the issue where enabling http compression in ClickHouse server causes 'Magic is not correct' error in JDBC driver
- The fix ensures compatibility with servers that have <enable_http_compression>1</enable_http_compression> configured
This commit is contained in:
taojinlong
2026-01-31 21:46:35 +08:00
committed by taojinlong
parent c40436e794
commit 49e625fedd

View File

@@ -11,25 +11,51 @@ import org.springframework.stereotype.Component;
public class CK extends DatasourceConfiguration {
private String driver = "com.clickhouse.jdbc.ClickHouseDriver";
private String extraParams = "";
private String compressAlgorithm = "none"; // 默认设置为none以避免HTTP压缩问题
public String getJdbc() {
if (StringUtils.isNoneEmpty(getUrlType()) && !getUrlType().equalsIgnoreCase("hostName")) {
if (!getJdbcUrl().startsWith("jdbc:clickhouse")) {
DEException.throwException("Illegal jdbcUrl: " + getJdbcUrl());
}
// 如果用户提供的JDBC URL中没有压缩算法设置添加默认设置
if (!getJdbcUrl().contains("compress_algorithm") && !getJdbcUrl().contains("enable_http_compression")) {
if (getJdbcUrl().contains("?")) {
return getJdbcUrl() + "&compress_algorithm=" + compressAlgorithm;
} else {
return getJdbcUrl() + "?compress_algorithm=" + compressAlgorithm;
}
}
return getJdbcUrl();
}
StringBuilder jdbcUrl = new StringBuilder();
if (StringUtils.isEmpty(extraParams.trim())) {
return "jdbc:clickhouse://HOSTNAME:PORT/DATABASE"
.replace("HOSTNAME", getLHost().trim())
.replace("PORT", getLPort().toString().trim())
.replace("DATABASE", getDataBase().trim());
jdbcUrl.append("jdbc:clickhouse://")
.append(getLHost().trim())
.append(":")
.append(getLPort().toString().trim())
.append("/")
.append(getDataBase().trim());
} else {
return "jdbc:clickhouse://HOSTNAME:PORT/DATABASE?EXTRA_PARAMS"
.replace("HOSTNAME", getLHost().trim())
.replace("PORT", getLPort().toString().trim())
.replace("DATABASE", getDataBase().trim())
.replace("EXTRA_PARAMS", getExtraParams().trim());
jdbcUrl.append("jdbc:clickhouse://")
.append(getLHost().trim())
.append(":")
.append(getLPort().toString().trim())
.append("/")
.append(getDataBase().trim())
.append("?")
.append(getExtraParams().trim());
}
// 如果URL中还没有压缩算法设置添加默认设置
if (!jdbcUrl.toString().contains("compress_algorithm") && !jdbcUrl.toString().contains("enable_http_compression")) {
if (jdbcUrl.toString().contains("?")) {
jdbcUrl.append("&compress_algorithm=").append(compressAlgorithm);
} else {
jdbcUrl.append("?compress_algorithm=").append(compressAlgorithm);
}
}
return jdbcUrl.toString();
}
}