函数 min(uuid) 在 postgresql 中不存在 [英] Function min(uuid) does not exist in postgresql

查看:215
本文介绍了函数 min(uuid) 在 postgresql 中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 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

但是我得到了错误:

导入失败:java.io.IOException:org.postgresql.util.PSQLException:错误:函数 min(uuid) 不存在

我尝试执行 sql 命令:SELECT min(id) from users 并得到相同的错误.我怎么能修好呢?我使用 Postgres 9.4、hadoop 2.9.0 和 sqoop 1.4.7

解决方案

我想归功于 @robin-salih 的回答,我已经使用它和 min for int 的实现来构建以下代码:

>

创建或替换函数 min(uuid, uuid)返回 uuid 为 $$开始如果 $2 为空或 $1 >2美元然后返还 2 美元;万一;返还 1 美元;结尾;$$ 语言 plpgsql;创建聚合 min(uuid) (sfunc = 分钟,stype = uuid,combinefunc = 分钟,平行=安全,sortop = 运算符 (<));

几乎一样,但利用了 B-tree 索引,所以 select min(id) from tbl 在几毫秒内工作.

P.S. 我不是 pgsql 专家,也许我的代码有问题,在生产中使用前请仔细检查,但我希望它正确使用索引和并行执行.我只是根据示例代码制作的,没有深入研究 PG 聚合背后的理论.

I have imported tables from Postgres to hdfs by using sqoop. My table have uuid field as primary key and my command sqoop as below:

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

But I got the error:

Import failed: java.io.IOException: org.postgresql.util.PSQLException: ERROR: function min(uuid) does not exist

I tried executed the sql command: SELECT min(id) from users and got the same error. How could I fix it ? I use Postgres 9.4, hadoop 2.9.0 and sqoop 1.4.7

解决方案

I'd like to credit @robin-salih 's answer, I've used it and implementation of min for int, to build following code:

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 (<)
);

It almost the same, but takes advantages of B-tree index, so select min(id) from tbl works in few millis.

P.S. I'm not pgsql expert, perhaps my code is somehow wrong, double check before use in production, but I hope it uses indexes and parallel execution correctly. I've made it just from sample code, not digging into theory behind aggregates in PG.

这篇关于函数 min(uuid) 在 postgresql 中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆