【发布时间】:2018-08-07 14:27:15
【问题描述】:
我正在尝试将 RDBMS 表引入 Hive。我通过以下方式获得了数据框:
val yearDF = spark.read.format("jdbc").option("url", connectionUrl)
.option("dbtable", "(select * from schema.tablename where source_system_name='DB2' and period_year='2017') as year2017")
.option("user", devUserName)
.option("password", devPassword)
.option("numPartitions",15)
.load()
这些是数据框的列:
geography:string|
project:string|
reference_code:string
product_line:string
book_type:string
cc_region:string
cc_channel:string
cc_function:string
pl_market:string
ptd_balance:double
qtd_balance:double
ytd_balance:double
xx_last_update_tms:timestamp
xx_last_update_log_id:int
xx_data_hash_code:string
xx_data_hash_id:bigint
ptd_balance, qtd_balance, ytd_balance 列是双精度数据类型。我们的项目希望通过创建新列将其数据类型从 Double 转换为 String:ptd_balance_text, qtd_balance_text, ytd_balance_text 使用相同的数据以避免任何数据截断。
withColumn 将在数据框中创建一个新列。
withColumnRenamed 将重命名现有列。
数据框有近 1000 万条记录。 有没有一种有效的方法来创建多个具有相同数据和不同类型的新列与数据框中的现有列?
【问题讨论】:
-
如果要发生截断,根据其精度将其加载到双列时就会发生截断。稍后将其转换为字符串有什么意义?
-
@philantrovert 哦,我不知道。在这种情况下,我如何将数据直接读入 String ?
-
您正在从 RDBMS 中读取数据,并且您在此处获得的数据类型取决于您的源表。如果您想将其作为字符串,请将 db_table 参数中的 select 子句更改为
cast(ptd_balance as string)或varchar或您的 rdbms 支持的任何内容。 -
@philantrovert 这是您建议阅读表格的方式吗:选择地理、项目、reference_code、product_line、book_type、cc_region、cc_channel、cc_function、pl_market、ptd_balance、qtd_balance、ytd_balance、xx_last_update_tms、xx_last_update_log_id、 xx_data_hash_code,xx_data_hash_id, ptd_balance::character 变化为 'ptd_balance_text', qtd_balance::character 变化为 'qtd_balance_text', ytd_balance::character 变化为 'ytd_balance_text' from schema.tablename where period_year='2017' 我给出了演员表(:: ) 到字符串(Greenplum 中不同的字符)。
标签: scala apache-spark hadoop