You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调试AWS Glue写入Redshift时的"String length exceeds DDL Length"错误

解决AWS Glue写入Redshift时"String length exceeds DDL length"错误

错误原因

错误码1204的直接原因是:你要写入Redshift的字符串字段值长度,超过了目标表对应字段在DDL中定义的最大长度。

比如你的代码中,basicChannelGroupingPath映射到Redshift表的channel_grouping字段,如果Redshift里该字段定义为VARCHAR(100),但源数据中存在长度超过100的basicChannelGroupingPath值,就会触发这个错误。

解决方法

1. 调整Redshift目标表的字段长度

  • 先查询目标表的字段定义,确认长度限制:
    SELECT column_name, data_type 
    FROM information_schema.columns 
    WHERE table_schema = 'dw' AND table_name = 'table';
    
  • 如果字段长度不足,修改表结构(根据实际数据的最大长度调整数值):
    ALTER TABLE dw.table ALTER COLUMN channel_grouping TYPE VARCHAR(500);
    

2. 在Glue中截断过长的字符串

如果不想修改Redshift表结构,可以在数据写入前截断超出长度限制的字符串:

from awsglue.dynamicframe import DynamicFrame
from pyspark.sql.functions import col, substring

# 将DynamicFrame转为DataFrame处理
df = applymapping1.toDF()
# 截断channel_grouping字段到目标表允许的长度(示例为100,根据实际情况修改)
df_truncated = df.withColumn("channel_grouping", substring(col("channel_grouping"), 1, 100))
# 转回DynamicFrame
applymapping1_truncated = DynamicFrame.fromDF(df_truncated, glueContext, "applymapping1_truncated")

# 使用处理后的DynamicFrame写入Redshift
datasink2 = glueContext.write_dynamic_frame.from_jdbc_conf(
    frame = applymapping1_truncated,
    catalog_connection = "redshift_test",
    connection_options ={"preactions":"truncate table dw.table;","dbtable": "dw.table", "database": "test","postactions":post_query},
    redshift_tmp_dir = args["TempDir"],
    transformation_ctx = "datasink2"
)

3. 提前校验源数据的字段长度

在处理前统计源数据字符串字段的最大长度,确保后续调整更精准:

from pyspark.sql.functions import col, max, length

# 查看源字段basicChannelGroupingPath的最大长度
df = datasource0.toDF()
df.select(max(length(col("basicChannelGroupingPath")))).show()

根据统计结果,选择调整Redshift表结构或者截断数据。

内容的提问来源于stack exchange,提问作者ben890

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 04:10:28