简体   繁体   English

AWS Redshift alter append 命令不适用于插入数据

[英]AWS Redshift alter append command not working for inserting data

I have to insert records into Redshift table on a periodic basis.我必须定期将记录插入 Redshift 表。 So I chose the strategy where I will copy s3 data into stage table and then append the stage table data into actual table using alter append command.所以我选择了将 s3 数据复制到阶段表中,然后使用 alter append 命令将阶段表数据复制到实际表中的策略 append。 In my case.就我而言。

Stage table - URL_DATA_TEMP阶段表 - URL_DATA_TEMP

Actual Table - URL_METADATA实际表 - URL_METADATA

now both tables I created using the same command and only changing the table name.现在,我使用相同的命令创建了两个表,并且只更改了表名。 ex:前任:

CREATE TABLE _360_model.URL_METADATA
(
URL VARCHAR(8000),
URL_MD5 VARCHAR(300),
INDEX VARCHAR(200),
ASSET_TYPE VARCHAR(200)
);

CREATE TABLE _360_model.URL_DATA_TEMP
(
URL VARCHAR(8000),
URL_MD5 VARCHAR(300),
INDEX VARCHAR(200),
ASSET_TYPE VARCHAR(200)
);

Still when I try to use following append command, it complains.仍然当我尝试使用以下 append 命令时,它会抱怨。

alter table _360_model.URL_METADATA append from _360_model.URL_DATA_TEMP;

error:  Columns don't match.
code:      8001
context:   Column "asset_type" has different attributes in the source table 
and the target table. Columns with the same name must have the same 
attributes in both tables.

I am not able to understand when I used almost same command to create table, how can column structure can be different.我无法理解当我使用几乎相同的命令创建表时,列结构怎么会不同。

This may be from column compression differences. 这可能来自列压缩差异。 On the copy command, are you using the option "COMPUPDATE ON" as shown below? 在复制命令中,您是否使用“COMPUPDATE ON”选项,如下所示? If so, it is likely your staging table's column compression options are different than your target table. 如果是这样,您的临时表的列压缩选项可能与目标表不同。

copy <table name> from 's3://<data location>'
CREDENTIALS <creds>
region <region>
GZIP
CSV
IGNOREHEADER 1
TIMEFORMAT 'auto' manifest
COMPUPDATE ON;

I ran into a similar issue and did the following. 我遇到了类似的问题并做了以下事情。

1) Load data with a copy command and turn COMPUPDATE ON 1)使用复制命令加载数据并打开COMPUPDATE

See: http://docs.aws.amazon.com/redshift/latest/dg/copy-parameters-data-load.html#copy-compupdate 请参阅: http//docs.aws.amazon.com/redshift/latest/dg/copy-parameters-data-load.html#copy-compupdate

2) Afer the load is complete, use the query below to see the automatic compression applied. 2)加载完成后,使用下面的查询查看应用的自动压缩。

select "column", type, encoding, distkey, sortkey
from pg_table_def where tablename = '<table name>';

See: http://docs.aws.amazon.com/redshift/latest/dg/t_Compressing_data_on_disk.html 请参阅: http//docs.aws.amazon.com/redshift/latest/dg/t_Compressing_data_on_disk.html

3) Recreate your target table using the same compression as the staging table. 3)使用与登台表相同的压缩重新创建目标表。

create table <target table name> (
    <column name> <type> encode <encoding>,
    ...
)

See: http://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html 请参阅: http//docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html

Other table attributes such as sortkey will need to match in both your staging and target tables. 其他表属性(如sortkey)需要在staging和target表中匹配。

I'm not able to recreate the issue with the SQL you've provided (using psql ). 我无法使用您提供的SQL重新创建问题(使用psql )。

Try running your SQL with psql to see if your tool is altering the submitted SQL in some way. 尝试使用psql运行SQL,看看你的工具是否以某种方式改变了提交的SQL。

This may be due to the different compression types of the target and staging table.这可能是由于目标表和暂存表的压缩类型不同所致。 Sometimes due to the different sortkeys in the target and staging table it happens.有时由于目标表和登台表中的排序键不同,它会发生。 Steps to resolve it:解决步骤:

  1. In redshift: show table 'target table name'.在 redshift 中:显示表“目标表名称”。 This will generate the DDL statement of the target table name这样会生成目标表名的DDL语句

  2. Drop the staging table: drop table 'staging table name'删除临时表:drop table 'staging table name'

  3. Recreate the staging table using the DDL command generated from the 1 command, just replace the table name with the staging table name使用1命令生成的DDL命令重新创建暂存表,只需将表名替换为暂存表名即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM