您可以通过开源的 MySQL JDBC 驱动程序连接到 ByteHouse 云数仓版。
<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.3.0</version> </dependency>
通过 ClickHouse JDBC Driver 连接 ByteHouse 的字符串信息如下,其中 {HOST}
以及另外需要的验证密钥 {API_KEY}
信息请参考 获取 ByteHouse 连接信息获取,{Port}
默认值为3306。
jdbc:mysql://{HOST}:{Port}/{DATABASE_NAME}
您可以参考下面的代码示例来进行数据插入与查询,注意替换第15行至第19行的连接信息。
import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.sql.*; public class TestMySQLConnection { private Connection connection; @BeforeClass private void beforeClass() throws SQLException, ClassNotFoundException { String host = "{HOST}"; int port = 3306; String user = "bytehouse"; String password = "{API_KEY}";//API_key密钥 String database = "{DATABASE_NAME}"; //数据库名称 String url = String.format("jdbc:mysql://%s:%d/%s", host, port, database); Class.forName(com.mysql.cj.jdbc.Driver.class.getName()); connection = DriverManager.getConnection(url, user, password); } @AfterClass public void tearDown() throws SQLException { // 关闭数据库连接 if (connection != null) { connection.close(); } } @Test public void testShowDatabases() { String sql = "show databases;"; try { Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql); System.out.println(toCSV(resultSet)); statement.close(); } catch (SQLException e) { Assert.fail(String.format("Failed to execute: %s:%s", sql, e.getMessage())); } } private String toCSV(ResultSet resultSet) throws SQLException { StringBuilder builder = new StringBuilder(); // 获取列名 for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) { builder.append(resultSet.getMetaData().getColumnLabel(i)); if (i < resultSet.getMetaData().getColumnCount()) { builder.append(","); } else { builder.append('\n'); } } // 获取数据 int columnCount = resultSet.getMetaData().getColumnCount(); while (resultSet.next()) { for (int colIndex = 1; colIndex <= columnCount; colIndex++) { Object result = resultSet.getObject(colIndex); if (null == result) { builder.append("\\N"); } else { builder.append(resultSet.getString(colIndex)); } if (colIndex < resultSet.getMetaData().getColumnCount()) { builder.append(","); } } builder.append('\n'); } return builder.toString(); } }
异常描述 | 解决方案 |
---|---|
DB::Exception: No local available worker group for vw-xxxx | 请在 计算组 页面,确认 默认计算组的 状态是否在 |
com.mysql.cj.exceptions.CJException: unknown error: Code: UNAUTHENTICATED | 请确认配置的 |
DB::Exception: Database | 请确认连接串里数据库名称是否配置正确 |