Vertica:重复/主键数据验证

7
我正在尝试在加载期间创建一个验证过程,以检查数据是否重复。然而,Vertica不支持此功能:Vertica仅在运行查询时检查约束违规,而不是在加载数据时检查。要在加载过程中检测约束违规,请使用带有NO COMMIT选项的COPY语句(第667页)。通过在不提交的情况下加载数据,您可以使用ANALYZE_CONSTRAINTS函数对数据进行后装载检查。如果该函数发现约束违规,则可以回滚加载,因为您还没有提交它。
问题在于我无法编写程序来实现此操作。我怀疑我需要一个存储过程,但我不熟悉Vertica的存储过程语法/限制。您能帮忙吗?以下是我的代码:
-- Create a new table. "id" is auto-incremented and "name" must be unique
CREATE TABLE IF NOT EXISTS my_table (
id IDENTITY
, name varchar(50) UNIQUE NOT NULL
, type varchar(20)
, description varchar(200)
);

--Insert a record
begin; 
copy my_table from stdin
abort on error
NO COMMIT; -- this begins the load
name1|type1|description1 --this is the load
\. -- this closes the load
commit;

-- insert the duplicate record
begin; 
copy my_table from stdin
abort on error
NO COMMIT; -- this begins the load
name1|type1|description1 --this is the load
\. -- this closes the load
commit; -- Surprisingly, the load executes successfully! What's going on?!?!

-- Check constraints. We see that there is a failed constraints:
select analyze_constraints('my_table');

我想要进行一些条件逻辑操作。下面是伪代码。你能帮我准备一下Vertica吗?

Begin
load data
if (select count(*) from (select analyze_constraints('my_table')) sub) == 0:
commit
else rollback
3个回答

4
-- Start by Setting Vertica up to rollback and return an error code 
-- if an error is encountered.

\set ON_ERROR_STOP on

-- Load Data here (code omitted since you already have this)


-- Raise an Error condition by selecting 1/0 if any rows were rejected
-- during the load
SELECT
         GET_NUM_REJECTED_ROWS() AS NumRejectedRows
        ,GET_NUM_ACCEPTED_ROWS() AS NumAcceptedRows
;

SELECT 1 / (1-SIGN(GET_NUM_REJECTED_ROWS()));


-- Raise an Error condition if there are duplicates in my_table
SELECT 1 / ( 1 - SIGN( COUNT(*) ) )
FROM ( SELECT  name1,type1,description1
         FROM MY_TABLE
       GROUP BY 1,2,3
       HAVING COUNT(*) > 1 ) AS T1 ;

-- Raise an Error if primary key constraint is violated.
SELECT 1 / ( 1 - SIGN( COUNT(*) ) )
FROM (SELECT  ANALYZE_CONSTRAINTS ('my_table')) AS T1;

COMMIT;    

Doug,谢谢你--直到现在我完全不知道GET_NUM_REJECTED_ROWS函数(或load_streams表)。谢谢! - Peter
在Vertica 5.1中,\set ON_ERROR_STOP on无效: 错误:ERROR: syntax error at or near "" 如果我执行以下操作: SET ON_ERROR_STOP TO 'on',我会收到错误消息:ERROR: Unsupported SET option ON_ERROR_STOP。 - Illarion Kovalchuk

1

Vertica没有存储过程。您需要在Vertica之外以某种编程方式进行操作。

您的伪代码很好; 只需在某些东西中实现它(JAVA,C ++等)。您不需要执行“回滚”。当使用 NO COMMIT 加载数据时,直到执行 COMMIT 语句之前,它不会被提交。


0

使用ansi merge怎么样?

-使用快速批量装载程序将数据上传到临时表 -从临时表合并到基础表


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接