redshift 胶水工作 bigint 问题

redshift glue job bigint issue

我有一个redshi9ft 数据库。在数据库中,我创建了一个 table,在 table 中,我有一个 bigint 列。我创建了一个胶水作业来将数据插入到红移中。但问题在于 bigint 字段。它没有插入。 bigint 似乎有些问题。工作代码如下。我正在使用 python 3 和 spark 2.2,

import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job

args = getResolvedOptions(sys.argv, ['TempDir','JOB_NAME'])

sc = SparkContext()
glueContext = GlueContext(sc)
 spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)

datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "test", table_name = 
"tbl_test", transformation_ctx = "datasource0")

applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [("testdata", "string", 
 "testdata", "string"), ("selling", "bigint", "selling", "bigint")], transformation_ctx = "applymapping1")

resolvechoice2 = ResolveChoice.apply(frame = applymapping1, choice = "make_cols", 
 transformation_ctx = "resolvechoice2")

dropnullfields3 = DropNullFields.apply(frame = resolvechoice2, transformation_ctx = 
"dropnullfields3")

 datasink4 = glueContext.write_dynamic_frame.from_jdbc_conf(frame = dropnullfields3, 
 catalog_connection = "redshift_con", connection_options = {"dbtable": "tbl_test", 
 "database": "test"}, redshift_tmp_dir = args["TempDir"], transformation_ctx = "datasink4")
 job.commit()

尝试在 ApplyMapping 调用中将类型转换为 "long"。如果您的粘合作业在写入 Redshift 时没有失败,有时会创建一个具有相同名称和 redshift 数据类型的新列。在这种情况下,selling_long

可以在 jdbc 驱动程序 here 中找到 Spark 到 Redshift 的映射。

|  Spark Type   |        JDBC Type         |
|---------------|--------------------------|
| IntegerType   | INTEGER                  |
| LongType      | BIGINT                   |
| DoubleType    | DOUBLE PRECISION         |
| FloatType     | REAL                     |
| ShortType     | INTEGER                  |
| ByteType      | SMALLINT                 |
| BooleanType   | BOOLEAN                  |
| StringType    | [VARCHAR|TEXT]           |
| TimestampType | TIMESTAMP                |
| DateType      | DATE                     |
| DecimalType   | DECIMAL(precision,scale) |

尝试使用映射:(​​"selling"、"int"、"selling"、"long")

如果这不起作用,您应该 post Glue 目录中的 "tbl_test" 定义。 ApplyMapping 中的第一个类型应该与目录的 table 定义中列出的类型相匹配。

我遇到了类似的问题,原来控制台中 Glue Crawler 创建的胶水 table 上的类型是 'int',而不是 'long',因此需要 ApplyMapping在 Redshift 类型 'bigint'.

的胶水作业中成为 ("fieldName", "int", "fieldName", "long")

有趣的是,它允许我将值保留在 Glue DynamicFrame 中,甚至在我将 ApplyMapping 设置为 ("field", "long", "field", "long"), 但不会将数据写入 Redshift。

希望对您有所帮助!

我们遇到了同样的问题,使用 int 和 bigint 导入值只会导致提供两种数据类型之一。
我们用这个解决方案解决了这个问题:

1) 确保胶水爬虫中的来源 table 的数据类型为 "bigint"
2) 确保这行代码在您的 Glue 作业中:

applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [("testdata", "string", "testdata", "string"), ("selling", "long", "selling", "long")], transformation_ctx = "applymapping1")

3) 在第 2 步和所有内容之后,直到 dropnullfields3(这是我们的最终解决方案),您必须使用以下代码行再次转换为 long

castedFrame = dropnullfields3.resolveChoice(specs = [('selling','cast:long')])

4) 现在您可以简单地将此 DF 用于您的最终加载线:

datasink4 = glueContext.write_dynamic_frame.from_jdbc_conf(frame = castedFrame, catalog_connection = "redshift_con", connection_options = {"dbtable": "tbl_test", "database": "test"}, redshift_tmp_dir = args["TempDir"], transformation_ctx = "datasink4") job.commit()

希望对您有所帮助!