语法无效:使用初始排序键创建表排序键 auto

2024-03-18

我正在尝试使用target-redshift将数据推送到aws-redshift

https://pypi.org/project/target-redshift/ https://pypi.org/project/target-redshift/

我在用airflow监控etl状态

这是错误日志,我不知道它是什么意思。 target-redshift 的在线文档几乎不存在。有什么办法可以解决这个错误吗?

[2021-03-07 15:04:11,841] {bash_operator.py:126} INFO - ERROR Exception writing records
[2021-03-07 15:04:11,841] {bash_operator.py:126} INFO - Traceback (most recent call last):
[2021-03-07 15:04:11,841] {bash_operator.py:126} INFO -   File "/usr/local/airflow/.virtualenvs/target-redshift/lib/python3.7/site-packages/target_postgres/postgres.py", line 300, in write_batch
[2021-03-07 15:04:11,841] {bash_operator.py:126} INFO -     {'version': target_table_version})
[2021-03-07 15:04:11,841] {bash_operator.py:126} INFO -   File "/usr/local/airflow/.virtualenvs/target-redshift/lib/python3.7/site-packages/target_postgres/sql_base.py", line 840, in write_batch_helper
[2021-03-07 15:04:11,841] {bash_operator.py:126} INFO -     metadata)
[2021-03-07 15:04:11,841] {bash_operator.py:126} INFO -   File "/usr/local/airflow/.virtualenvs/target-redshift/lib/python3.7/site-packages/target_postgres/postgres.py", line 588, in write_table_batch
[2021-03-07 15:04:11,841] {bash_operator.py:126} INFO -     table=sql.Identifier(remote_schema['name'])
[2021-03-07 15:04:11,841] {bash_operator.py:126} INFO -   File "/usr/local/airflow/.virtualenvs/target-redshift/lib/python3.7/site-packages/target_postgres/postgres.py", line 65, in execute
[2021-03-07 15:04:11,841] {bash_operator.py:126} INFO -     return super(_MillisLoggingCursor, self).execute(query, vars)
[2021-03-07 15:04:11,841] {bash_operator.py:126} INFO -   File "/usr/local/airflow/.virtualenvs/target-redshift/lib/python3.7/site-packages/psycopg2/extras.py", line 461, in execute
[2021-03-07 15:04:11,841] {bash_operator.py:126} INFO -     return super(LoggingCursor, self).execute(query, vars)
[2021-03-07 15:04:11,842] {bash_operator.py:126} INFO - psycopg2.errors.InternalError_: Invalid syntax: Create table sortkey auto with initial sortkeys.
[2021-03-07 15:04:11,842] {bash_operator.py:126} INFO - 
[2021-03-07 15:04:11,842] {bash_operator.py:126} INFO - CRITICAL ('Exception writing records', InternalError_('Invalid syntax: Create table sortkey auto with initial sortkeys.\n'))

在您的任务下面的某处正在运行这种形式的查询。

CREATE TABLE schema.tablename_copy 
(LIKE schema.tablename)

这就是引发错误的原因Invalid syntax: Create table sortkey auto with initial sortkeys.

修复或删除原始表的 SORTKEY 和 DISTSTYLE 可解决该问题。例如。

ALTER TABLE schema.tablename ALTER SORTKEY NONE;
ALTER TABLE schema.tablename ALTER DISTSTYLE EVEN;

根据您的表实际需要(在排序和分布键方面),您可能会做其他事情。然而,这很可能是一些加载表并且没有 SORTKEY 等应该不是什么大问题。

其他方法如果上述方法失败,可以采取多种避免方法LIKE也会起作用。从此LIKE很可能是加载/更新插入序列的一部分。例如

  • 制作目标表的临时副本(使用 LIKE 语句)
  • 将新数据加载到临时表中
  • 从目标表中删除新数据中包含 ID 的行
  • 将新数据插入目标表
  • 清理临时表。

Airflow 可能会在幕后为您执行这一系列 SQL 查询。您可以将 Airflow 的步骤拆解为单独的任务,并避免使用LIKE.

  1. 删除原始表(根据用例可能是可能的)-我发现在重新创建表时,LIKE 的问题消失了。
  2. 使用普通的 CREATE 语句显式创建重复表(而不是依赖于 LIKE)
  3. 考虑直接对表执行 INSERT / APPEND / COPY 并稍后处理重复项

额外信息:这个新错误位于红移侧。 不完全确定原因,但LIKE语句正在尝试传输 SORTKEY,但无法执行此操作。

如果你检查原始表的定义schema.tablename您可能会发现 Redshifts 对它的理解有些奇怪。

Amazon 的人提供了这个视图来让你获得全表 DDL 语句link https://github.com/awslabs/amazon-redshift-utils/blob/master/src/AdminViews/v_generate_tbl_ddl.sql

针对视图运行:

SELECT * FROM 
WHERE schemaname = 'schema' AND tablename = 'tablename'

也许会展示一些有用的东西。就我而言,SORTKEY 和 DISTKEY 都抛出错误:对我来说,这表明 Redshift 对键的内部理解存在缺陷。更重要的是 - 就我而言,我从未设置过这些键,所以我认为它们是由 Redshift 隐含的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

语法无效:使用初始排序键创建表排序键 auto 的相关文章

随机推荐