无效语法:使用初始排序键创建表排序键自动
我正在尝试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'))
回答
在您的任务下,正在运行这种形式的查询。
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.
- 删除原始表(可能取决于用例) - 我发现在重新创建表时 LIKE 的问题消失了。
- 使用普通的 CREATE STATEMENT(而不是依赖于 LIKE)显式创建重复表
- 考虑直接对表执行 INSERT / APPEND / COPY 并稍后处理重复项
额外信息:
这个新错误在Redshift 端。不完全确定原因,但该LIKE语句正在尝试传输 SORTKEY 并且无法这样做。
如果您检查原始表的定义,schema.tablename您可能会发现 Redshifts 对它的理解有些奇怪。
亚马逊的家伙提供了这个视图,让你获得完整的表 DDL 语句链接
反对观点:
SELECT * FROM
WHERE schemaname = 'schema' AND tablename = 'tablename'
也许会显示一些有用的东西。在我的情况下,SORTKEY 和 DISTKEY 都抛出错误:对我来说,这表明 Redshift 对密钥的内部理解有缺陷。更重要的是 - 就我而言,我从未设置过这些键,所以我认为它们是 Redshift 暗示的。