【发布时间】:2018-08-01 19:01:54
【问题描述】:
我有一列大于 varchar(max) 数据类型,据我了解,这是 AWS Glue 使用的最大数据类型,当我尝试加载我的因为它的桌子。我不想截断该列,因为它并不是那么重要,并且无法弄清楚如何在 Glue 中做到这一点。我知道如果我在 EC2 实例中使用 psql 连接到我的数据库并且实际上可以让我的表以这种方式成功加载,我可以使用 TRUNCATECOLUMNS 作为复制命令上的标签。但是,我的老板坚持要我使用 Glue 来完成这项工作,所以我正在寻找一种使用 Glue 脚本截断列的方法。我浏览了很多文档,但找不到类似的东西。谢谢。
这里有一些工作代码,供其他可能遇到此问题并需要完整参考的人使用。请注意,varchar(65535) 是 Redshift 中一列可以包含的最大字符数:
val truncColUdf = udf((str: String) => if (str.length > 29999) str.substring(0, 29999) else str)
val datasource30 = glueContext.getCatalogSource(database = "database", tableName = "entry", redshiftTmpDir = "", transformationContext = "datasource30").getDynamicFrame()
val revDF30 = datasource30.toDF()
.withColumn("message", truncColUdf(col("message")))
val truncDynamicFrame30 = DynamicFrame(revDF30, glueContext)
val applymapping30 = truncDynamicFrame30.applyMapping(mappings = Seq(("id", "bigint", "id", "bigint"), ("message", "string", "message", "varchar(65535)"), ("state", "string", "state", "varchar(256)"), ("created_at", "timestamp", "created_at", "timestamp"), ("depth", "int", "depth", "int")), caseSensitive = false, transformationContext = "applymapping30")
val resolvechoice30 = applymapping30.resolveChoice(choiceOption = Some(ChoiceOption("make_cols")), transformationContext = "resolvechoice30")
val dropnullfields30 = resolvechoice30.dropNulls(transformationContext = "dropnullfields30")
val datasink30 = glueContext.getJDBCSink(catalogConnection = "databaseConnection", options = JsonOptions("""{"dbtable": "entry", "database": "database"}"""), redshiftTmpDir = args("TempDir"), transformationContext = "datasink30").writeDynamicFrame(dropnullfields30)
这是正在读取的示例数据行:
01,"<p>Here is the message where the quotations are in case of commas within the message, like so.</p>",active,2017-08-27 23:38:40,1
【问题讨论】:
标签: amazon-web-services amazon-redshift aws-glue