问题原因
分别从MySQL和Hive获取数据进行join操作。由于join时,使用的字段数据类型不同,导致无法join。需要指定字段数据类型,让左右两边的字段数据类型保持一致。
报错信息
# table1和table2表中的id在R中的数据类型不同,一个是integer64,另一个是characte
> table <- table1 %>%
inner_join(table2, by = "id") %>%
collect()
Error: Can't join on 'id' x 'id' because of incompatible types (integer64 / character)
处理方法1:在使用dbGetQuery()方法在使用SQL获取Hive中table2的数据时,将id改为cast(id as int) as id,再重新执行,继续报错:
Warning message:
Column `suite_id` has different attributes on LHS and RHS of join
处理方法2:使用sapply方法查看两个table中id的数据类型,这次虽然都是integer但是仍然不是相同的数据类型。将table2中的id改为integer64类型,执行成功。需要先安装bit或者bit64包。
# 检查table1和table2的字段数据类型
> sapply(table1, class)
id num
"integer64" "integer64"
> sapply(table2
R语言join操作中数据类型不匹配问题及解决

在尝试使用R进行MySQL与Hive数据表的join操作时,因左右两侧字段数据类型不同引发错误。报错信息提示R列数据属性不匹配。解决方法包括:1) 使用dbGetQuery()时指定数据类型;2) 将Hive表的id字段转换为integer64类型,确保两边类型一致。参考资源涉及integer64类型的转化和处理。
最低0.47元/天 解锁文章
379

被折叠的 条评论
为什么被折叠?



