gpt4 book ai didi

apache-spark - Spark 中 bigint 的兼容数据类型是什么?我们如何将 bigint 转换为 spark 兼容的数据类型?

转载 作者:行者123 更新时间:2023-12-02 22:00:37 28 4
gpt4 key购买 nike

我正在尝试使用 Spark 将数据从 greenplum 移动到 HDFS。我可以从源表中成功读取数据,数据框(greenplum 表)的 spark 推断模式是:

数据框架构:

 je_header_id: long (nullable = true)
je_line_num: long (nullable = true)
last_updated_by: decimal(15,0) (nullable = true)
last_updated_by_name: string (nullable = true)
ledger_id: long (nullable = true)
code_combination_id: long (nullable = true)
balancing_segment: string (nullable = true)
cost_center_segment: string (nullable = true)
period_name: string (nullable = true)
effective_date: timestamp (nullable = true)
status: string (nullable = true)
creation_date: timestamp (nullable = true)
created_by: decimal(15,0) (nullable = true)
entered_dr: decimal(38,20) (nullable = true)
entered_cr: decimal(38,20) (nullable = true)
entered_amount: decimal(38,20) (nullable = true)
accounted_dr: decimal(38,20) (nullable = true)
accounted_cr: decimal(38,20) (nullable = true)
accounted_amount: decimal(38,20) (nullable = true)
xx_last_update_log_id: integer (nullable = true)
source_system_name: string (nullable = true)
period_year: decimal(15,0) (nullable = true)
period_num: decimal(15,0) (nullable = true)

Hive表对应的schema为:

je_header_id:bigint|je_line_num:bigint|last_updated_by:bigint|last_updated_by_name:string|ledger_id:bigint|code_combination_id:bigint|balancing_segment:string|cost_center_segment:string|period_name:string|effective_date:timestamp|status:string|creation_date:timestamp|created_by:bigint|entered_dr:double|entered_cr:double|entered_amount:double|accounted_dr:double|accounted_cr:double|accounted_amount:double|xx_last_update_log_id:int|source_system_name:string|period_year:bigint|period_num:bigint

使用上面提到的 Hive 表模式,我使用逻辑创建了以下 StructType:

def convertDatatype(datatype: String): DataType = {
val convert = datatype match {
case "string" => StringType
case "bigint" => LongType
case "int" => IntegerType
case "double" => DoubleType
case "date" => TimestampType
case "boolean" => BooleanType
case "timestamp" => TimestampType
}
convert
}

准备好的架构:

 je_header_id: long (nullable = true)
je_line_num: long (nullable = true)
last_updated_by: long (nullable = true)
last_updated_by_name: string (nullable = true)
ledger_id: long (nullable = true)
code_combination_id: long (nullable = true)
balancing_segment: string (nullable = true)
cost_center_segment: string (nullable = true)
period_name: string (nullable = true)
effective_date: timestamp (nullable = true)
status: string (nullable = true)
creation_date: timestamp (nullable = true)
created_by: long (nullable = true)
entered_dr: double (nullable = true)
entered_cr: double (nullable = true)
entered_amount: double (nullable = true)
accounted_dr: double (nullable = true)
accounted_cr: double (nullable = true)
accounted_amount: double (nullable = true)
xx_last_update_log_id: integer (nullable = true)
source_system_name: string (nullable = true)
period_year: long (nullable = true)
period_num: long (nullable = true)

当我尝试在数据框架构上应用我的新架构时,出现异常:

java.lang.RuntimeException: java.math.BigDecimal is not a valid external type for schema of bigint

我知道它正在尝试将 BigDecimal 转换为 Bigint 但它失败了,但是谁能告诉我如何将 bigint 转换为 spark 兼容的数据类型?如果不是,我如何修改我的逻辑以在 case 语句中为这个 bigint/bigdecimal 问题提供正确的数据类型?

最佳答案

在这里看到你的问题,似乎你正在尝试将 bigint 值转换为 big decimal,这是不正确的。 Bigdecimal 是一个必须具有固定精度(最大位数)和小数位数(点右侧的位数)的小数。而你的似乎是长期值(value)。

这里不使用BigDecimal 数据类型,而是尝试使用LongType 来正确转换bigint 值。看看这是否解决了您的目的。

关于apache-spark - Spark 中 bigint 的兼容数据类型是什么?我们如何将 bigint 转换为 spark 兼容的数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54632543/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com