2016-11-15 914 views
0

我在clickhouse表中有String列。 我尝试改变与修改型表UInt32的:如何在clickhouse中使用toUInt32OrZero函数改变列的类型?

 
ALTER TABLE main.abonents 
MODIFY COLUMN 
device_type UInt32 DEFAULT 0 

,但有错误:

 
Received exception from server: 
Code: 6. DB::Exception: Received from 5.200.55.122:9000. DB::Exception: Cannot parse string 'mo' as UInt32: syntax error at begin of string. Note: there are toUInt32OrZero function, which returns zero instead of throwing exception.. 

很显然,clickhouse像“移动”和抛出异常字符串中使用toUint32功能。其建议使用功能toUInt32OrZero来转换类型。

如何使用toUInt32OrZero功能与ALTER TABLE?

回答

0

有没有这样的方式(据我所知)。

你可以用第二个表来实现它。我们来创建一个:

CREATE TABLE main.abonents_new AS main.abonents; 

然后,我们必须在该新表中更改列。 这个表有没有数据,所以不会引发异常:

ALTER TABLE main.abonents_new MODIFY COLUMN device_type UInt32 DEFAULT 0; 

然后,确保没有新的数据写入到main.abonents。当我们将数据传输到新表格时,我们希望保留所有内容。

使用INSERT INTO SELECT查询插入数据。确保以相同的顺序列出所有字段;包裹DEVICE_TYPE的转换器功能(toUInt32OrZero):

INSERT INTO main.abonents_new SELECT field1, field2, ..., toUInt32OrZero(device_type) AS device_type, ..., fieldN FROM main.abonents; 

然后,确保一切是正常的(即行数是一样的,DEVICE_TYPE被等转化为预期),然后重命名表:

RENAME TABLE main.abonents TO main.abonents_old; 
RENAME TABLE main.abonents_new TO main.abonents; 

(或者,您可以用DROP替代旧的表格;尽管如果东西朝南,我会保留旧数据以便恢复)

相关问题