我已经使用 sqoop 从 Postgres 导入表到 hdfs。我的表有 uuid 字段作为主键,我的命令 sqoop 如下:
sqoop import --connect 'jdbc:postgresql://localhost:5432/mydb' --username postgreuser --password 123456abcA --driver org.postgresql.Driver --table users --map-column-java id=String --target-dir /hdfs/postgre/users --as-avrodatafile --compress -m 2
Run Code Online (Sandbox Code Playgroud)
但我得到了错误:
Import failed: java.io.IOException: org.postgresql.util.PSQLException: ERROR: function min(uuid) does not exist
Run Code Online (Sandbox Code Playgroud)
我尝试执行 sql 命令:SELECT min(id) from users并得到相同的错误。我怎么能修好呢?我使用 Postgres 9.4、hadoop 2.9.0 和 sqoop 1.4.7
我想归功于@robin-salih 的回答,我已经使用它和 min for int 的实现来构建以下代码:
CREATE OR REPLACE FUNCTION min(uuid, uuid)
RETURNS uuid AS $$
BEGIN
IF $2 IS NULL OR $1 > $2 THEN
RETURN $2;
END IF;
RETURN $1;
END;
$$ LANGUAGE plpgsql;
create aggregate min(uuid) (
sfunc = min,
stype = uuid,
combinefunc = min,
parallel = safe,
sortop = operator (<)
);
Run Code Online (Sandbox Code Playgroud)
它几乎相同,但利用了 B 树索引,因此select min(id) from tbl可以在几毫秒内工作。
PS我不是pgsql专家,也许我的代码有点错误,在生产中使用前仔细检查,但我希望它正确使用索引和并行执行。我只是从示例代码中完成的,而不是深入研究 PG 中聚合背后的理论。
小智 -3
这不是 sqoop 的问题。Postgres 不允许 uuid 上的最小/最大。每个 uuid 都是唯一的,不会被认为比其他 uuid 更大/更小。
要在 sqoop 中解决此问题,您可能需要使用其他一些字段作为分割键。我使用created_At时间戳作为我的分割键。
| 归档时间: |
|
| 查看次数: |
7081 次 |
| 最近记录: |