【问题标题】:How Can I use spring Jdbc template or others batch insert data to hive table with a partition?如何使用 spring Jdbc 模板或其他批量插入数据到带有分区的配置单元表?
【发布时间】:2020-04-02 09:10:16
【问题描述】:

如题,我想通过spring NamedParameterJbbcTemplate批量插入数据到hive表中,可以这样工作:

SqlParameterSource[] batchParameterSources = SqlParameterSourceUtils.createBatch(batchValues);
template.batchUpdate("insert into table(a, b, c) values(:a, :b, :c)", batchParameterSources);

但是,上面的sql不能分配分区,使用sql

"insert overwrite table partition(date = '2020-04-02') values (:a, :b, :c)"

spring 会抛出 SQLFeatureNotSupportException: Method not supported。

那么如何使用spring jdbc模板批量插入数据到具有特殊分区的hive表中呢?

期待您的回答, 非常感谢!

【问题讨论】:

  • 我尝试使用insert into table(a, b, c) partition(date = '2020-04-02') values (:a, :b, :c),还是不行。

标签: spring hive jdbctemplate partition batch-insert


【解决方案1】:

我研究了 Spring 教程,但没有找到关于批量插入到具有特殊分区的配置单元表的信息。

所以我使用原生的 hive 驱动。

您可以编写如下代码:

try {
    Class.forName("org.apache.hive.jdbc.HiveDriver");
    Connection connection = DriverManager.getConnection("hiveserver", "username", "password");
    Statement statement = connection.createStatement();
    boolean result = statement.execute("insert overwrite table table_name partition(dt = 'date') values (valueA), (valueB), (valueC)");
    if (!result) {
        log.info("write data successfully");
    }
}

注意:

  1. 如果你存储了一些数据,statement.execute()方法会返回false。因为存储不会返回任何结果。
  2. 如果你的数据太多,最好批量存储,比如
insert overwrite table_name partition(dt='date') values (valueA), (valueB)...
insert into table_name partition(dt='date') values (valueC), (valueD)...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 2017-07-02
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多