我想在 Hive 中创建一个 auto_increment 列。 我没有在 hive 文档上看到任何关于此的内容,但我发现我们可以使用: UDFRowSequence 来实现这一点。
是否有最新的方法可以做到这一点,或者是否有一种新的、最“简单”的方法?
我已经尝试过了:所以在我的 Java 项目中,我创建了这样的函数:
private static void createAutoIncrFunction() throws SQLException {
Statement stmt = conn.createStatement();
String sql = "create function autoincr as \"org.apache.hadoop.hive.contrib.udf.UDFRowSequence\"";
stmt.execute(sql);
}
该函数的创建正在运行。 但现在我不知道如何使用我尝试过的这个函数创建我的表:
private static void createTableLine() throws SQLException {
String sql = "CREATE TABLE IF NOT EXISTS line(id_line INT autoincr(), "
+ "uid_ticket VARCHAR(64), "
+ "number INT, "
+ "kind INT)";
Statement stmt = conn.createStatement();
stmt.execute(sql);
}
但是它不起作用,所以我的问题是:如何创建带有 auto_increment 列的表以及如何在其中插入数据?
最佳答案
表创建正常。但是在添加时,您可能会使用您创建的函数。
hive> CREATE TABLE increment_table1 (id INT, c1 STRING, c2 STRING, c3 STRING);
hive> INSERT OVERWRITE TABLE increment_table1 SELECT incr() AS inc, id, c1, c2 FROM t1;
您可以使用这个link了解更多信息
关于java - Hive自动增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31181582/