我在使用 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);
}
}
最佳答案
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/