【问题标题】:nvalid syntax: Create table sortkey auto with initial sortkeys无效语法:使用初始排序键自动创建表排序键
【发布时间】:2021-06-06 13:33:05
【问题描述】:

我正在尝试使用target-redshift 将数据推送到aws-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'))

【问题讨论】:

  • 我刚刚遇到了同样的错误。我的数据管道直到昨天都运行良好,现在我遇到了这个问题。

标签: python-3.x amazon-redshift airflow etl


【解决方案1】:

在您的任务下方某处正在运行此表单的查询。

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;

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

其他方法 如果上述方法失败,则任何避免LIKE 的方法也将起作用。因为这个LIKE 很可能是加载/更新序列的一部分。例如

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

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

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

额外信息: 这个新错误位于 Redshift 方面。 不完全确定原因,但 LIKE 语句正在尝试传输 SORTKEY 并且无法这样做。

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

亚马逊的人提供了这个视图来让你获得完整的 DDL 语句link

逆着视野跑:

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

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

【讨论】:

    猜你喜欢
    • 2012-10-10
    • 2017-06-19
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    相关资源
    最近更新 更多