【问题标题】:Clickhouse Batch Execution not inserting new rowsClickhouse批处理执行不插入新行
【发布时间】:2021-06-08 16:41:15
【问题描述】:

我刚刚在本地安装了一个clickhouse服务器,包clickhouse-serverlinux包。 我创建了一些 java 代码来在表中插入 N 行,它通过 JDBC 运行良好。 但是为了提高性能,我现在使用批处理而不是单独的 INSERTS 来实现它,但是下面的代码不起作用,并且 executeBatch 函数返回一个长度为 0 的数组。

出于测试目的,我创建了以下代码:

    public static void main(String[] args) throws PulsarClientException, SQLException {

       Connection con = null;
       String connectionString = "jdbc:clickhouse://localhost:8123/bank";

        try {
            con = DriverManager.getConnection(connectionString);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
     
        String query = "INSERT INTO bank.test (numero,palavra) VALUES (3,'girassol');";

        Statement stmt = con.createStatement();

        con.setAutoCommit(false);      
        try {
            stmt.addBatch(query);
            stmt.addBatch(query);
            stmt.executeBatch();

            con.commit()
        
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        con.close();

    }

stmt.executeBatch(); 返回 0 个元素,并且没有任何内容插入到 clickhouse 服务器上。我只是通过替换连接字符串在 Postgres DB 上测试了相同的代码,它按预期工作。 我在 clickhouse 上是否缺少任何配置?

【问题讨论】:

  • 这里一切都错了。 CH 不支持事务。就 Clickhouse 而言,它不是批量插入。 Clickhouse 不返回插入的行数(它始终为 0)。
  • 是的,我必须改变的只是使用preparedstatements而不是statemtns,就像在文档中一样

标签: java jdbc clickhouse


【解决方案1】:

尝试预处理插入数据:

// Setting Preprocessing Strings
String query = "INSERT INTO bank.test (numero,palavra) VALUES (?,?);";

然后,插入每个参数,并将参数添加到批处理中,并提交参数:

PreparedStatement stmt = con.preparedStatement(query);
// setting first parameter
stmt.setInt(1, 3);
// setting second parameter
stmt.setString(2, "girassol");
stmt.addBatch();
stmt.executeBatch();

con.commit();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 2015-04-12
    • 1970-01-01
    • 2021-04-06
    • 2019-01-01
    相关资源
    最近更新 更多