java - Vertica Jdbc 驱动程序未抛出 - SQL 状态 : 22001 when ERROR: value too long for type

标签 java sql database jdbc vertica

我在使用 Vertica JDBC 驱动程序时遇到问题 我尝试将数据存储到我的数据库中 - 但有时数据根本没有存储,并且我没有收到任何错误,它似乎已成功。当我尝试使用文件时也会发生这种情况。

这似乎是一个主要错误 - 它违背了数据管理的第四大支柱。 附示例代码。

我该如何解决这个问题?

public class VerticaTest {
    public static void main(String[] args) throws Exception
    {
        Class.forName("com.vertica.jdbc.Driver");
        String tableName = "vertica_test1";
        java.sql.Connection connection = DriverManager.getConnection("jdbc:vertica://xyz.qwe.com:5433/DB?tcpKeepAlive=true", "user", "admin");
        java.sql.Statement st = connection.createStatement();
        st.execute("Create table " + tableName + " (test_name varchar(10))");
        st.execute("grant all on " + tableName + " to public ");
        String value = "short";
        if (true) {
            value = "543543543 Sun Aug 11 065650 UTC 207657657650";
        }
        InputStream stream = new ByteArrayInputStream(value.getBytes(StandardCharsets.UTF_8));
        String copy = "COPY " + tableName + " FROM stdin  WITH parser LibCSVParser() abort on error no commit";
        VerticaCopyStream vcs = new VerticaCopyStream((VerticaConnection) connection, copy);
        vcs.start();
        vcs.addStream(stream);
        System.out.println("Reject size ROW IS " + vcs.getRejects().size());
        vcs.execute();
        ResultSet rs = st.executeQuery("SELECT count(1) FROM " + tableName);
        while (rs.next()) {
            int count = rs.getInt(1);
            System.out.println("result = " + count);
        }
        rs.close();
        // st.execute("drop table " + tableName);
    }

}

enter image description here

最佳答案

Vertica 复制语句的行为与传统 SQL INSERT 语句不同。 COPY 不会失败,但报告无法插入的行。

这是明智的:假设您要插入 1000 万行,您可能不希望它因为单行无效而失败。

要处理插入错误,您有多种选择:

  • "ABORT ON ERROR" parameter允许您在发生一个错误时立即使整个 COPY 命令失败
  • 对于 Java,我倾向于使用 INSERT 准备好的语句(在幕后使用 COPY),如 documented here 。该PreparedStatement.executeBatch();返回一个 int 数组,表示每个处理行的成功/失败。
  • 处理 COPY 错误的其他策略有 documented here

关于java - Vertica Jdbc 驱动程序未抛出 - SQL 状态 : 22001 when ERROR: value too long for type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46212460/

相关文章:

sql - 返回计数最高的行(如交易最多的城市,当有多个城市时)

sql - "column ambiguously defined"

mysql - 从 MySQL 中以字符分隔的文本字段中搜索项目

java - 在 Java 中如何将 String 转换为 List<ClassName>?

java - 如何使用 weblogic 服务器在 eclipse 中调试 java web 应用程序

sql - Sybase 插入一个空格来代替空字符串 ''

database - 高性能网站

python - 处理本地文件和更新 Titan 图数据库的最佳方式

java - JaxB 如何将列表的内容编码为单独的标签

java - 两个具有不同参数的同名函数,一个没有作用域解析运算符,另一个有