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
4

3 回答 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;    
于 2012-09-29T12:45:06.183 回答
1

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

您拥有的伪逻辑很好;只需在某些东西(JAVA、C++ 等)中实现它。您无需执行“回滚”。加载数据时(使用),在执行语句NO COMMIT之前不会提交。COMMIT

于 2012-09-29T01:24:19.130 回答
0

使用 ansi 合并怎么样?

- 使用快速批量加载器将数据上传到临时表 - 从临时表合并到基表

于 2012-09-30T14:40:10.203 回答