我正在尝试使用 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 对它的理解有些奇怪。
Amazon 人员提供了此 View 以允许您获取完整的表 DDL 语句 link
对着 View 运行:
SELECT * FROM
WHERE schemaname = 'schema' AND tablename = 'tablename'
可能会展示一些有用的东西。在我的例子中,SORTKEY 和 DISTKEY 都抛出错误:对我来说,这表明 Redshift 对键的内部理解存在缺陷。更重要的是 - 在我的例子中,我从未设置过这些键,所以我认为它们是由 Redshift 暗示的。
关于python-3.x - 无效语法 : Create table sortkey auto with initial sortkeys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66533721/